add --shared
[pylucene.git] / lucene-java-3.4.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     this.offset = 0;
193     append(otherChars, otherOffset, otherLength);
194   }
195
196   /**
197    * Appends the given array to this CharsRef starting at the current offset
198    */
199   public void append(char[] otherChars, int otherOffset, int otherLength) {
200     grow(this.offset + otherLength);
201     System.arraycopy(otherChars, otherOffset, this.chars, this.offset,
202         otherLength);
203     this.length = otherLength;
204   }
205
206   @Override
207   public String toString() {
208     return new String(chars, offset, length);
209   }
210
211   public int length() {
212     return length;
213   }
214
215   public char charAt(int index) {
216     return chars[offset + index];
217   }
218
219   public CharSequence subSequence(int start, int end) {
220     return new CharsRef(chars, offset + start, offset + end - 1);
221   }
222   
223   private final static Comparator<CharsRef> utf16SortedAsUTF8SortOrder = new UTF16SortedAsUTF8Comparator();
224   
225   public static Comparator<CharsRef> getUTF16SortedAsUTF8Comparator() {
226     return utf16SortedAsUTF8SortOrder;
227   }
228   
229   private static class UTF16SortedAsUTF8Comparator implements Comparator<CharsRef> {
230     // Only singleton
231     private UTF16SortedAsUTF8Comparator() {};
232
233     public int compare(CharsRef a, CharsRef b) {
234       if (a == b)
235         return 0;
236
237       final char[] aChars = a.chars;
238       int aUpto = a.offset;
239       final char[] bChars = b.chars;
240       int bUpto = b.offset;
241
242       final int aStop = aUpto + Math.min(a.length, b.length);
243
244       while (aUpto < aStop) {
245         char aChar = aChars[aUpto++];
246         char bChar = bChars[bUpto++];
247         if (aChar != bChar) {
248           // http://icu-project.org/docs/papers/utf16_code_point_order.html
249           
250           /* aChar != bChar, fix up each one if they're both in or above the surrogate range, then compare them */
251           if (aChar >= 0xd800 && bChar >= 0xd800) {
252             if (aChar >= 0xe000) {
253               aChar -= 0x800;
254             } else {
255               aChar += 0x2000;
256             }
257             
258             if (bChar >= 0xe000) {
259               bChar -= 0x800;
260             } else {
261               bChar += 0x2000;
262             }
263           }
264           
265           /* now aChar and bChar are in code point order */
266           return (int)aChar - (int)bChar; /* int must be 32 bits wide */
267         }
268       }
269
270       // One is a prefix of the other, or, they are equal:
271       return a.length - b.length;
272     }
273   }
274 }