1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.Serializable;
22 import org.apache.lucene.analysis.TokenStream;
23 import org.apache.lucene.util.ArrayUtil;
26 * A Payload is metadata that can be stored together with each occurrence
27 * of a term. This metadata is stored inline in the posting list of the
30 * To store payloads in the index a {@link TokenStream} has to be used that
31 * produces payload data.
33 * Use {@link TermPositions#getPayloadLength()} and {@link TermPositions#getPayload(byte[], int)}
34 * to retrieve the payloads from the index.<br>
37 public class Payload implements Serializable, Cloneable {
38 /** the byte array containing the payload data */
39 protected byte[] data;
41 /** the offset within the byte array */
44 /** the length of the payload data */
47 /** Creates an empty payload and does not allocate a byte array. */
53 * Creates a new payload with the the given array as data.
54 * A reference to the passed-in array is held, i. e. no
57 * @param data the data of this payload
59 public Payload(byte[] data) {
60 this(data, 0, data.length);
64 * Creates a new payload with the the given array as data.
65 * A reference to the passed-in array is held, i. e. no
68 * @param data the data of this payload
69 * @param offset the offset in the data byte array
70 * @param length the length of the data
72 public Payload(byte[] data, int offset, int length) {
73 if (offset < 0 || offset + length > data.length) {
74 throw new IllegalArgumentException();
82 * Sets this payloads data.
83 * A reference to the passed-in array is held, i. e. no
86 public void setData(byte[] data) {
87 setData(data, 0, data.length);
91 * Sets this payloads data.
92 * A reference to the passed-in array is held, i. e. no
95 public void setData(byte[] data, int offset, int length) {
102 * Returns a reference to the underlying byte array
103 * that holds this payloads data.
105 public byte[] getData() {
110 * Returns the offset in the underlying byte array
112 public int getOffset() {
117 * Returns the length of the payload data.
119 public int length() {
124 * Returns the byte at the given index.
126 public byte byteAt(int index) {
127 if (0 <= index && index < this.length) {
128 return this.data[this.offset + index];
130 throw new ArrayIndexOutOfBoundsException(index);
134 * Allocates a new byte array, copies the payload data into it and returns it.
136 public byte[] toByteArray() {
137 byte[] retArray = new byte[this.length];
138 System.arraycopy(this.data, this.offset, retArray, 0, this.length);
143 * Copies the payload data to a byte array.
145 * @param target the target byte array
146 * @param targetOffset the offset in the target byte array
148 public void copyTo(byte[] target, int targetOffset) {
149 if (this.length > target.length + targetOffset) {
150 throw new ArrayIndexOutOfBoundsException();
152 System.arraycopy(this.data, this.offset, target, targetOffset, this.length);
156 * Clones this payload by creating a copy of the underlying
160 public Object clone() {
162 // Start with a shallow copy of data
163 Payload clone = (Payload) super.clone();
164 // Only copy the part of data that belongs to this Payload
165 if (offset == 0 && length == data.length) {
166 // It is the whole thing, so just clone it.
167 clone.data = data.clone();
171 clone.data = this.toByteArray();
175 } catch (CloneNotSupportedException e) {
176 throw new RuntimeException(e); // shouldn't happen
181 public boolean equals(Object obj) {
184 if (obj instanceof Payload) {
185 Payload other = (Payload) obj;
186 if (length == other.length) {
187 for(int i=0;i<length;i++)
188 if (data[offset+i] != other.data[other.offset+i])
198 public int hashCode() {
199 return ArrayUtil.hashCode(data, offset, offset+length);