Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / IndexSpec.java
blobda73def64a7238a1cbf64030c909203c9aa0e797
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 * .build();
19 * Index index = searchService.getIndex(spec);
20 * </pre>
23 public class IndexSpec {
25 /**
26 * A builder of IndexSpec.
28 public static final class Builder {
29 private String name;
31 /**
32 * Constructs a builder for an IndexSpec.
34 private Builder() {
37 /**
38 * Sets the unique name of the index.
40 * @param name the name of the index
41 * @return this Builder
42 * @throws IllegalArgumentException if the index name length is not between 1
43 * and {@literal IndexChecker#MAXIMUM_INDEX_NAME_LENGTH}
45 public Builder setName(String name) {
46 this.name = IndexChecker.checkName(name);
47 return this;
50 /**
51 * Builds a valid IndexSpec. The builder must have set a valid
52 * index name.
54 * @return the IndexSpec built by this builder
55 * @throws IllegalArgumentException if the IndexSpec built is not valid
57 public IndexSpec build() {
58 return new IndexSpec(this);
62 private final String name;
64 /**
65 * Creates new index specification.
67 * @param builder the IndexSpec builder to use to construct an instance
68 * @throws IllegalArgumentException if the index name is invalid
70 private IndexSpec(Builder builder) {
71 name = builder.name;
74 /**
75 * @return the name of the index
77 public String getName() {
78 return name;
81 /**
82 * Creates a new IndexSpec builder. You must use this method to obtain a new
83 * builder. The returned builder must be used to specify all properties of
84 * the IndexSpec. To obtain the IndexSpec call the {@link Builder#build()}
85 * method on the returned builder.
87 * @return a builder which constructs a IndexSpec object
89 public static Builder newBuilder() {
90 return new Builder();
93 @Override
94 public int hashCode() {
95 final int prime = 31;
96 int result = 1;
97 result = prime * result + ((name == null) ? 0 : name.hashCode());
98 return result;
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj) {
104 return true;
106 if (obj == null) {
107 return false;
109 if (getClass() != obj.getClass()) {
110 return false;
112 IndexSpec other = (IndexSpec) obj;
113 if (name == null) {
114 if (other.name != null) {
115 return false;
117 } else if (!name.equals(other.name)) {
118 return false;
120 return true;
123 @Override
124 public String toString() {
125 return String.format("IndexSpec{name: %s}", name);
129 * Creates an SearchServicePb.IndexSpec from the given Index.
131 * @param namespace the namespace for the index
132 * @return a valid SearchServicePb.IndexSpec.Builder
134 SearchServicePb.IndexSpec.Builder copyToProtocolBuffer(String namespace) {
135 SearchServicePb.IndexSpec.Builder builder = SearchServicePb.IndexSpec.newBuilder()
136 .setName(getName())
137 .setNamespace(namespace);
138 return builder;