pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / SpanPayloadCheckQuery.java
1 package org.apache.lucene.search.spans;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  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 org.apache.lucene.util.ToStringUtils;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26
27 /**
28  *   Only return those matches that have a specific payload at
29  *  the given position.
30  *<p/>
31  * Do not use this with an SpanQuery that contains a {@link org.apache.lucene.search.spans.SpanNearQuery}.  Instead, use
32  * {@link SpanNearPayloadCheckQuery} since it properly handles the fact that payloads
33  * aren't ordered by {@link org.apache.lucene.search.spans.SpanNearQuery}.
34  *
35  **/
36 public class SpanPayloadCheckQuery extends SpanPositionCheckQuery{
37   protected final Collection<byte[]> payloadToMatch;
38
39   /**
40    *
41    * @param match The underlying {@link org.apache.lucene.search.spans.SpanQuery} to check
42    * @param payloadToMatch The {@link java.util.Collection} of payloads to match
43    */
44   public SpanPayloadCheckQuery(SpanQuery match, Collection<byte[]> payloadToMatch) {
45     super(match);
46     if (match instanceof SpanNearQuery){
47       throw new IllegalArgumentException("SpanNearQuery not allowed");
48     }
49     this.payloadToMatch = payloadToMatch;
50   }
51
52   @Override
53   protected AcceptStatus acceptPosition(Spans spans) throws IOException {
54     boolean result = spans.isPayloadAvailable();
55     if (result == true){
56       Collection<byte[]> candidate = spans.getPayload();
57       if (candidate.size() == payloadToMatch.size()){
58         //TODO: check the byte arrays are the same
59         Iterator<byte[]> toMatchIter = payloadToMatch.iterator();
60         //check each of the byte arrays, in order
61         //hmm, can't rely on order here
62         for (byte[] candBytes : candidate) {
63           //if one is a mismatch, then return false
64           if (Arrays.equals(candBytes, toMatchIter.next()) == false){
65             return AcceptStatus.NO;
66           }
67         }
68         //we've verified all the bytes
69         return AcceptStatus.YES;
70       } else {
71         return AcceptStatus.NO;
72       }
73     }
74     return AcceptStatus.YES;
75   } 
76
77   @Override
78   public String toString(String field) {
79     StringBuilder buffer = new StringBuilder();
80     buffer.append("spanPayCheck(");
81     buffer.append(match.toString(field));
82     buffer.append(", payloadRef: ");
83     for (byte[] bytes : payloadToMatch) {
84       ToStringUtils.byteArray(buffer, bytes);
85       buffer.append(';');
86     }
87     buffer.append(")");
88     buffer.append(ToStringUtils.boost(getBoost()));
89     return buffer.toString();
90   }
91
92   @Override
93   public Object clone() {
94     SpanPayloadCheckQuery result = new SpanPayloadCheckQuery((SpanQuery) match.clone(), payloadToMatch);
95     result.setBoost(getBoost());
96     return result;
97   }
98
99   @Override
100   public boolean equals(Object o) {
101     if (this == o) return true;
102     if (!(o instanceof SpanPayloadCheckQuery)) return false;
103
104     SpanPayloadCheckQuery other = (SpanPayloadCheckQuery)o;
105     return this.payloadToMatch.equals(other.payloadToMatch)
106          && this.match.equals(other.match)
107          && this.getBoost() == other.getBoost();
108   }
109
110   @Override
111   public int hashCode() {
112     int h = match.hashCode();
113     h ^= (h << 8) | (h >>> 25);  // reversible
114     //TODO: is this right?
115     h ^= payloadToMatch.hashCode();
116     h ^= Float.floatToRawIntBits(getBoost()) ;
117     return h;
118   }
119 }