View Javadoc
1   /*
2    * #%L
3    * Index.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  
21  package com.allanbank.mongodb.builder;
22  
23  import com.allanbank.mongodb.bson.element.IntegerElement;
24  import com.allanbank.mongodb.bson.element.StringElement;
25  
26  /**
27   * Provides the ability to easily specify the index type of a field within an
28   * index specification.
29   * 
30   * @api.yes This class is part of the driver's API. Public and protected members
31   *          will be deprecated for at least 1 non-bugfix release (version
32   *          numbers are <major>.<minor>.<bugfix>) before being
33   *          removed or modified.
34   * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
35   */
36  public final class Index {
37  
38      /** The value to indicate an ascending index order. */
39      public static final int ASCENDING = Sort.ASCENDING;
40  
41      /** The value to indicate an descending index order. */
42      public static final int DESCENDING = Sort.DESCENDING;
43  
44      /** The value for the {@value} index. */
45      public static final String GEO_2D_INDEX_NAME = "2d";
46  
47      /** The value for the {@value} index. */
48      public static final String GEO_2DSPHERE_INDEX_NAME = "2dsphere";
49  
50      /** The value for the {@value} index. */
51      public static final String GEO_HAYSTACK_INDEX_NAME = "geoHaystack";
52  
53      /** The value for the {@value} index. */
54      public static final String HASHED_INDEX_NAME = "hashed";
55  
56      /** The value for the {@value} index. */
57      public static final String TEXT_INDEX_NAME = "text";
58  
59      /**
60       * Creates an ascending order specification, e.g.,
61       * <tt>{ &lt;field&gt; : 1 }</tt>.
62       * <p>
63       * This method is equivalent to {@link Sort#asc(String)} method.
64       * </p>
65       * 
66       * @param field
67       *            The field to create the ascending index on.
68       * @return The ascending sort specification.
69       */
70      public static IntegerElement asc(final String field) {
71          return Sort.asc(field);
72      }
73  
74      /**
75       * Creates an descending order specification, e.g.,
76       * <tt>{ &lt;field&gt; : -1 }</tt>.
77       * <p>
78       * This method is equivalent to the {@link Sort#desc(String)} method.
79       * </p>
80       * 
81       * @param field
82       *            The field to create the descending index on.
83       * @return The descending sort specification.
84       */
85      public static IntegerElement desc(final String field) {
86          return Sort.desc(field);
87      }
88  
89      /**
90       * Creates an 2D index specification, e.g.,
91       * <tt>{ &lt;field&gt; : "2d" }</tt>.
92       * 
93       * @param field
94       *            The field to create the '2d' index on.
95       * @return The 2D index specification.
96       */
97      public static StringElement geo2d(final String field) {
98          return new StringElement(field, GEO_2D_INDEX_NAME);
99      }
100 
101     /**
102      * Creates an 2D Sphere index specification, e.g.,
103      * <tt>{ &lt;field&gt; : "2dsphere" }</tt>.
104      * 
105      * @param field
106      *            The field to create the '2dsphere' index on.
107      * @return The 2D Sphere index specification.
108      * @since MongoDB 2.4
109      */
110     public static StringElement geo2dSphere(final String field) {
111         return new StringElement(field, GEO_2DSPHERE_INDEX_NAME);
112     }
113 
114     /**
115      * Creates a haystack index specification, e.g.,
116      * <tt>{ &lt;field&gt; : "geoHaystack" }</tt>.
117      * 
118      * @param field
119      *            The field to create the 'geoHaystack' index on.
120      * @return The 2D Sphere index specification.
121      * @see <a
122      *      href="http://docs.mongodb.org/manual/applications/geohaystack/">Haystack
123      *      Index Documentation</a>
124      */
125     public static StringElement geoHaystack(final String field) {
126         return new StringElement(field, GEO_HAYSTACK_INDEX_NAME);
127     }
128 
129     /**
130      * Creates an 'hashed' index specification, e.g.,
131      * <tt>{ &lt;field&gt; : "hashed" }</tt>.
132      * 
133      * @param field
134      *            The field to create the 'hashed' index on.
135      * @return The 'hashed' index specification.
136      * @since MongoDB 2.4
137      */
138     public static StringElement hashed(final String field) {
139         return new StringElement(field, HASHED_INDEX_NAME);
140     }
141 
142     /**
143      * Creates an 'text' index specification, e.g.,
144      * <tt>{ &lt;field&gt; : "text" }</tt>.
145      * <p>
146      * <b>Note:</b> MongoDB Inc. considers text indexes to be an experimental
147      * feature in the 2.4 release. Use with <b>extreme</b> caution. At a minimum
148      * make sure you have read the <a
149      * href="http://docs.mongodb.org/manual/release-notes/2.4/#text-indexes"
150      * >MongoDB Text Index Documentation</a>.
151      * </p>
152      * 
153      * @param field
154      *            The field to create the 'text' index on.
155      * @return The 'text' index specification.
156      * @since MongoDB 2.4
157      * @see <a
158      *      href="http://docs.mongodb.org/manual/release-notes/2.4/#text-indexes">MongoDB
159      *      Text Index Documentation</a>
160      */
161     public static StringElement text(final String field) {
162         return new StringElement(field, TEXT_INDEX_NAME);
163     }
164 
165     /**
166      * Creates a new Sort.
167      */
168     private Index() {
169         super();
170     }
171 }