pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / SpanNearPayloadCheckQuery.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
25
26 /**
27  * Only return those matches that have a specific payload at
28  * the given position.
29  * <p/>
30  * 
31  */
32 public class SpanNearPayloadCheckQuery extends SpanPositionCheckQuery {
33   protected final Collection<byte[]> payloadToMatch;
34
35   /**
36    * @param match          The underlying {@link SpanQuery} to check
37    * @param payloadToMatch The {@link java.util.Collection} of payloads to match
38    */
39   public SpanNearPayloadCheckQuery(SpanNearQuery match, Collection<byte[]> payloadToMatch) {
40     super(match);
41     this.payloadToMatch = payloadToMatch;
42   }
43
44   @Override
45   protected AcceptStatus acceptPosition(Spans spans) throws IOException {
46     boolean result = spans.isPayloadAvailable();
47     if (result == true) {
48       Collection<byte[]> candidate = spans.getPayload();
49       if (candidate.size() == payloadToMatch.size()) {
50         //TODO: check the byte arrays are the same
51         //hmm, can't rely on order here
52         int matches = 0;
53         for (byte[] candBytes : candidate) {
54           //Unfortunately, we can't rely on order, so we need to compare all
55           for (byte[] payBytes : payloadToMatch) {
56             if (Arrays.equals(candBytes, payBytes) == true) {
57               matches++;
58               break;
59             }
60           }
61         }
62         if (matches == payloadToMatch.size()){
63           //we've verified all the bytes
64           return AcceptStatus.YES;
65         } else {
66           return AcceptStatus.NO;
67         }
68       } else {
69         return AcceptStatus.NO;
70       }
71     }
72     return AcceptStatus.NO;
73   }
74
75   @Override
76   public String toString(String field) {
77     StringBuilder buffer = new StringBuilder();
78     buffer.append("spanPayCheck(");
79     buffer.append(match.toString(field));
80     buffer.append(", payloadRef: ");
81     for (byte[] bytes : payloadToMatch) {
82       ToStringUtils.byteArray(buffer, bytes);
83       buffer.append(';');
84     }
85     buffer.append(")");
86     buffer.append(ToStringUtils.boost(getBoost()));
87     return buffer.toString();
88   }
89
90   @Override
91   public Object clone() {
92     SpanNearPayloadCheckQuery result = new SpanNearPayloadCheckQuery((SpanNearQuery) match.clone(), payloadToMatch);
93     result.setBoost(getBoost());
94     return result;
95   }
96
97   @Override
98   public boolean equals(Object o) {
99     if (this == o) return true;
100     if (!(o instanceof SpanNearPayloadCheckQuery)) return false;
101
102     SpanNearPayloadCheckQuery other = (SpanNearPayloadCheckQuery) o;
103     return this.payloadToMatch.equals(other.payloadToMatch)
104             && this.match.equals(other.match)
105             && this.getBoost() == other.getBoost();
106   }
107
108   @Override
109   public int hashCode() {
110     int h = match.hashCode();
111     h ^= (h << 8) | (h >>> 25);  // reversible
112     //TODO: is this right?
113     h ^= payloadToMatch.hashCode();
114     h ^= Float.floatToRawIntBits(getBoost());
115     return h;
116   }
117 }