pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / CharsRef.java
1 package org.apache.lucene.util;
2
3 import java.util.Comparator;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /**
23  * Represents char[], as a slice (offset + length) into an existing char[].
24  * The {@link #chars} member should never be null; use
25  * {@link #EMPTY_ARRAY} if necessary.
26  * @lucene.internal
27  */
28 public final class CharsRef implements Comparable<CharsRef>, CharSequence {
29   private static final char[] EMPTY_ARRAY = new char[0];
30   public char[] chars;
31   public int offset;
32   public int length;
33
34   /**
35    * Creates a new {@link CharsRef} initialized an empty array zero-length
36    */
37   public CharsRef() {
38     this(EMPTY_ARRAY, 0, 0);
39   }
40
41   /**
42    * Creates a new {@link CharsRef} initialized with an array of the given
43    * capacity
44    */
45   public CharsRef(int capacity) {
46     chars = new char[capacity];
47   }
48
49   /**
50    * Creates a new {@link CharsRef} initialized with the given array, offset and
51    * length
52    */
53   public CharsRef(char[] chars, int offset, int length) {
54     assert chars != null;
55     assert chars.length >= offset + length;
56     this.chars = chars;
57     this.offset = offset;
58     this.length = length;
59   }
60
61   /**
62    * Creates a new {@link CharsRef} initialized with the given Strings character
63    * array
64    */
65   public CharsRef(String string) {
66     this.chars = string.toCharArray();
67     this.offset = 0;
68     this.length = chars.length;
69   }
70
71   /**
72    * Creates a new {@link CharsRef} and copies the contents of the source into
73    * the new instance.
74    * @see #copy(CharsRef)
75    */
76   public CharsRef(CharsRef other) {
77     copy(other);
78   }
79
80   @Override
81   public Object clone() {
82     return new CharsRef(this);
83   }
84
85   @Override
86   public int hashCode() {
87     final int prime = 31;
88     int result = 0;
89     final int end = offset + length;
90     for (int i = offset; i < end; i++) {
91       result = prime * result + chars[i];
92     }
93     return result;
94   }
95
96   @Override
97   public boolean equals(Object other) {
98     if (this == other) {
99       return true;
100     }
101
102     if (other instanceof CharsRef) {
103       return charsEquals((CharsRef) other);
104     }
105
106     if (other instanceof CharSequence) {
107       final CharSequence seq = (CharSequence) other;
108       if (length == seq.length()) {
109         int n = length;
110         int i = offset;
111         int j = 0;
112         while (n-- != 0) {
113           if (chars[i++] != seq.charAt(j++))
114             return false;
115         }
116         return true;
117       }
118     }
119     return false;
120   }
121
122   public boolean charsEquals(CharsRef other) {
123     if (length == other.length) {
124       int otherUpto = other.offset;
125       final char[] otherChars = other.chars;
126       final int end = offset + length;
127       for (int upto = offset; upto < end; upto++, otherUpto++) {
128         if (chars[upto] != otherChars[otherUpto]) {
129           return false;
130         }
131       }
132       return true;
133     } else {
134       return false;
135     }
136   }
137
138   /** Signed int order comparison */
139   public int compareTo(CharsRef other) {
140     if (this == other)
141       return 0;
142
143     final char[] aChars = this.chars;
144     int aUpto = this.offset;
145     final char[] bChars = other.chars;
146     int bUpto = other.offset;
147
148     final int aStop = aUpto + Math.min(this.length, other.length);
149
150     while (aUpto < aStop) {
151       int aInt = aChars[aUpto++];
152       int bInt = bChars[bUpto++];
153       if (aInt > bInt) {
154         return 1;
155       } else if (aInt < bInt) {
156         return -1;
157       }
158     }
159
160     // One is a prefix of the other, or, they are equal:
161     return this.length - other.length;
162   }
163   
164   /**
165    * Copies the given {@link CharsRef} referenced content into this instance
166    * starting at offset 0.
167    * 
168    * @param other
169    *          the {@link CharsRef} to copy
170    */
171   public void copy(CharsRef other) {
172     if (chars == null) {
173       chars = new char[other.length];
174     } else {
175       chars = ArrayUtil.grow(chars, other.length);
176     }
177     System.arraycopy(other.chars, other.offset, chars, 0, other.length);
178     length = other.length;
179     offset = 0;
180   }
181
182   public void grow(int newLength) {
183     if (chars.length < newLength) {
184       chars = ArrayUtil.grow(chars, newLength);
185     }
186   }
187
188   /**
189    * Copies the given array into this CharsRef starting at offset 0
190    */
191   public void copy(char[] otherChars, int otherOffset, int otherLength) {
192     grow(otherLength);
193     System.arraycopy(otherChars, otherOffset, this.chars, 0,
194         otherLength);
195     this.offset = 0;
196     this.length = otherLength;
197   }
198
199   /**
200    * Appends the given array to this CharsRef
201    */
202   public void append(char[] otherChars, int otherOffset, int otherLength) {
203     final int newLength = length + otherLength;
204     grow(this.offset + newLength);
205     System.arraycopy(otherChars, otherOffset, this.chars, this.offset+length,
206         otherLength);
207     this.length += otherLength;
208   }
209
210   @Override
211   public String toString() {
212     return new String(chars, offset, length);
213   }
214
215   public int length() {
216     return length;
217   }
218
219   public char charAt(int index) {
220     return chars[offset + index];
221   }
222
223   public CharSequence subSequence(int start, int end) {
224     return new CharsRef(chars, offset + start, offset + end - 1);
225   }
226   
227   private final static Comparator<CharsRef> utf16SortedAsUTF8SortOrder = new UTF16SortedAsUTF8Comparator();
228   
229   public static Comparator<CharsRef> getUTF16SortedAsUTF8Comparator() {
230     return utf16SortedAsUTF8SortOrder;
231   }
232   
233   private static class UTF16SortedAsUTF8Comparator implements Comparator<CharsRef> {
234     // Only singleton
235     private UTF16SortedAsUTF8Comparator() {};
236
237     public int compare(CharsRef a, CharsRef b) {
238       if (a == b)
239         return 0;
240
241       final char[] aChars = a.chars;
242       int aUpto = a.offset;
243       final char[] bChars = b.chars;
244       int bUpto = b.offset;
245
246       final int aStop = aUpto + Math.min(a.length, b.length);
247
248       while (aUpto < aStop) {
249         char aChar = aChars[aUpto++];
250         char bChar = bChars[bUpto++];
251         if (aChar != bChar) {
252           // http://icu-project.org/docs/papers/utf16_code_point_order.html
253           
254           /* aChar != bChar, fix up each one if they're both in or above the surrogate range, then compare them */
255           if (aChar >= 0xd800 && bChar >= 0xd800) {
256             if (aChar >= 0xe000) {
257               aChar -= 0x800;
258             } else {
259               aChar += 0x2000;
260             }
261             
262             if (bChar >= 0xe000) {
263               bChar -= 0x800;
264             } else {
265               bChar += 0x2000;
266             }
267           }
268           
269           /* now aChar and bChar are in code point order */
270           return (int)aChar - (int)bChar; /* int must be 32 bits wide */
271         }
272       }
273
274       // One is a prefix of the other, or, they are equal:
275       return a.length - b.length;
276     }
277   }
278 }