mirror of
https://github.com/apache/lucene.git
synced 2025-03-01 22:09:24 +00:00
Remove my warning and also prevent creation of additional variable. Moved @SuppressWarnings up to the method. Also do some tab->spaces cleanup
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@829077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6a06b0f0dd
commit
0a36a22599
@ -30,161 +30,159 @@ import java.util.Locale;
|
||||
*
|
||||
* @since lucene 1.4
|
||||
*/
|
||||
class FieldDocSortedHitQueue
|
||||
extends PriorityQueue<FieldDoc> {
|
||||
class FieldDocSortedHitQueue extends PriorityQueue<FieldDoc> {
|
||||
|
||||
volatile SortField[] fields;
|
||||
volatile SortField[] fields;
|
||||
|
||||
// used in the case where the fields are sorted by locale
|
||||
// based strings
|
||||
volatile Collator[] collators;
|
||||
// used in the case where the fields are sorted by locale
|
||||
// based strings
|
||||
volatile Collator[] collators;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a hit queue sorted by the given list of fields.
|
||||
* @param fields Fieldable names, in priority order (highest priority first).
|
||||
* @param size The number of hits to retain. Must be greater than zero.
|
||||
*/
|
||||
FieldDocSortedHitQueue (SortField[] fields, int size) {
|
||||
this.fields = fields;
|
||||
this.collators = hasCollators (fields);
|
||||
initialize (size);
|
||||
}
|
||||
/**
|
||||
* Creates a hit queue sorted by the given list of fields.
|
||||
* @param fields Fieldable names, in priority order (highest priority first).
|
||||
* @param size The number of hits to retain. Must be greater than zero.
|
||||
*/
|
||||
FieldDocSortedHitQueue (SortField[] fields, int size) {
|
||||
this.fields = fields;
|
||||
this.collators = hasCollators (fields);
|
||||
initialize (size);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows redefinition of sort fields if they are <code>null</code>.
|
||||
* This is to handle the case using ParallelMultiSearcher where the
|
||||
* original list contains AUTO and we don't know the actual sort
|
||||
* type until the values come back. The fields can only be set once.
|
||||
* This method is thread safe.
|
||||
* @param fields
|
||||
*/
|
||||
synchronized void setFields (SortField[] fields) {
|
||||
this.fields = fields;
|
||||
this.collators = hasCollators (fields);
|
||||
}
|
||||
/**
|
||||
* Allows redefinition of sort fields if they are <code>null</code>.
|
||||
* This is to handle the case using ParallelMultiSearcher where the
|
||||
* original list contains AUTO and we don't know the actual sort
|
||||
* type until the values come back. The fields can only be set once.
|
||||
* This method is thread safe.
|
||||
* @param fields
|
||||
*/
|
||||
synchronized void setFields (SortField[] fields) {
|
||||
this.fields = fields;
|
||||
this.collators = hasCollators (fields);
|
||||
}
|
||||
|
||||
|
||||
/** Returns the fields being used to sort. */
|
||||
SortField[] getFields() {
|
||||
return fields;
|
||||
}
|
||||
/** Returns the fields being used to sort. */
|
||||
SortField[] getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
/** Returns an array of collators, possibly <code>null</code>. The collators
|
||||
* correspond to any SortFields which were given a specific locale.
|
||||
* @param fields Array of sort fields.
|
||||
* @return Array, possibly <code>null</code>.
|
||||
*/
|
||||
private Collator[] hasCollators (final SortField[] fields) {
|
||||
if (fields == null) return null;
|
||||
Collator[] ret = new Collator[fields.length];
|
||||
for (int i=0; i<fields.length; ++i) {
|
||||
Locale locale = fields[i].getLocale();
|
||||
if (locale != null)
|
||||
ret[i] = Collator.getInstance (locale);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/** Returns an array of collators, possibly <code>null</code>. The collators
|
||||
* correspond to any SortFields which were given a specific locale.
|
||||
* @param fields Array of sort fields.
|
||||
* @return Array, possibly <code>null</code>.
|
||||
*/
|
||||
private Collator[] hasCollators (final SortField[] fields) {
|
||||
if (fields == null) return null;
|
||||
Collator[] ret = new Collator[fields.length];
|
||||
for (int i=0; i<fields.length; ++i) {
|
||||
Locale locale = fields[i].getLocale();
|
||||
if (locale != null)
|
||||
ret[i] = Collator.getInstance (locale);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether <code>a</code> is less relevant than <code>b</code>.
|
||||
* @param a ScoreDoc
|
||||
* @param b ScoreDoc
|
||||
* @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.
|
||||
*/
|
||||
protected final boolean lessThan (final FieldDoc docA, final FieldDoc docB) {
|
||||
final int n = fields.length;
|
||||
int c = 0;
|
||||
for (int i=0; i<n && c==0; ++i) {
|
||||
final int type = fields[i].getType();
|
||||
switch (type) {
|
||||
case SortField.SCORE:{
|
||||
float r1 = ((Float)docA.fields[i]).floatValue();
|
||||
float r2 = ((Float)docB.fields[i]).floatValue();
|
||||
if (r1 > r2) c = -1;
|
||||
if (r1 < r2) c = 1;
|
||||
break;
|
||||
/**
|
||||
* Returns whether <code>a</code> is less relevant than <code>b</code>.
|
||||
* @param a ScoreDoc
|
||||
* @param b ScoreDoc
|
||||
* @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected final boolean lessThan (final FieldDoc docA, final FieldDoc docB) {
|
||||
final int n = fields.length;
|
||||
int c = 0;
|
||||
for (int i=0; i<n && c==0; ++i) {
|
||||
final int type = fields[i].getType();
|
||||
switch (type) {
|
||||
case SortField.SCORE:{
|
||||
float r1 = ((Float)docA.fields[i]).floatValue();
|
||||
float r2 = ((Float)docB.fields[i]).floatValue();
|
||||
if (r1 > r2) c = -1;
|
||||
if (r1 < r2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.DOC:
|
||||
case SortField.INT:{
|
||||
int i1 = ((Integer)docA.fields[i]).intValue();
|
||||
int i2 = ((Integer)docB.fields[i]).intValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
case SortField.INT:{
|
||||
int i1 = ((Integer)docA.fields[i]).intValue();
|
||||
int i2 = ((Integer)docB.fields[i]).intValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.LONG:{
|
||||
long l1 = ((Long)docA.fields[i]).longValue();
|
||||
long l2 = ((Long)docB.fields[i]).longValue();
|
||||
if (l1 < l2) c = -1;
|
||||
if (l1 > l2) c = 1;
|
||||
break;
|
||||
long l1 = ((Long)docA.fields[i]).longValue();
|
||||
long l2 = ((Long)docB.fields[i]).longValue();
|
||||
if (l1 < l2) c = -1;
|
||||
if (l1 > l2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.STRING:{
|
||||
String s1 = (String) docA.fields[i];
|
||||
String s2 = (String) docB.fields[i];
|
||||
// null values need to be sorted first, because of how FieldCache.getStringIndex()
|
||||
// works - in that routine, any documents without a value in the given field are
|
||||
// put first. If both are null, the next SortField is used
|
||||
if (s1 == null) c = (s2==null) ? 0 : -1;
|
||||
else if (s2 == null) c = 1; //
|
||||
else if (fields[i].getLocale() == null) {
|
||||
c = s1.compareTo(s2);
|
||||
} else {
|
||||
c = collators[i].compare (s1, s2);
|
||||
}
|
||||
break;
|
||||
String s1 = (String) docA.fields[i];
|
||||
String s2 = (String) docB.fields[i];
|
||||
// null values need to be sorted first, because of how FieldCache.getStringIndex()
|
||||
// works - in that routine, any documents without a value in the given field are
|
||||
// put first. If both are null, the next SortField is used
|
||||
if (s1 == null) c = (s2==null) ? 0 : -1;
|
||||
else if (s2 == null) c = 1; //
|
||||
else if (fields[i].getLocale() == null) {
|
||||
c = s1.compareTo(s2);
|
||||
} else {
|
||||
c = collators[i].compare (s1, s2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SortField.FLOAT:{
|
||||
float f1 = ((Float)docA.fields[i]).floatValue();
|
||||
float f2 = ((Float)docB.fields[i]).floatValue();
|
||||
if (f1 < f2) c = -1;
|
||||
if (f1 > f2) c = 1;
|
||||
break;
|
||||
float f1 = ((Float)docA.fields[i]).floatValue();
|
||||
float f2 = ((Float)docB.fields[i]).floatValue();
|
||||
if (f1 < f2) c = -1;
|
||||
if (f1 > f2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.DOUBLE:{
|
||||
double d1 = ((Double)docA.fields[i]).doubleValue();
|
||||
double d2 = ((Double)docB.fields[i]).doubleValue();
|
||||
if (d1 < d2) c = -1;
|
||||
if (d1 > d2) c = 1;
|
||||
break;
|
||||
double d1 = ((Double)docA.fields[i]).doubleValue();
|
||||
double d2 = ((Double)docB.fields[i]).doubleValue();
|
||||
if (d1 < d2) c = -1;
|
||||
if (d1 > d2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.BYTE:{
|
||||
int i1 = ((Byte)docA.fields[i]).byteValue();
|
||||
int i2 = ((Byte)docB.fields[i]).byteValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
int i1 = ((Byte)docA.fields[i]).byteValue();
|
||||
int i2 = ((Byte)docB.fields[i]).byteValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.SHORT:{
|
||||
int i1 = ((Short)docA.fields[i]).shortValue();
|
||||
int i2 = ((Short)docB.fields[i]).shortValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
int i1 = ((Short)docA.fields[i]).shortValue();
|
||||
int i2 = ((Short)docB.fields[i]).shortValue();
|
||||
if (i1 < i2) c = -1;
|
||||
if (i1 > i2) c = 1;
|
||||
break;
|
||||
}
|
||||
case SortField.CUSTOM:{
|
||||
// TODO: Use FieldComparator? This does not make sense!
|
||||
@SuppressWarnings("unchecked") final int temp =
|
||||
c = ((Comparable) docA.fields[i]).compareTo((Comparable) docB.fields[i]);
|
||||
break;
|
||||
c = ((Comparable) docA.fields[i]).compareTo((Comparable) docB.fields[i]);
|
||||
break;
|
||||
}
|
||||
default:{
|
||||
throw new RuntimeException ("invalid SortField type: "+type);
|
||||
throw new RuntimeException ("invalid SortField type: "+type);
|
||||
}
|
||||
}
|
||||
if (fields[i].getReverse()) {
|
||||
c = -c;
|
||||
}
|
||||
}
|
||||
if (fields[i].getReverse()) {
|
||||
c = -c;
|
||||
}
|
||||
}
|
||||
|
||||
// avoid random sort order that could lead to duplicates (bug #31241):
|
||||
if (c == 0)
|
||||
return docA.doc > docB.doc;
|
||||
|
||||
return c > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user