1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.util.PriorityQueue; 20 21 import java.text.Collator ; 22 import java.util.Locale ; 23 24 34 class FieldDocSortedHitQueue 35 extends PriorityQueue { 36 37 volatile SortField[] fields; 40 41 volatile Collator [] collators; 44 45 46 51 FieldDocSortedHitQueue (SortField[] fields, int size) { 52 this.fields = fields; 53 this.collators = hasCollators (fields); 54 initialize (size); 55 } 56 57 58 66 synchronized void setFields (SortField[] fields) { 67 if (this.fields == null) { 68 this.fields = fields; 69 this.collators = hasCollators (fields); 70 } 71 } 72 73 74 75 SortField[] getFields() { 76 return fields; 77 } 78 79 80 85 private Collator [] hasCollators (final SortField[] fields) { 86 if (fields == null) return null; 87 Collator [] ret = new Collator [fields.length]; 88 for (int i=0; i<fields.length; ++i) { 89 Locale locale = fields[i].getLocale(); 90 if (locale != null) 91 ret[i] = Collator.getInstance (locale); 92 } 93 return ret; 94 } 95 96 97 103 protected final boolean lessThan (final Object a, final Object b) { 104 final FieldDoc docA = (FieldDoc) a; 105 final FieldDoc docB = (FieldDoc) b; 106 final int n = fields.length; 107 int c = 0; 108 for (int i=0; i<n && c==0; ++i) { 109 final int type = fields[i].getType(); 110 switch (type) { 111 case SortField.SCORE: 112 float r1 = ((Float )docA.fields[i]).floatValue(); 113 float r2 = ((Float )docB.fields[i]).floatValue(); 114 if (r1 > r2) c = -1; 115 if (r1 < r2) c = 1; 116 break; 117 case SortField.DOC: 118 case SortField.INT: 119 int i1 = ((Integer )docA.fields[i]).intValue(); 120 int i2 = ((Integer )docB.fields[i]).intValue(); 121 if (i1 < i2) c = -1; 122 if (i1 > i2) c = 1; 123 break; 124 case SortField.STRING: 125 String s1 = (String ) docA.fields[i]; 126 String s2 = (String ) docB.fields[i]; 127 if (s1 == null) c = (s2==null) ? 0 : -1; 131 else if (s2 == null) c = 1; else if (fields[i].getLocale() == null) { 133 c = s1.compareTo(s2); 134 } else { 135 c = collators[i].compare (s1, s2); 136 } 137 break; 138 case SortField.FLOAT: 139 float f1 = ((Float )docA.fields[i]).floatValue(); 140 float f2 = ((Float )docB.fields[i]).floatValue(); 141 if (f1 < f2) c = -1; 142 if (f1 > f2) c = 1; 143 break; 144 case SortField.CUSTOM: 145 c = docA.fields[i].compareTo (docB.fields[i]); 146 break; 147 case SortField.AUTO: 148 throw new RuntimeException ("FieldDocSortedHitQueue cannot use an AUTO SortField"); 153 default: 154 throw new RuntimeException ("invalid SortField type: "+type); 155 } 156 if (fields[i].getReverse()) { 157 c = -c; 158 } 159 } 160 161 if (c == 0) 163 return docA.doc > docB.doc; 164 165 return c > 0; 166 } 167 } 168 | Popular Tags |