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 IndexInput
= Lucene
.Net
.Store
.IndexInput
;
18 namespace Lucene
.Net
.Index
21 sealed class TermBuffer
: System
.ICloneable
23 private static readonly char[] NO_CHARS
= new char[0];
25 private System
.String field
;
26 private char[] text
= NO_CHARS
;
27 private int textLength
;
28 private Term term
; // cached
30 public int CompareTo(TermBuffer other
)
32 if ((System
.Object
) field
== (System
.Object
) other
.field
)
33 // fields are interned
34 return CompareChars(text
, textLength
, other
.text
, other
.textLength
);
36 return String
.CompareOrdinal(field
, other
.field
);
39 private static int CompareChars(char[] v1
, int len1
, char[] v2
, int len2
)
41 int end
= System
.Math
.Min(len1
, len2
);
42 for (int k
= 0; k
< end
; k
++)
54 private void SetTextLength(int newLength
)
56 if (text
.Length
< newLength
)
58 char[] newText
= new char[newLength
];
59 Array
.Copy(text
, 0, newText
, 0, textLength
);
62 textLength
= newLength
;
65 public void Read(IndexInput input
, FieldInfos fieldInfos
)
67 this.term
= null; // invalidate cache
68 int start
= input
.ReadVInt();
69 int length
= input
.ReadVInt();
70 int totalLength
= start
+ length
;
71 SetTextLength(totalLength
);
72 input
.ReadChars(this.text
, start
, length
);
73 this.field
= fieldInfos
.FieldName(input
.ReadVInt());
76 public void Set(Term term
)
84 // copy text into the buffer
85 SetTextLength(term
.Text().Length
);
86 text
= term
.Text().ToCharArray();
88 this.field
= term
.Field();
92 public void Set(TermBuffer other
)
94 SetTextLength(other
.textLength
);
95 Array
.Copy(other
.text
, 0, text
, 0, textLength
);
97 this.field
= other
.field
;
98 this.term
= other
.term
;
115 term
= new Term(field
, new System
.String(text
, 0, textLength
), false);
120 public System
.Object
Clone()
122 TermBuffer clone
= null;
125 clone
= (TermBuffer
) base.MemberwiseClone();
127 catch (System
.Exception e
)
131 clone
.text
= new char[text
.Length
];
132 Array
.Copy(text
, 0, clone
.text
, 0, textLength
);