1 package org.apache.lucene.search.spans;
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import org.apache.lucene.util.ToStringUtils;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collection;
27 * Only return those matches that have a specific payload at
32 public class SpanNearPayloadCheckQuery extends SpanPositionCheckQuery {
33 protected final Collection<byte[]> payloadToMatch;
36 * @param match The underlying {@link SpanQuery} to check
37 * @param payloadToMatch The {@link java.util.Collection} of payloads to match
39 public SpanNearPayloadCheckQuery(SpanNearQuery match, Collection<byte[]> payloadToMatch) {
41 this.payloadToMatch = payloadToMatch;
45 protected AcceptStatus acceptPosition(Spans spans) throws IOException {
46 boolean result = spans.isPayloadAvailable();
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
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) {
62 if (matches == payloadToMatch.size()){
63 //we've verified all the bytes
64 return AcceptStatus.YES;
66 return AcceptStatus.NO;
69 return AcceptStatus.NO;
72 return AcceptStatus.NO;
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);
86 buffer.append(ToStringUtils.boost(getBoost()));
87 return buffer.toString();
91 public Object clone() {
92 SpanNearPayloadCheckQuery result = new SpanNearPayloadCheckQuery((SpanNearQuery) match.clone(), payloadToMatch);
93 result.setBoost(getBoost());
98 public boolean equals(Object o) {
99 if (this == o) return true;
100 if (!(o instanceof SpanNearPayloadCheckQuery)) return false;
102 SpanNearPayloadCheckQuery other = (SpanNearPayloadCheckQuery) o;
103 return this.payloadToMatch.equals(other.payloadToMatch)
104 && this.match.equals(other.match)
105 && this.getBoost() == other.getBoost();
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());