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.
17 using InputStream
= Lucene
.Net
.Store
.InputStream
;
18 namespace Lucene
.Net
.Index
21 public class TermBuffer
:System
.ICloneable
{
22 private static char[] NO_CHARS
= new char[0];
25 private char[] text
= NO_CHARS
;
26 private int textLength
;
27 private Term term
; // cached
29 public int CompareTo(TermBuffer other
)
31 if (field
== other
.field
) // fields are interned
32 return CompareChars(text
, textLength
, other
.text
, other
.textLength
);
34 return field
.CompareTo(other
.field
);
37 private static int CompareChars(char[] v1
, int len1
, char[] v2
, int len2
)
39 int end
= Math
.Min(len1
, len2
);
40 for (int k
= 0; k
< end
; k
++) {
50 private void SetTextLength(int newLength
)
52 if (text
.Length
< newLength
) {
53 char[] newText
= new char[newLength
];
54 System
.Array
.Copy(text
, 0, newText
, 0, textLength
);
57 textLength
= newLength
;
60 public void Read(InputStream input
, FieldInfos fieldInfos
)
62 this.term
= null; // invalidate cache
63 int start
= input
.ReadVInt();
64 int length
= input
.ReadVInt();
65 int totalLength
= start
+ length
;
66 SetTextLength(totalLength
);
67 input
.ReadChars(this.text
, start
, length
);
68 this.field
= fieldInfos
.FieldName(input
.ReadVInt());
71 public void Set(Term term
)
78 // copy text into the buffer
79 SetTextLength(term
.Text().Length
);
80 text
= term
.Text().ToCharArray(0, term
.Text().Length
);
82 this.field
= term
.Field();
86 public void Set(TermBuffer other
)
88 SetTextLength(other
.textLength
);
89 System
.Array
.Copy(other
.text
, 0, text
, 0, textLength
);
91 this.field
= other
.field
;
92 this.term
= other
.term
;
104 if (field
== null) // unset
108 term
= new Term(field
, new String(text
, 0, textLength
), false);
113 public System
.Object
Clone()
115 TermBuffer clone
= null;
118 clone
= (TermBuffer
) base.MemberwiseClone();
120 catch (System
.Exception
)
124 clone
.text
= new char[text
.Length
];
125 System
.Array
.Copy(text
, 0, clone
.text
, 0, textLength
);