Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / IndexSpec.java
blob9260294ba5b156375ffa2f4ecf8806268c4e68e9
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 import com.google.appengine.api.search.checkers.IndexChecker;
7 /**
8 * Represents information about an index. This class is used to fully specify
9 * the index you want to retrieve from the {@link SearchService}.
10 * To build an instance use the {@link #newBuilder()} method and set
11 * all required parameters, plus optional values different than the defaults.
12 * <pre>
13 * SearchService searchService = SearchServiceFactory.getSearchService();
15 * IndexSpec spec = IndexSpec.newBuilder()
16 * .setName("docs")
17 * .setConsistency(Consistency.PER_DOCUMENT)
18 * .build();
20 * Index index = searchService.getIndex(spec);
21 * </pre>
24 public class IndexSpec {
26 /**
27 * A builder of IndexSpec.
29 public static final class Builder {
30 private String name;
31 private Consistency consistency;
33 /**
34 * Constructs a builder for an IndexSpec.
36 private Builder() {
39 /**
40 * Sets the unique name of the index.
42 * @param name the name of the index
43 * @return this Builder
44 * @throws IllegalArgumentException if the index name length is not between 1
45 * and {@literal IndexChecker#MAXIMUM_INDEX_NAME_LENGTH}
47 public Builder setName(String name) {
48 this.name = IndexChecker.checkName(name);
49 return this;
52 /**
53 * Sets the consistency mode in which the index operates.
55 * @param consistency the consistency mode in which the index operates
56 * @return this Builder
58 public Builder setConsistency(Consistency consistency) {
59 this.consistency = consistency;
60 return this;
63 /**
64 * Builds a valid IndexSpec. The builder must have set a valid
65 * index name.
67 * @return the IndexSpec built by this builder
68 * @throws IllegalArgumentException if the IndexSpec built is not valid
70 public IndexSpec build() {
71 return new IndexSpec(this);
75 private final String name;
76 private final Consistency consistency;
78 /**
79 * Creates new index specification.
81 * @param builder the IndexSpec builder to use to construct an instance
82 * @throws IllegalArgumentException if the index name is invalid
84 private IndexSpec(Builder builder) {
85 consistency = Util.defaultIfNull(builder.consistency, Consistency.PER_DOCUMENT);
86 name = builder.name;
89 /**
90 * @return the name of the index
92 public String getName() {
93 return name;
96 /**
97 * @return the consistency mode of this index
99 public Consistency getConsistency() {
100 return consistency;
104 * Creates a new IndexSpec builder. You must use this method to obtain a new
105 * builder. The returned builder must be used to specify all properties of
106 * the IndexSpec. To obtain the IndexSpec call the {@link Builder#build()}
107 * method on the returned builder.
109 * @return a builder which constructs a IndexSpec object
111 public static Builder newBuilder() {
112 return new Builder();
115 @Override
116 public int hashCode() {
117 final int prime = 31;
118 int result = 1;
119 result = prime * result + ((consistency == null) ? 0 : consistency.hashCode());
120 result = prime * result + ((name == null) ? 0 : name.hashCode());
121 return result;
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
129 if (obj == null) {
130 return false;
132 if (getClass() != obj.getClass()) {
133 return false;
135 IndexSpec other = (IndexSpec) obj;
136 if (consistency == null) {
137 if (other.consistency != null) {
138 return false;
140 } else if (!consistency.equals(other.consistency)) {
141 return false;
143 if (name == null) {
144 if (other.name != null) {
145 return false;
147 } else if (!name.equals(other.name)) {
148 return false;
150 return true;
153 @Override
154 public String toString() {
155 return String.format("IndexSpec{name: %s, consistency: %s}", name, consistency.name());
159 * Creates an SearchServicePb.IndexSpec from the given Index.
161 * @param namespace the namespace for the index
162 * @return a valid SearchServicePb.IndexSpec.Builder
163 * @throws IllegalArgumentException if the consistency is not a known type
165 SearchServicePb.IndexSpec.Builder copyToProtocolBuffer(String namespace) {
166 SearchServicePb.IndexSpec.Builder builder = SearchServicePb.IndexSpec.newBuilder()
167 .setName(getName())
168 .setNamespace(namespace);
170 switch (getConsistency().getConsistency()) {
171 case GLOBAL:
172 builder.setConsistency(SearchServicePb.IndexSpec.Consistency.GLOBAL);
173 break;
174 case PER_DOCUMENT:
175 builder.setConsistency(SearchServicePb.IndexSpec.Consistency.PER_DOCUMENT);
176 break;
177 default:
178 throw new IllegalArgumentException(String.format("unknown consistency type %s",
179 getConsistency().getConsistency().name()));
181 return builder;