001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import org.apache.lucene.store.IndexInput;
022:
023: final class TermBuffer implements Cloneable {
024: private static final char[] NO_CHARS = new char[0];
025:
026: private String field;
027: private char[] text = NO_CHARS;
028: private int textLength;
029: private Term term; // cached
030:
031: public final int compareTo(TermBuffer other) {
032: if (field == other.field) // fields are interned
033: return compareChars(text, textLength, other.text,
034: other.textLength);
035: else
036: return field.compareTo(other.field);
037: }
038:
039: private static final int compareChars(char[] v1, int len1,
040: char[] v2, int len2) {
041: int end = Math.min(len1, len2);
042: for (int k = 0; k < end; k++) {
043: char c1 = v1[k];
044: char c2 = v2[k];
045: if (c1 != c2) {
046: return c1 - c2;
047: }
048: }
049: return len1 - len2;
050: }
051:
052: private final void setTextLength(int newLength) {
053: if (text.length < newLength) {
054: char[] newText = new char[newLength];
055: System.arraycopy(text, 0, newText, 0, textLength);
056: text = newText;
057: }
058: textLength = newLength;
059: }
060:
061: public final void read(IndexInput input, FieldInfos fieldInfos)
062: throws IOException {
063: this .term = null; // invalidate cache
064: int start = input.readVInt();
065: int length = input.readVInt();
066: int totalLength = start + length;
067: setTextLength(totalLength);
068: input.readChars(this .text, start, length);
069: this .field = fieldInfos.fieldName(input.readVInt());
070: }
071:
072: public final void set(Term term) {
073: if (term == null) {
074: reset();
075: return;
076: }
077:
078: // copy text into the buffer
079: setTextLength(term.text().length());
080: term.text().getChars(0, term.text().length(), text, 0);
081:
082: this .field = term.field();
083: this .term = term;
084: }
085:
086: public final void set(TermBuffer other) {
087: setTextLength(other.textLength);
088: System.arraycopy(other.text, 0, text, 0, textLength);
089:
090: this .field = other.field;
091: this .term = other.term;
092: }
093:
094: public void reset() {
095: this .field = null;
096: this .textLength = 0;
097: this .term = null;
098: }
099:
100: public Term toTerm() {
101: if (field == null) // unset
102: return null;
103:
104: if (term == null)
105: term = new Term(field, new String(text, 0, textLength),
106: false);
107:
108: return term;
109: }
110:
111: protected Object clone() {
112: TermBuffer clone = null;
113: try {
114: clone = (TermBuffer) super .clone();
115: } catch (CloneNotSupportedException e) {
116: }
117:
118: clone.text = new char[text.length];
119: System.arraycopy(text, 0, clone.text, 0, textLength);
120:
121: return clone;
122: }
123: }
|