19 using System.Globalization;
20 using Lucene.Net.Support;
23 using Single = Lucene.Net.Support.Single;
24 using Term = Lucene.Net.Index.Term;
28 namespace Lucene.Net.Search
43 public const int SCORE = 0;
48 public const int DOC = 1;
55 public const int STRING = 3;
60 public const int INT = 4;
65 public const int FLOAT = 5;
70 public const int LONG = 6;
75 public const int DOUBLE = 7;
80 public const int SHORT = 8;
85 public const int CUSTOM = 9;
90 public const int BYTE = 10;
97 public const int STRING_VAL = 11;
109 private System.String field;
111 private System.Globalization.CultureInfo locale;
112 internal bool reverse =
false;
113 private Lucene.Net.Search.Parser parser;
128 InitFieldType(field, type);
141 public SortField(System.String field,
int type,
bool reverse)
143 InitFieldType(field, type);
144 this.reverse = reverse;
160 public SortField(System.String field, Lucene.Net.Search.Parser parser):this(field, parser, false)
179 public SortField(System.String field, Lucene.Net.Search.Parser parser,
bool reverse)
181 if (parser is Lucene.Net.Search.IntParser)
182 InitFieldType(field, INT);
183 else if (parser is Lucene.Net.Search.FloatParser)
184 InitFieldType(field, FLOAT);
185 else if (parser is Lucene.Net.Search.ShortParser)
186 InitFieldType(field, SHORT);
187 else if (parser is Lucene.Net.Search.ByteParser)
188 InitFieldType(field, BYTE);
189 else if (parser is Lucene.Net.Search.LongParser)
190 InitFieldType(field, LONG);
191 else if (parser is Lucene.Net.Search.DoubleParser)
192 InitFieldType(field, DOUBLE);
195 throw new System.ArgumentException(
"Parser instance does not subclass existing numeric parser from FieldCache (got " + parser +
")");
198 this.reverse = reverse;
199 this.parser = parser;
209 public SortField(System.String field, System.Globalization.CultureInfo locale)
211 InitFieldType(field, STRING);
212 this.locale = locale;
222 public SortField(System.String field, System.Globalization.CultureInfo locale,
bool reverse)
224 InitFieldType(field, STRING);
225 this.locale = locale;
226 this.reverse = reverse;
236 InitFieldType(field, CUSTOM);
237 this.comparatorSource = comparator;
249 InitFieldType(field, CUSTOM);
250 this.reverse = reverse;
251 this.comparatorSource = comparator;
256 private void InitFieldType(System.String field,
int type)
261 if (type != SCORE && type != DOC)
262 throw new System.ArgumentException(
"field can only be null when type is SCORE or DOC");
274 public virtual string Field
276 get {
return field; }
281 public virtual int Type
290 public virtual CultureInfo Locale
292 get {
return locale; }
301 get {
return parser; }
306 public virtual bool Reverse
308 get {
return reverse; }
317 get {
return comparatorSource; }
320 public override System.String ToString()
322 System.Text.StringBuilder buffer =
new System.Text.StringBuilder();
327 buffer.Append(
"<score>");
331 buffer.Append(
"<doc>");
335 buffer.Append(
"<string: \"").Append(field).Append(
"\">");
339 buffer.Append(
"<string_val: \"").Append(field).Append(
"\">");
343 buffer.Append(
"<byte: \"").Append(field).Append(
"\">");
347 buffer.Append(
"<short: \"").Append(field).Append(
"\">");
351 buffer.Append(
"<int: \"").Append(field).Append(
"\">");
355 buffer.Append(
"<long: \"").Append(field).Append(
"\">");
359 buffer.Append(
"<float: \"").Append(field).Append(
"\">");
363 buffer.Append(
"<double: \"").Append(field).Append(
"\">");
367 buffer.Append(
"<custom:\"").Append(field).Append(
"\": ").Append(comparatorSource).Append(
'>');
371 buffer.Append(
"<???: \"").Append(field).Append(
"\">");
377 buffer.Append(
'(').Append(locale).Append(
')');
379 buffer.Append(
'(').Append(parser).Append(
')');
383 return buffer.ToString();
391 public override bool Equals(System.Object o)
398 return ((System.Object) other.field == (System.Object) this.field && other.type ==
this.type &&
399 other.reverse ==
this.reverse &&
400 (other.locale == null ?
this.locale == null : other.locale.Equals(
this.locale)) &&
401 (other.comparatorSource == null
402 ?
this.comparatorSource == null
403 : other.comparatorSource.Equals(
this.comparatorSource)) &&
404 (other.parser == null ?
this.parser == null : other.parser.Equals(
this.parser)));
413 public override int GetHashCode()
415 int hash = type ^ 0x346565dd + (reverse ? Boolean.TrueString.GetHashCode() : Boolean.FalseString.GetHashCode()) ^ unchecked((
int) 0xaf5998bb);
417 hash += (field.GetHashCode() ^ unchecked((
int) 0xff5685dd));
420 hash += (locale.GetHashCode() ^ 0x08150815);
422 if (comparatorSource != null)
423 hash += comparatorSource.GetHashCode();
425 hash += (parser.GetHashCode() ^ 0x3aaf56ff);
437 [System.Runtime.Serialization.OnDeserialized]
438 internal void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
467 return new FieldComparator.StringComparatorLocale(numHits, field, locale);
497 System.Diagnostics.Debug.Assert(comparatorSource != null);
498 return comparatorSource.NewComparator(field, numHits, sortPos, reverse);
501 return new FieldComparator.StringOrdValComparator(numHits, field, sortPos, reverse);
507 throw new System.SystemException(
"Illegal sort type: " + type);