1 package org.apache.lucene.index; 2 3 18 19 import java.io.IOException ; 20 import org.apache.lucene.store.IndexInput; 21 22 final class TermBuffer implements Cloneable { 23 private static final char[] NO_CHARS = new char[0]; 24 25 private String field; 26 private char[] text = NO_CHARS; 27 private int textLength; 28 private Term term; 30 public final int compareTo(TermBuffer other) { 31 if (field == other.field) 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 { 61 this.term = null; 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 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) return null; 101 102 if (term == null) 103 term = new Term(field, new String (text, 0, textLength), false); 104 105 return term; 106 } 107 108 protected Object clone() { 109 TermBuffer clone = null; 110 try { 111 clone = (TermBuffer)super.clone(); 112 } catch (CloneNotSupportedException 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 |