KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > pull > CharSequenceImpl


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml.pull;
10
11 import j2me.lang.CharSequence;
12 import j2me.lang.Comparable;
13 import javolution.realtime.ObjectFactory;
14 import javolution.util.FastComparator;
15
16 /**
17  * This class represents the <code>CharSequence</code> generated while
18  * parsing XML document; parsers may reuse instances of this class
19  * to avoid dynamic memory allocation.
20  *
21  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
22  * @version 3.2, March 21, 2005
23  */

24 final class CharSequenceImpl implements CharSequence, Comparable {
25
26     /**
27      * Holds the associated factory.
28      */

29     static final ObjectFactory FACTORY = new ObjectFactory() {
30         protected Object create() {
31             return new CharSequenceImpl();
32         }
33     };
34
35     /**
36      * Holds an empty character sequence.
37      */

38     static final CharSequenceImpl EMPTY = new CharSequenceImpl("");
39
40     /**
41      * Holds the character data.
42      */

43     char[] data;
44
45     /**
46      * Holds the index of the first character.
47      */

48     int offset;
49
50     /**
51      * Holds the length of char sequence.
52      */

53     int length;
54
55     /**
56      * Default constructor.
57      */

58     CharSequenceImpl() {
59     }
60
61     /**
62      * Creates a character sequence from the specified String.
63      *
64      * @param string the String.
65      */

66     CharSequenceImpl(String string) {
67         data = string.toCharArray();
68         offset = 0;
69         length = string.length();
70     }
71
72     /**
73      * Returns the length of this character sequence.
74      *
75      * @return the number of characters (16-bits Unicode) composing this
76      * character sequence.
77      */

78     public int length() {
79         return length;
80     }
81
82     /**
83      * Returns the character at the specified index.
84      *
85      * @param index the index of the character starting at <code>0</code>.
86      * @return the character at the specified index of this character sequence.
87      * @throws IndexOutOfBoundsException if <code>((index < 0) ||
88      * (index >= length))</code>
89      */

90     public char charAt(int index) {
91         if ((index < 0) || (index >= length))
92             throw new IndexOutOfBoundsException("index: " + index);
93         return data[offset + index];
94     }
95
96     /**
97      * Returns a new character sequence that is a subsequence of this sequence.
98      *
99      * @param start the index of the first character inclusive.
100      * @param end the index of the last character exclusive.
101      * @return the character sequence starting at the specified
102      * <code>start</code> position and ending just before the specified
103      * <code>end</code> position.
104      * @throws IndexOutOfBoundsException if <code>(start < 0) || (end < 0) ||
105      * (start > end) || (end > this.length())</code>
106      */

107     public CharSequence subSequence(int start, int end) {
108         if ((start < 0) || (end < 0) ||
109                  (start > end) || (end > this.length()))
110             throw new IndexOutOfBoundsException();
111         CharSequenceImpl chars = (CharSequenceImpl) FACTORY.object();
112         chars.data = data;
113         chars.offset = offset + start;
114         chars.length = end - start;
115         return chars;
116     }
117
118     /**
119      * Returns the <code>String<code> corresponding to this character
120      * sequence. The <code>String</code> returned is always allocated on the
121      * heap and can safely be referenced elsewhere.
122      *
123      * @return the <code>java.lang.String</code> for this character sequence.
124      */

125     public String toString() {
126         return new String(data, offset, length);
127     }
128
129     /**
130      * Returns the hash code for this {@link CharSequenceImpl}.
131      *
132      * <p> Note: Returns the same hashCode as <code>java.lang.String</code>
133      * (consistent with {@link #equals})</p>
134      *
135      * @return the hash code value.
136      */

137     public int hashCode() {
138         int h = 0;
139         for (int i = 0, j = offset; i < length; i++) {
140             h = 31 * h + data[j++];
141         }
142         return h;
143     }
144
145     /**
146      * Compares this character sequence against the specified object
147      * (<code>String</code> or <code>CharSequence</code>).
148      *
149      * @param that the object to compare with.
150      * @return <code>true</code> if both objects represent the same sequence;
151      * <code>false</code> otherwise.
152      */

153     public boolean equals(Object that) {
154         if (that instanceof CharSequenceImpl) {
155             return equals((CharSequenceImpl) that);
156         } else if (that instanceof String) { // J2ME: String not a CharSequence.
157
return equals((String) that);
158         } else if (that instanceof CharSequence) {
159             return equals((CharSequence) that);
160         } else {
161             return false;
162         }
163     }
164
165     /**
166      * Compares this character sequence against the specified
167      * {@link CharSequenceImpl}.
168      *
169      * @param that the character sequence to compare with.
170      * @return <code>true</code> if both objects represent the same sequence;
171      * <code>false</code> otherwise.
172      */

173     public boolean equals(CharSequenceImpl that) {
174         if (that == null)
175             return false;
176         if (this.length != that.length)
177             return false;
178         final char[] thatData = that.data;
179         final int end = offset + length;
180         for (int i = offset, j = that.offset; i < end;) {
181             if (data[i++] != thatData[j++])
182                 return false;
183         }
184         return true;
185     }
186
187     /**
188      * Compares this character sequence against the specified String.
189      *
190      * @param chars the character sequence to compare with.
191      * @return <code>true</code> if both objects represent the same sequence;
192      * <code>false</code> otherwise.
193      */

194     public boolean equals(String str) {
195         if (str == null)
196             return false;
197         if (this.length != str.length())
198             return false;
199         for (int i = 0, j = offset; i < length;) {
200             if (data[j++] != str.charAt(i++))
201                 return false;
202
203         }
204         return true;
205     }
206
207     /**
208      * Compares this character sequence against the specified character
209      * sequence.
210      *
211      * @param chars the character sequence to compare with.
212      * @return <code>true</code> if both objects represent the same sequence;
213      * <code>false</code> otherwise.
214      */

215     public boolean equals(CharSequence chars) {
216         if (chars == null)
217             return false;
218         if (this.length != chars.length())
219             return false;
220         for (int i = 0, j = offset; i < length;) {
221             if (data[j++] != chars.charAt(i++))
222                 return false;
223
224         }
225         return true;
226     }
227
228     /**
229      * Compares this {@link CharSequenceImpl} with the specified character
230      * sequence lexicographically.
231      *
232      * @param seq the character sequence to be compared.
233      * @return <code>{@link FastComparator#LEXICAL}.compare(this, seq)</code>
234      * @throws ClassCastException if the specifed object is not a
235      * <code>CharSequence</code>.
236      */

237     public int compareTo(Object seq) {
238         return FastComparator.LEXICAL.compare(this, seq);
239     }
240 }
Popular Tags