1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 import java.util.Arrays ; 21 import java.util.Comparator ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 25 26 class ConjunctionScorer extends Scorer { 27 private LinkedList scorers = new LinkedList (); 28 private boolean firstTime = true; 29 private boolean more = true; 30 private float coord; 31 32 public ConjunctionScorer(Similarity similarity) { 33 super(similarity); 34 } 35 36 final void add(Scorer scorer) { 37 scorers.addLast(scorer); 38 } 39 40 private Scorer first() { return (Scorer)scorers.getFirst(); } 41 private Scorer last() { return (Scorer)scorers.getLast(); } 42 43 public int doc() { return first().doc(); } 44 45 public boolean next() throws IOException { 46 if (firstTime) { 47 init(true); 48 } else if (more) { 49 more = last().next(); } 51 return doNext(); 52 } 53 54 private boolean doNext() throws IOException { 55 while (more && first().doc() < last().doc()) { more = first().skipTo(last().doc()); scorers.addLast(scorers.removeFirst()); } 59 return more; } 61 62 public boolean skipTo(int target) throws IOException { 63 if(firstTime) { 64 init(false); 65 } 66 67 Iterator i = scorers.iterator(); 68 while (more && i.hasNext()) { 69 more = ((Scorer)i.next()).skipTo(target); 70 } 71 72 if (more) 73 sortScorers(); 75 return doNext(); 76 } 77 78 public float score() throws IOException { 79 float score = 0.0f; Iterator i = scorers.iterator(); 81 while (i.hasNext()) 82 score += ((Scorer)i.next()).score(); 83 score *= coord; 84 return score; 85 } 86 87 private void init(boolean initScorers) throws IOException { 88 coord = getSimilarity().coord(scorers.size(), scorers.size()); 90 91 more = scorers.size() > 0; 92 93 if(initScorers){ 94 Iterator i = scorers.iterator(); 96 while (more && i.hasNext()) { 97 more = ((Scorer)i.next()).next(); 98 } 99 if (more) 100 sortScorers(); } 102 103 firstTime = false; 104 } 105 106 private void sortScorers() { 107 Scorer[] array = (Scorer[])scorers.toArray(new Scorer[scorers.size()]); 109 scorers.clear(); 111 Arrays.sort(array, new Comparator () { public int compare(Object o1, Object o2) { 114 return ((Scorer)o1).doc() - ((Scorer)o2).doc(); 115 } 116 }); 117 118 for (int i = 0; i < array.length; i++) { 119 scorers.addLast(array[i]); } 121 } 122 123 public Explanation explain(int doc) { 124 throw new UnsupportedOperationException (); 125 } 126 127 } 128 | Popular Tags |