KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > SortField


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.Serializable JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 /**
23  * Stores information about how to sort documents by terms in an individual
24  * field. Fields must be indexed in order to sort by them.
25  *
26  * <p>Created: Feb 11, 2004 1:25:29 PM
27  *
28  * @author Tim Jones (Nacimiento Software)
29  * @since lucene 1.4
30  * @version $Id: SortField.java 150357 2004-05-24 22:51:42Z tjones $
31  * @see Sort
32  */

33 public class SortField
34 implements Serializable JavaDoc {
35
36   /** Sort by document score (relevancy). Sort values are Float and higher
37    * values are at the front. */

38   public static final int SCORE = 0;
39
40   /** Sort by document number (index order). Sort values are Integer and lower
41    * values are at the front. */

42   public static final int DOC = 1;
43
44   /** Guess type of sort based on field contents. A regular expression is used
45    * to look at the first term indexed for the field and determine if it
46    * represents an integer number, a floating point number, or just arbitrary
47    * string characters. */

48   public static final int AUTO = 2;
49
50   /** Sort using term values as Strings. Sort values are String and lower
51    * values are at the front. */

52   public static final int STRING = 3;
53
54   /** Sort using term values as encoded Integers. Sort values are Integer and
55    * lower values are at the front. */

56   public static final int INT = 4;
57
58   /** Sort using term values as encoded Floats. Sort values are Float and
59    * lower values are at the front. */

60   public static final int FLOAT = 5;
61
62   /** Sort using a custom Comparator. Sort values are any Comparable and
63    * sorting is done according to natural order. */

64   public static final int CUSTOM = 9;
65
66   // IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace"
67
// as the above static int values. Any new values must not have the same value
68
// as FieldCache.STRING_INDEX.
69

70
71   /** Represents sorting by document score (relevancy). */
72   public static final SortField FIELD_SCORE = new SortField (null, SCORE);
73
74   /** Represents sorting by document number (index order). */
75   public static final SortField FIELD_DOC = new SortField (null, DOC);
76
77
78   private String JavaDoc field;
79   private int type = AUTO; // defaults to determining type dynamically
80
private Locale JavaDoc locale; // defaults to "natural order" (no Locale)
81
boolean reverse = false; // defaults to natural order
82
private SortComparatorSource factory;
83
84   /** Creates a sort by terms in the given field where the type of term value
85    * is determined dynamically ({@link #AUTO AUTO}).
86    * @param field Name of field to sort by, cannot be <code>null</code>.
87    */

88   public SortField (String JavaDoc field) {
89     this.field = field.intern();
90   }
91
92   /** Creates a sort, possibly in reverse, by terms in the given field where
93    * the type of term value is determined dynamically ({@link #AUTO AUTO}).
94    * @param field Name of field to sort by, cannot be <code>null</code>.
95    * @param reverse True if natural order should be reversed.
96    */

97   public SortField (String JavaDoc field, boolean reverse) {
98     this.field = field.intern();
99     this.reverse = reverse;
100   }
101
102   /** Creates a sort by terms in the given field with the type of term
103    * values explicitly given.
104    * @param field Name of field to sort by. Can be <code>null</code> if
105    * <code>type</code> is SCORE or DOC.
106    * @param type Type of values in the terms.
107    */

108   public SortField (String JavaDoc field, int type) {
109     this.field = (field != null) ? field.intern() : field;
110     this.type = type;
111   }
112
113   /** Creates a sort, possibly in reverse, by terms in the given field with the
114    * type of term values explicitly given.
115    * @param field Name of field to sort by. Can be <code>null</code> if
116    * <code>type</code> is SCORE or DOC.
117    * @param type Type of values in the terms.
118    * @param reverse True if natural order should be reversed.
119    */

120   public SortField (String JavaDoc field, int type, boolean reverse) {
121     this.field = (field != null) ? field.intern() : field;
122     this.type = type;
123     this.reverse = reverse;
124   }
125
126   /** Creates a sort by terms in the given field sorted
127    * according to the given locale.
128    * @param field Name of field to sort by, cannot be <code>null</code>.
129    * @param locale Locale of values in the field.
130    */

131   public SortField (String JavaDoc field, Locale JavaDoc locale) {
132     this.field = field.intern();
133     this.type = STRING;
134     this.locale = locale;
135   }
136
137   /** Creates a sort, possibly in reverse, by terms in the given field sorted
138    * according to the given locale.
139    * @param field Name of field to sort by, cannot be <code>null</code>.
140    * @param locale Locale of values in the field.
141    */

142   public SortField (String JavaDoc field, Locale JavaDoc locale, boolean reverse) {
143     this.field = field.intern();
144     this.type = STRING;
145     this.locale = locale;
146     this.reverse = reverse;
147   }
148
149   /** Creates a sort with a custom comparison function.
150    * @param field Name of field to sort by; cannot be <code>null</code>.
151    * @param comparator Returns a comparator for sorting hits.
152    */

153   public SortField (String JavaDoc field, SortComparatorSource comparator) {
154     this.field = (field != null) ? field.intern() : field;
155     this.type = CUSTOM;
156     this.factory = comparator;
157   }
158
159   /** Creates a sort, possibly in reverse, with a custom comparison function.
160    * @param field Name of field to sort by; cannot be <code>null</code>.
161    * @param comparator Returns a comparator for sorting hits.
162    * @param reverse True if natural order should be reversed.
163    */

164   public SortField (String JavaDoc field, SortComparatorSource comparator, boolean reverse) {
165     this.field = (field != null) ? field.intern() : field;
166     this.type = CUSTOM;
167     this.reverse = reverse;
168     this.factory = comparator;
169   }
170
171   /** Returns the name of the field. Could return <code>null</code>
172    * if the sort is by SCORE or DOC.
173    * @return Name of field, possibly <code>null</code>.
174    */

175   public String JavaDoc getField() {
176     return field;
177   }
178
179   /** Returns the type of contents in the field.
180    * @return One of the constants SCORE, DOC, AUTO, STRING, INT or FLOAT.
181    */

182   public int getType() {
183     return type;
184   }
185
186   /** Returns the Locale by which term values are interpreted.
187    * May return <code>null</code> if no Locale was specified.
188    * @return Locale, or <code>null</code>.
189    */

190   public Locale JavaDoc getLocale() {
191     return locale;
192   }
193
194   /** Returns whether the sort should be reversed.
195    * @return True if natural order should be reversed.
196    */

197   public boolean getReverse() {
198     return reverse;
199   }
200
201   public SortComparatorSource getFactory() {
202     return factory;
203   }
204
205   public String JavaDoc toString() {
206     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
207     switch (type) {
208       case SCORE: buffer.append("<score>");
209                   break;
210
211       case DOC: buffer.append("<doc>");
212                 break;
213
214       case CUSTOM: buffer.append ("<custom:\"" + field + "\": "
215                                                + factory + ">");
216                 break;
217
218       default: buffer.append("\"" + field + "\"");
219                break;
220     }
221
222     if (locale != null) buffer.append ("("+locale+")");
223     if (reverse) buffer.append('!');
224
225     return buffer.toString();
226   }
227 }
228
Popular Tags