1 package org.apache.lucene.search.spans; 2 3 18 19 import java.io.IOException ; 20 21 import java.util.Collection ; 22 23 import org.apache.lucene.index.IndexReader; 24 import org.apache.lucene.search.Query; 25 import org.apache.lucene.util.ToStringUtils; 26 27 28 public class SpanFirstQuery extends SpanQuery { 29 private SpanQuery match; 30 private int end; 31 32 34 public SpanFirstQuery(SpanQuery match, int end) { 35 this.match = match; 36 this.end = end; 37 } 38 39 40 public SpanQuery getMatch() { return match; } 41 42 43 public int getEnd() { return end; } 44 45 public String getField() { return match.getField(); } 46 47 public Collection getTerms() { return match.getTerms(); } 48 49 public String toString(String field) { 50 StringBuffer buffer = new StringBuffer (); 51 buffer.append("spanFirst("); 52 buffer.append(match.toString(field)); 53 buffer.append(", "); 54 buffer.append(end); 55 buffer.append(")"); 56 buffer.append(ToStringUtils.boost(getBoost())); 57 return buffer.toString(); 58 } 59 60 public Spans getSpans(final IndexReader reader) throws IOException { 61 return new Spans() { 62 private Spans spans = match.getSpans(reader); 63 64 public boolean next() throws IOException { 65 while (spans.next()) { if (end() <= end) 67 return true; 68 } 69 return false; 70 } 71 72 public boolean skipTo(int target) throws IOException { 73 if (!spans.skipTo(target)) 74 return false; 75 76 if (spans.end() <= end) return true; 78 79 return next(); } 81 82 public int doc() { return spans.doc(); } 83 public int start() { return spans.start(); } 84 public int end() { return spans.end(); } 85 86 public String toString() { 87 return "spans(" + SpanFirstQuery.this.toString() + ")"; 88 } 89 90 }; 91 } 92 93 public Query rewrite(IndexReader reader) throws IOException { 94 SpanFirstQuery clone = null; 95 96 SpanQuery rewritten = (SpanQuery) match.rewrite(reader); 97 if (rewritten != match) { 98 clone = (SpanFirstQuery) this.clone(); 99 clone.match = rewritten; 100 } 101 102 if (clone != null) { 103 return clone; } else { 105 return this; } 107 } 108 109 public boolean equals(Object o) { 110 if (this == o) return true; 111 if (!(o instanceof SpanFirstQuery)) return false; 112 113 SpanFirstQuery other = (SpanFirstQuery)o; 114 return this.end == other.end 115 && this.match.equals(other.match) 116 && this.getBoost() == other.getBoost(); 117 } 118 119 public int hashCode() { 120 int h = match.hashCode(); 121 h ^= (h << 8) | (h >>> 25); h ^= Float.floatToRawIntBits(getBoost()) ^ end; 123 return h; 124 } 125 126 127 } 128 | Popular Tags |