Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Sort.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Support;
20 
21 namespace Lucene.Net.Search
22 {
23 
24 
99  [Serializable]
100  public class Sort
101  {
102 
108  public static readonly Sort RELEVANCE = new Sort();
109 
111  public static readonly Sort INDEXORDER;
112 
113  // internal representation of the sort criteria
114  internal SortField[] fields;
115 
120  public Sort():this(SortField.FIELD_SCORE)
121  {
122  }
123 
125  public Sort(SortField field)
126  {
127  SetSort(field);
128  }
129 
131  public Sort(params SortField[] fields)
132  {
133  SetSort(fields);
134  }
135 
137  public virtual void SetSort(SortField field)
138  {
139  this.fields = new SortField[]{field};
140  }
141 
143  public virtual void SetSort(params SortField[] fields)
144  {
145  this.fields = fields;
146  }
147 
151  public virtual SortField[] GetSort()
152  {
153  return fields;
154  }
155 
156  public override System.String ToString()
157  {
158  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
159 
160  for (int i = 0; i < fields.Length; i++)
161  {
162  buffer.Append(fields[i].ToString());
163  if ((i + 1) < fields.Length)
164  buffer.Append(',');
165  }
166 
167  return buffer.ToString();
168  }
169 
171  public override bool Equals(System.Object o)
172  {
173  if (this == o)
174  return true;
175  if (!(o is Sort))
176  return false;
177  Sort other = (Sort) o;
178 
179  bool result = false;
180  if ((this.fields == null) && (other.fields == null))
181  result = true;
182  else if ((this.fields != null) && (other.fields != null))
183  {
184  if (this.fields.Length == other.fields.Length)
185  {
186  int length = this.fields.Length;
187  result = true;
188  for (int i = 0; i < length; i++)
189  {
190  if (!(this.fields[i].Equals(other.fields[i])))
191  {
192  result = false;
193  break;
194  }
195  }
196  }
197  }
198  return result;
199  }
200 
202  public override int GetHashCode()
203  {
204  // TODO in Java 1.5: switch to Arrays.hashCode(). The
205  // Java 1.4 workaround below calculates the same hashCode
206  // as Java 1.5's new Arrays.hashCode()
207  return 0x45aaf665 + EquatableList<SortField>.GetHashCode(fields);
208  }
209  static Sort()
210  {
211  INDEXORDER = new Sort(SortField.FIELD_DOC);
212  }
213  }
214 }