Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / Index.java
blob6a08048ab15b081ea77c761a97d2d3cf845f467c
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;
21 import java.util.Objects;
23 /**
24 * A Datastore {@code Index} definition.
27 public final class Index implements Serializable {
29 /**
30 * Indicates the state of the {@link Index}.
32 public enum IndexState {
33 /**
34 * Indicates the given {@link Index} is being built and therefore can not
35 * be used to service queries.
37 BUILDING,
38 /**
39 * Indicates the given {@link Index} is ready to service queries.
41 SERVING,
42 /**
43 * Indicates the given {@link Index} is being deleted.
45 DELETING,
46 /**
47 * Indicates the given {@link Index} encountered an error in the
48 * {@code BUILDING} state.
50 ERROR
53 /**
54 * An indexed property.
56 public static class Property implements Serializable {
58 private static final long serialVersionUID = -5946842287951548217L;
60 private String name;
61 private SortDirection direction;
63 @SuppressWarnings("unused")
64 private Property() {}
66 /**
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) {
73 if (name == null) {
74 throw new NullPointerException("name must not be null");
76 this.name = name;
77 this.direction = direction;
80 public String getName() {
81 return name;
84 public SortDirection getDirection() {
85 return direction;
88 @Override
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);
94 return false;
97 @Override
98 public int hashCode() {
99 return name.hashCode() * 31 + (direction == null ? 0 : direction.hashCode());
102 @Override
103 public String toString() {
104 return name + " " + direction;
108 private static final long serialVersionUID = 8595801877003574982L;
110 private long id;
111 private String kind;
112 private boolean isAncestor;
113 private List<Property> properties;
115 @SuppressWarnings("unused")
116 private Index() {}
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) {
129 if (kind == null) {
130 throw new NullPointerException("kind must not be null");
132 if (properties == null) {
133 throw new NullPointerException("properties must not be null");
135 this.id = id;
136 this.kind = kind;
137 this.isAncestor = isAncestor;
138 this.properties = properties;
141 public long getId() {
142 return id;
146 * Get the index's kind, or the empty string ("") if it has none.
148 public String getKind() {
149 return kind;
152 public boolean isAncestor() {
153 return isAncestor;
156 public List<Property> getProperties() {
157 return Collections.unmodifiableList(properties);
160 @Override
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);
169 return false;
172 @Override
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();
180 @Override
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(")");
191 if (isAncestor) {
192 stBuilder.append(" INCLUDES ANCESTORS");
194 return stBuilder.toString();