KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > spans > SpanNearQuery


1 package org.apache.lucene.search.spans;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.util.ToStringUtils;
30
31 /** Matches spans which are near one another. One can specify <i>slop</i>, the
32  * maximum number of intervening unmatched positions, as well as whether
33  * matches are required to be in-order. */

34 public class SpanNearQuery extends SpanQuery {
35   private List JavaDoc clauses;
36   private int slop;
37   private boolean inOrder;
38
39   private String JavaDoc field;
40
41   /** Construct a SpanNearQuery. Matches spans matching a span from each
42    * clause, with up to <code>slop</code> total unmatched positions between
43    * them. * When <code>inOrder</code> is true, the spans from each clause
44    * must be * ordered as in <code>clauses</code>. */

45   public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) {
46
47     // copy clauses array into an ArrayList
48
this.clauses = new ArrayList JavaDoc(clauses.length);
49     for (int i = 0; i < clauses.length; i++) {
50       SpanQuery clause = clauses[i];
51       if (i == 0) { // check field
52
field = clause.getField();
53       } else if (!clause.getField().equals(field)) {
54         throw new IllegalArgumentException JavaDoc("Clauses must have same field.");
55       }
56       this.clauses.add(clause);
57     }
58
59     this.slop = slop;
60     this.inOrder = inOrder;
61   }
62
63   /** Return the clauses whose spans are matched. */
64   public SpanQuery[] getClauses() {
65     return (SpanQuery[])clauses.toArray(new SpanQuery[clauses.size()]);
66   }
67
68   /** Return the maximum number of intervening unmatched positions permitted.*/
69   public int getSlop() { return slop; }
70
71   /** Return true if matches are required to be in-order.*/
72   public boolean isInOrder() { return inOrder; }
73
74   public String JavaDoc getField() { return field; }
75
76   public Collection JavaDoc getTerms() {
77     Collection JavaDoc terms = new ArrayList JavaDoc();
78     Iterator JavaDoc i = clauses.iterator();
79     while (i.hasNext()) {
80       SpanQuery clause = (SpanQuery)i.next();
81       terms.addAll(clause.getTerms());
82     }
83     return terms;
84   }
85
86   public String JavaDoc toString(String JavaDoc field) {
87     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
88     buffer.append("spanNear([");
89     Iterator JavaDoc i = clauses.iterator();
90     while (i.hasNext()) {
91       SpanQuery clause = (SpanQuery)i.next();
92       buffer.append(clause.toString(field));
93       if (i.hasNext()) {
94         buffer.append(", ");
95       }
96     }
97     buffer.append("], ");
98     buffer.append(slop);
99     buffer.append(", ");
100     buffer.append(inOrder);
101     buffer.append(")");
102     buffer.append(ToStringUtils.boost(getBoost()));
103     return buffer.toString();
104   }
105
106   public Spans getSpans(final IndexReader reader) throws IOException JavaDoc {
107     if (clauses.size() == 0) // optimize 0-clause case
108
return new SpanOrQuery(getClauses()).getSpans(reader);
109
110     if (clauses.size() == 1) // optimize 1-clause case
111
return ((SpanQuery)clauses.get(0)).getSpans(reader);
112
113     return new NearSpans(this, reader);
114   }
115
116   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
117     SpanNearQuery clone = null;
118     for (int i = 0 ; i < clauses.size(); i++) {
119       SpanQuery c = (SpanQuery)clauses.get(i);
120       SpanQuery query = (SpanQuery) c.rewrite(reader);
121       if (query != c) { // clause rewrote: must clone
122
if (clone == null)
123           clone = (SpanNearQuery) this.clone();
124         clone.clauses.set(i,query);
125       }
126     }
127     if (clone != null) {
128       return clone; // some clauses rewrote
129
} else {
130       return this; // no clauses rewrote
131
}
132   }
133
134   /** Returns true iff <code>o</code> is equal to this. */
135   public boolean equals(Object JavaDoc o) {
136     if (this == o) return true;
137     if (!(o instanceof SpanNearQuery)) return false;
138
139     final SpanNearQuery spanNearQuery = (SpanNearQuery) o;
140
141     if (inOrder != spanNearQuery.inOrder) return false;
142     if (slop != spanNearQuery.slop) return false;
143     if (!clauses.equals(spanNearQuery.clauses)) return false;
144
145     return getBoost() == spanNearQuery.getBoost();
146   }
147
148   public int hashCode() {
149     int result;
150     result = clauses.hashCode();
151     // Mix bits before folding in things like boost, since it could cancel the
152
// last element of clauses. This particular mix also serves to
153
// differentiate SpanNearQuery hashcodes from others.
154
result ^= (result << 14) | (result >>> 19); // reversible
155
result += Float.floatToRawIntBits(getBoost());
156     result += slop;
157     result ^= (inOrder ? 0x99AFD3BD : 0);
158     return result;
159   }
160 }
161
Popular Tags