cvsimport
[beagle.git] / beagled / Lucene.Net / Index / TermBuffer.cs
blobbf6989fc913b353b955492b243d569b58699ee61
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexInput = Lucene.Net.Store.IndexInput;
20 namespace Lucene.Net.Index
23 sealed class TermBuffer : System.ICloneable
25 private static readonly char[] NO_CHARS = new char[0];
27 private System.String field;
28 private char[] text = NO_CHARS;
29 private int textLength;
30 private Term term; // cached
32 public int CompareTo(TermBuffer other)
34 if ((System.Object) field == (System.Object) other.field)
35 // fields are interned
36 return CompareChars(text, textLength, other.text, other.textLength);
37 else
38 return String.CompareOrdinal(field, other.field);
41 private static int CompareChars(char[] v1, int len1, char[] v2, int len2)
43 int end = System.Math.Min(len1, len2);
44 for (int k = 0; k < end; k++)
46 char c1 = v1[k];
47 char c2 = v2[k];
48 if (c1 != c2)
50 return c1 - c2;
53 return len1 - len2;
56 private void SetTextLength(int newLength)
58 if (text.Length < newLength)
60 char[] newText = new char[newLength];
61 Array.Copy(text, 0, newText, 0, textLength);
62 text = newText;
64 textLength = newLength;
67 public void Read(IndexInput input, FieldInfos fieldInfos)
69 this.term = null; // invalidate cache
70 int start = input.ReadVInt();
71 int length = input.ReadVInt();
72 int totalLength = start + length;
73 SetTextLength(totalLength);
74 input.ReadChars(this.text, start, length);
75 this.field = fieldInfos.FieldName(input.ReadVInt());
78 public void Set(Term term)
80 if (term == null)
82 Reset();
83 return ;
86 // copy text into the buffer
87 SetTextLength(term.Text().Length);
88 text = term.Text().ToCharArray();
90 this.field = term.Field();
91 this.term = term;
94 public void Set(TermBuffer other)
96 SetTextLength(other.textLength);
97 Array.Copy(other.text, 0, text, 0, textLength);
99 this.field = other.field;
100 this.term = other.term;
103 public void Reset()
105 this.field = null;
106 this.textLength = 0;
107 this.term = null;
110 public Term ToTerm()
112 if (field == null)
113 // unset
114 return null;
116 if (term == null)
117 term = new Term(field, new System.String(text, 0, textLength), false);
119 return term;
122 public System.Object Clone()
124 TermBuffer clone = null;
127 clone = (TermBuffer) base.MemberwiseClone();
129 catch (System.Exception)
133 clone.text = new char[text.Length];
134 Array.Copy(text, 0, clone.text, 0, textLength);
136 return clone;