App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / api / datastore / Index.java
blob4e8823223a43b71f332165780227284f9030303c
1 /*
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
12 * the License.
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;
22 /**
23 * A Datastore {@code Index} definition.
26 public final class Index implements Serializable {
28 /**
29 * Indicates the state of the {@link Index}.
31 public enum IndexState {
32 /**
33 * Indicates the given {@link Index} is being built and therefore can not
34 * be used to service queries.
36 BUILDING,
37 /**
38 * Indicates the given {@link Index} is ready to service queries.
40 SERVING,
41 /**
42 * Indicates the given {@link Index} is being deleted.
44 DELETING,
45 /**
46 * Indicates the given {@link Index} encountered an error in the
47 * {@code BUILDING} state.
49 ERROR
52 /**
53 * An indexed property.
55 public static class Property implements Serializable {
57 private static final long serialVersionUID = -5946842287951548217L;
59 private String name;
60 private SortDirection direction;
62 @SuppressWarnings("unused")
63 private Property() {}
65 /**
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) {
72 if (name == null) {
73 throw new NullPointerException("name must not be null");
75 if (direction == null) {
76 throw new NullPointerException("direction must not be null");
78 this.name = name;
79 this.direction = direction;
82 public String getName() {
83 return name;
86 public SortDirection getDirection() {
87 return direction;
90 @Override
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);
96 return false;
99 @Override
100 public int hashCode() {
101 return name.hashCode() * 31 + direction.hashCode();
104 @Override
105 public String toString() {
106 return name + " " + direction;
110 private static final long serialVersionUID = 8595801877003574982L;
112 private long id;
113 private String kind;
114 private boolean isAncestor;
115 private List<Property> properties;
117 @SuppressWarnings("unused")
118 private Index() {}
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) {
131 if (kind == null) {
132 throw new NullPointerException("kind must not be null");
134 if (properties == null) {
135 throw new NullPointerException("properties must not be null");
137 this.id = id;
138 this.kind = kind;
139 this.isAncestor = isAncestor;
140 this.properties = properties;
143 public long getId() {
144 return id;
148 * Get the index's kind, or the empty string ("") if it has none.
150 public String getKind() {
151 return kind;
154 public boolean isAncestor() {
155 return isAncestor;
158 public List<Property> getProperties() {
159 return Collections.unmodifiableList(properties);
162 @Override
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);
171 return false;
174 @Override
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();
182 @Override
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(")");
193 if (isAncestor) {
194 stBuilder.append(" INCLUDES ANCESTORS");
196 return stBuilder.toString();