KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TermBuffer


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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 java.io.IOException JavaDoc;
20 import org.apache.lucene.store.IndexInput;
21
22 final class TermBuffer implements Cloneable JavaDoc {
23   private static final char[] NO_CHARS = new char[0];
24
25   private String JavaDoc field;
26   private char[] text = NO_CHARS;
27   private int textLength;
28   private Term term; // cached
29

30   public final int compareTo(TermBuffer other) {
31     if (field == other.field) // fields are interned
32
return compareChars(text, textLength, other.text, other.textLength);
33     else
34       return field.compareTo(other.field);
35   }
36
37   private static final int compareChars(char[] v1, int len1,
38                                         char[] v2, int len2) {
39     int end = Math.min(len1, len2);
40     for (int k = 0; k < end; k++) {
41       char c1 = v1[k];
42       char c2 = v2[k];
43       if (c1 != c2) {
44         return c1 - c2;
45       }
46     }
47     return len1 - len2;
48   }
49
50   private final void setTextLength(int newLength) {
51     if (text.length < newLength) {
52       char[] newText = new char[newLength];
53       System.arraycopy(text, 0, newText, 0, textLength);
54       text = newText;
55     }
56     textLength = newLength;
57   }
58
59   public final void read(IndexInput input, FieldInfos fieldInfos)
60     throws IOException JavaDoc {
61     this.term = null; // invalidate cache
62
int start = input.readVInt();
63     int length = input.readVInt();
64     int totalLength = start + length;
65     setTextLength(totalLength);
66     input.readChars(this.text, start, length);
67     this.field = fieldInfos.fieldName(input.readVInt());
68   }
69
70   public final void set(Term term) {
71     if (term == null) {
72       reset();
73       return;
74     }
75
76     // copy text into the buffer
77
setTextLength(term.text().length());
78     term.text().getChars(0, term.text().length(), text, 0);
79
80     this.field = term.field();
81     this.term = term;
82   }
83
84   public final void set(TermBuffer other) {
85     setTextLength(other.textLength);
86     System.arraycopy(other.text, 0, text, 0, textLength);
87
88     this.field = other.field;
89     this.term = other.term;
90   }
91
92   public void reset() {
93     this.field = null;
94     this.textLength = 0;
95     this.term = null;
96   }
97
98   public Term toTerm() {
99     if (field == null) // unset
100
return null;
101
102     if (term == null)
103       term = new Term(field, new String JavaDoc(text, 0, textLength), false);
104
105     return term;
106   }
107
108   protected Object JavaDoc clone() {
109     TermBuffer clone = null;
110     try {
111       clone = (TermBuffer)super.clone();
112     } catch (CloneNotSupportedException JavaDoc e) {}
113
114     clone.text = new char[text.length];
115     System.arraycopy(text, 0, clone.text, 0, textLength);
116
117     return clone;
118   }
119 }
120
Popular Tags