2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
);
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
++)
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
);
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
)
86 // copy text into the buffer
87 SetTextLength(term
.Text().Length
);
88 text
= term
.Text().ToCharArray();
90 this.field
= term
.Field();
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
;
117 term
= new Term(field
, new System
.String(text
, 0, textLength
), false);
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
);