KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > CharSlice


1 package net.sf.saxon.tinytree;
2
3 import java.io.Writer JavaDoc;
4 import java.io.Serializable JavaDoc;
5
6 /**
7  * This is an implementation of the JDK 1.4 CharSequence interface: it implements
8  * a CharSequence as a view of an array. The implementation relies on the array
9  * being immutable: as a minimum, the caller is required to ensure that the array
10  * contents will not change so long as the CharSlice remains in existence.
11  *
12  * This class should be more efficient than String because it avoids copying the
13  * characters unnecessarily.
14  *
15  * The methods in the class don't check their arguments. Incorrect arguments will
16  * generally result in exceptions from lower-level classes.
17  *
18  */

19 public final class CharSlice implements CharSequence JavaDoc, Serializable JavaDoc {
20
21     private char[] array;
22     private int offset;
23     private int count;
24
25     public CharSlice(char[] array) {
26         this.array = array;
27         this.offset = 0;
28         this.count = array.length;
29     }
30
31     public CharSlice(char[] array, int start, int length) {
32         this.array = array;
33         this.offset = start;
34         this.count = length;
35     }
36
37     /**
38      * Returns the length of this character sequence. The length is the number
39      * of 16-bit Unicode characters in the sequence. </p>
40      *
41      * @return the number of characters in this sequence
42      */

43     public int length() {
44         return count;
45     }
46
47     /**
48      * Set the length of this character sequence, without changing the array and start offset
49      * to which it is bound
50      */

51     public void setLength(int length) {
52         count = length;
53     }
54
55     /**
56      * Returns the character at the specified index. An index ranges from zero
57      * to <tt>length() - 1</tt>. The first character of the sequence is at
58      * index zero, the next at index one, and so on, as for array
59      * indexing. </p>
60      *
61      * @param index the index of the character to be returned
62      *
63      * @return the specified character
64      *
65      * @throws java.lang.IndexOutOfBoundsException
66      * if the <tt>index</tt> argument is negative or not less than
67      * <tt>length()</tt>
68      */

69     public char charAt(int index) {
70         return array[offset+index];
71     }
72
73     /**
74      * Returns a new character sequence that is a subsequence of this sequence.
75      * The subsequence starts with the character at the specified index and
76      * ends with the character at index <tt>end - 1</tt>. The length of the
77      * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>
78      * then an empty sequence is returned. </p>
79      *
80      * @param start the start index, inclusive
81      * @param end the end index, exclusive
82      *
83      * @return the specified subsequence
84      *
85      * @throws java.lang.IndexOutOfBoundsException
86      * if <tt>start</tt> or <tt>end</tt> are negative,
87      * if <tt>end</tt> is greater than <tt>length()</tt>,
88      * or if <tt>start</tt> is greater than <tt>end</tt>
89      */

90     public CharSequence JavaDoc subSequence(int start, int end) {
91         return new CharSlice(array, offset+start, end-start);
92     }
93
94     /**
95      * Convert to a string
96      */

97
98     public String JavaDoc toString() {
99         return new String JavaDoc(array, offset, count);
100     }
101
102     /**
103      * Compare equality
104      */

105
106     public boolean equals(Object JavaDoc other) {
107         return toString().equals(other);
108     }
109
110     /**
111      * Generate a hash code
112      */

113
114     public int hashCode() {
115         // Same algorithm as String#hashCode(), but not cached
116
int end = offset+count;
117         int h = 0;
118         for (int i = offset; i < end; i++) {
119             h = 31 * h + array[i];
120         }
121         return h;
122     }
123
124     /**
125      * Get the index of a specific character in the sequence. Returns -1 if not found.
126      * This method mimics {@link String#indexOf}
127      * @param c the character to be found
128      * @return the position of the first occurrence of that character, or -1 if not found.
129      */

130
131     public int indexOf(char c) {
132         int end = offset+count;
133         for (int i = offset; i < end; i++) {
134             if (array[i] == c) {
135                 return i-offset;
136             };
137         }
138         return -1;
139     }
140
141     /**
142      * Returns a new character sequence that is a subsequence of this sequence.
143      * Unlike subSequence, this is guaranteed to return a String.
144      */

145
146     public String JavaDoc substring(int start, int end) {
147         return new String JavaDoc(array, offset+start, end-start);
148     }
149
150     /**
151      * Append the contents to another array at a given offset. The caller is responsible
152      * for ensuring that sufficient space is available.
153      * @param destination the array to which the characters will be copied
154      * @param destOffset the offset in the target array where the copy will start
155      */

156
157     public void copyTo(char[] destination, int destOffset) {
158         System.arraycopy(array, offset, destination, destOffset, count);
159     }
160
161     /**
162      * Write the value to a writer
163      */

164
165     public void write(Writer JavaDoc writer) throws java.io.IOException JavaDoc {
166         writer.write(array, offset, count);
167     }
168
169 }
170
171 //
172
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
173
// you may not use this file except in compliance with the License. You may obtain a copy of the
174
// License at http://www.mozilla.org/MPL/
175
//
176
// Software distributed under the License is distributed on an "AS IS" basis,
177
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
178
// See the License for the specific language governing rights and limitations under the License.
179
//
180
// The Original Code is: all this file.
181
//
182
// The Initial Developer of the Original Code is Michael H. Kay
183
//
184
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
185
//
186
// Contributor(s): none
187
//
188
Popular Tags