2 * Copyright 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package com
.google
.appengine
.api
.datastore
;
16 import com
.google
.appengine
.api
.datastore
.Query
.SortDirection
;
18 import java
.io
.Serializable
;
19 import java
.util
.Collections
;
20 import java
.util
.List
;
21 import java
.util
.Objects
;
24 * A Datastore {@code Index} definition.
27 public final class Index
implements Serializable
{
30 * Indicates the state of the {@link Index}.
32 public enum IndexState
{
34 * Indicates the given {@link Index} is being built and therefore can not
35 * be used to service queries.
39 * Indicates the given {@link Index} is ready to service queries.
43 * Indicates the given {@link Index} is being deleted.
47 * Indicates the given {@link Index} encountered an error in the
48 * {@code BUILDING} state.
54 * An indexed property.
56 public static class Property
implements Serializable
{
58 private static final long serialVersionUID
= -5946842287951548217L;
61 private SortDirection direction
;
63 @SuppressWarnings("unused")
67 * Constructs a new unmodifiable {@code Property} object.
69 * @param name the property name
70 * @param direction the sort order for this property
72 Property(String name
, SortDirection direction
) {
74 throw new NullPointerException("name must not be null");
77 this.direction
= direction
;
80 public String
getName() {
84 public SortDirection
getDirection() {
89 public boolean equals(Object obj
) {
90 if (obj
instanceof Property
) {
91 Property other
= (Property
) obj
;
92 return name
.equals(other
.name
) && Objects
.equals(direction
, other
.direction
);
98 public int hashCode() {
99 return name
.hashCode() * 31 + (direction
== null ?
0 : direction
.hashCode());
103 public String
toString() {
104 return name
+ " " + direction
;
108 private static final long serialVersionUID
= 8595801877003574982L;
112 private boolean isAncestor
;
113 private List
<Property
> properties
;
115 @SuppressWarnings("unused")
119 * Constructs a new unmodifiable {@code Index} object.
121 * @param id unique index identifier
122 * @param kind specifies the kind of the entities to index
123 * @param isAncestor true if the index supports a query that filters entities
124 * by the entity group parent, false otherwise.
125 * @param properties the entity properties to index. The order of the
126 * {@code properties} elements specifies the order in the index.
128 Index(long id
, String kind
, boolean isAncestor
, List
<Property
> properties
) {
130 throw new NullPointerException("kind must not be null");
132 if (properties
== null) {
133 throw new NullPointerException("properties must not be null");
137 this.isAncestor
= isAncestor
;
138 this.properties
= properties
;
141 public long getId() {
146 * Get the index's kind, or the empty string ("") if it has none.
148 public String
getKind() {
152 public boolean isAncestor() {
156 public List
<Property
> getProperties() {
157 return Collections
.unmodifiableList(properties
);
161 public boolean equals(Object obj
) {
162 if (obj
instanceof Index
) {
163 Index other
= (Index
) obj
;
164 return id
== other
.id
165 && kind
.equals(other
.kind
)
166 && isAncestor
== other
.isAncestor
167 && properties
.equals(other
.properties
);
173 public int hashCode() {
174 int result
= Long
.valueOf(id
).hashCode();
175 result
= result
* 31 + properties
.hashCode();
176 result
= result
* 31 + kind
.hashCode();
177 return result
* 31 + Boolean
.valueOf(isAncestor
).hashCode();
181 public String
toString() {
182 StringBuilder stBuilder
= new StringBuilder("INDEX [").append(id
)
183 .append("] ON ").append(kind
).append('(');
184 if (!properties
.isEmpty()) {
185 for (Property property
: properties
) {
186 stBuilder
.append(property
).append(", ");
188 stBuilder
.setLength(stBuilder
.length() - 2);
190 stBuilder
.append(")");
192 stBuilder
.append(" INCLUDES ANCESTORS");
194 return stBuilder
.toString();