Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / AppIdNamespace.java
blob46b7c03307f3cd7db32ebd86da1d6ea942f34ffe
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.apphosting.api.NamespaceResources;
7 import java.io.Serializable;
9 /**
10 * Abstraction for a "mangled" AppId. A mangled AppId is a combination
11 * of the application id and the name_space which this class will
12 * manage.
15 class AppIdNamespace implements Serializable, Comparable<AppIdNamespace> {
16 private final String appId;
17 private final String namespace;
19 private static final String BAD_APP_ID_MESSAGE =
20 "appId or namespace cannot contain '" + NamespaceResources.NAMESPACE_SEPARATOR + "'";
22 /**
23 * Constructs an {@link AppIdNamespace} given {@code #appId} and {@code #namespace}.
25 public AppIdNamespace(String appId, String namespace) {
26 if (appId == null || namespace == null) {
27 throw new IllegalArgumentException("appId or namespace may not be null");
29 if (appId.indexOf(NamespaceResources.NAMESPACE_SEPARATOR) != -1 ||
30 namespace.indexOf(NamespaceResources.NAMESPACE_SEPARATOR) != -1) {
31 throw new IllegalArgumentException(BAD_APP_ID_MESSAGE);
33 this.appId = appId;
34 this.namespace = namespace;
37 /**
38 * Converts an encoded appId/namespace to {@link AppIdNamespace}.
40 * <p>Only one form of an appId/namespace pair will be allowed. i.e. "app!"
41 * is an illegal form and must be encoded as "app".
43 * <p>An appId/namespace pair may contain at most one "!" character.
45 * @param encodedAppIdNamespace The encoded application Id/namespace string.
47 public static AppIdNamespace parseEncodedAppIdNamespace(String encodedAppIdNamespace) {
48 if (encodedAppIdNamespace == null) {
49 throw new IllegalArgumentException("appIdNamespaceString may not be null");
51 int index = encodedAppIdNamespace.indexOf(NamespaceResources.NAMESPACE_SEPARATOR);
52 if (index == -1) {
53 return new AppIdNamespace(encodedAppIdNamespace, "");
55 String appId = encodedAppIdNamespace.substring(0, index);
56 String namespace = encodedAppIdNamespace.substring(index + 1);
57 if (namespace.length() == 0) {
58 throw new IllegalArgumentException(
59 "encodedAppIdNamespace with empty namespace may not contain a '" +
60 NamespaceResources.NAMESPACE_SEPARATOR + "'");
62 return new AppIdNamespace(appId, namespace);
65 /**
66 * Perform a "lexical" comparison to {@code other} {@link AppIdNamespace}.
67 * @return See {@link String#compareTo(String)}.
69 @Override
70 public int compareTo(AppIdNamespace other) {
71 int appidCompare = appId.compareTo(other.appId);
72 if (appidCompare == 0) {
73 return namespace.compareTo(other.namespace);
75 return appidCompare;
78 public String getAppId() {
79 return appId;
82 public String getNamespace() {
83 return namespace;
86 /**
87 * Returns an "encoded" appId/namespace string.
89 * <p>Note: If the {@link #namespace} is empty, the return value is exactly the {@link #appId}.
91 public String toEncodedString() {
92 if (namespace.isEmpty()) {
93 return appId;
94 } else {
95 return appId + NamespaceResources.NAMESPACE_SEPARATOR + namespace;
99 @Override
100 public int hashCode() {
101 final int prime = 31;
102 int result = 1;
103 result = prime * result + ((appId == null) ? 0 : appId.hashCode());
104 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
105 return result;
108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj) {
111 return true;
113 if (obj == null) {
114 return false;
116 if (getClass() != obj.getClass()) {
117 return false;
119 AppIdNamespace other = (AppIdNamespace) obj;
120 if (appId == null) {
121 if (other.appId != null) {
122 return false;
124 } else if (!appId.equals(other.appId)) {
125 return false;
127 if (namespace == null) {
128 if (other.namespace != null) {
129 return false;
131 } else if (!namespace.equals(other.namespace)) {
132 return false;
134 return true;
137 @Override
138 public String toString() {
139 return toEncodedString();