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
;
23 * A Datastore {@code Index} definition.
26 public final class Index
implements Serializable
{
29 * Indicates the state of the {@link Index}.
31 public enum IndexState
{
33 * Indicates the given {@link Index} is being built and therefore can not
34 * be used to service queries.
38 * Indicates the given {@link Index} is ready to service queries.
42 * Indicates the given {@link Index} is being deleted.
46 * Indicates the given {@link Index} encountered an error in the
47 * {@code BUILDING} state.
53 * An indexed property.
55 public static class Property
implements Serializable
{
57 private static final long serialVersionUID
= -5946842287951548217L;
60 private SortDirection direction
;
62 @SuppressWarnings("unused")
66 * Constructs a new unmodifiable {@code Property} object.
68 * @param name the property name
69 * @param direction the sort order for this property
71 Property(String name
, SortDirection direction
) {
73 throw new NullPointerException("name must not be null");
75 if (direction
== null) {
76 throw new NullPointerException("direction must not be null");
79 this.direction
= direction
;
82 public String
getName() {
86 public SortDirection
getDirection() {
91 public boolean equals(Object obj
) {
92 if (obj
instanceof Property
) {
93 Property other
= (Property
) obj
;
94 return name
.equals(other
.name
) && direction
.equals(other
.direction
);
100 public int hashCode() {
101 return name
.hashCode() * 31 + direction
.hashCode();
105 public String
toString() {
106 return name
+ " " + direction
;
110 private static final long serialVersionUID
= 8595801877003574982L;
114 private boolean isAncestor
;
115 private List
<Property
> properties
;
117 @SuppressWarnings("unused")
121 * Constructs a new unmodifiable {@code Index} object.
123 * @param id unique index identifier
124 * @param kind specifies the kind of the entities to index
125 * @param isAncestor true if the index supports a query that filters entities
126 * by the entity group parent, false otherwise.
127 * @param properties the entity properties to index. The order of the
128 * {@code properties} elements specifies the order in the index.
130 Index(long id
, String kind
, boolean isAncestor
, List
<Property
> properties
) {
132 throw new NullPointerException("kind must not be null");
134 if (properties
== null) {
135 throw new NullPointerException("properties must not be null");
139 this.isAncestor
= isAncestor
;
140 this.properties
= properties
;
143 public long getId() {
148 * Get the index's kind, or the empty string ("") if it has none.
150 public String
getKind() {
154 public boolean isAncestor() {
158 public List
<Property
> getProperties() {
159 return Collections
.unmodifiableList(properties
);
163 public boolean equals(Object obj
) {
164 if (obj
instanceof Index
) {
165 Index other
= (Index
) obj
;
166 return id
== other
.id
167 && kind
.equals(other
.kind
)
168 && isAncestor
== other
.isAncestor
169 && properties
.equals(other
.properties
);
175 public int hashCode() {
176 int result
= Long
.valueOf(id
).hashCode();
177 result
= result
* 31 + properties
.hashCode();
178 result
= result
* 31 + kind
.hashCode();
179 return result
* 31 + Boolean
.valueOf(isAncestor
).hashCode();
183 public String
toString() {
184 StringBuilder stBuilder
= new StringBuilder("INDEX [").append(id
)
185 .append("] ON ").append(kind
).append('(');
186 if (!properties
.isEmpty()) {
187 for (Property property
: properties
) {
188 stBuilder
.append(property
).append(", ");
190 stBuilder
.setLength(stBuilder
.length() - 2);
192 stBuilder
.append(")");
194 stBuilder
.append(" INCLUDES ANCESTORS");
196 return stBuilder
.toString();