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
;
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
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
+ "'";
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
);
34 this.namespace
= namespace
;
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
);
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
);
66 * Perform a "lexical" comparison to {@code other} {@link AppIdNamespace}.
67 * @return See {@link String#compareTo(String)}.
70 public int compareTo(AppIdNamespace other
) {
71 int appidCompare
= appId
.compareTo(other
.appId
);
72 if (appidCompare
== 0) {
73 return namespace
.compareTo(other
.namespace
);
78 public String
getAppId() {
82 public String
getNamespace() {
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()) {
95 return appId
+ NamespaceResources
.NAMESPACE_SEPARATOR
+ namespace
;
100 public int hashCode() {
101 final int prime
= 31;
103 result
= prime
* result
+ ((appId
== null) ?
0 : appId
.hashCode());
104 result
= prime
* result
+ ((namespace
== null) ?
0 : namespace
.hashCode());
109 public boolean equals(Object obj
) {
116 if (getClass() != obj
.getClass()) {
119 AppIdNamespace other
= (AppIdNamespace
) obj
;
121 if (other
.appId
!= null) {
124 } else if (!appId
.equals(other
.appId
)) {
127 if (namespace
== null) {
128 if (other
.namespace
!= null) {
131 } else if (!namespace
.equals(other
.namespace
)) {
138 public String
toString() {
139 return toEncodedString();