KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > re > CharBuffer


1 /*
2  * CharBuffer.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser.re;
23
24 /**
25  * A character buffer. This class provides an API identical to
26  * StringBuffer, with the exception that none of the methods in this
27  * class are synchronized.
28  *
29  * @author Per Cederberg, <per at percederberg dot net>
30  * @version 1.5
31  *
32  * @deprecated The CharBuffer class has been deprecated in favor
33  * of LookAheadReader as of version 1.5.
34  */

35 public class CharBuffer {
36
37     /**
38      * The character buffer length.
39      */

40     private int length = 0;
41
42     /**
43      * The character buffer.
44      */

45     private char[] contents = null;
46
47     /**
48      * Creates a new character buffer.
49      */

50     public CharBuffer() {
51         this(16);
52     }
53
54     /**
55      * Creates a new character buffer with the specified initial size.
56      *
57      * @param initialSize the initial size of the buffer
58      */

59     public CharBuffer(int initialSize) {
60         contents = new char[initialSize];
61     }
62
63     /**
64      * Creates a new character buffer from the specified string.
65      *
66      * @param str the string to copy
67      */

68     public CharBuffer(String JavaDoc str) {
69         length = str.length();
70         contents = str.toCharArray();
71     }
72
73     /**
74      * Creates a new character buffer from the specified string
75      * buffer.
76      *
77      * @param str the string buffer to copy
78      */

79     public CharBuffer(StringBuffer JavaDoc str) {
80         length = str.length();
81         contents = new char[length];
82         str.getChars(0, length, contents, 0);
83     }
84
85     /**
86      * Appends the string representation of a boolean value to the
87      * end of the buffer.
88      *
89      * @param b the boolean value to append
90      *
91      * @return this character buffer
92      */

93     public CharBuffer append(boolean b) {
94         return append(String.valueOf(b));
95     }
96
97     /**
98      * Appends a character to the end of the buffer.
99      *
100      * @param c the character to append
101      *
102      * @return this character buffer
103      */

104     public CharBuffer append(char c) {
105         ensureCapacity(length + 1);
106         contents[length++] = c;
107         return this;
108     }
109
110     /**
111      * Appends a character array to the end of the buffer.
112      *
113      * @param str the characters to append
114      *
115      * @return this character buffer
116      */

117     public CharBuffer append(char[] str) {
118         return append(str, 0, str.length);
119     }
120
121     /**
122      * Appends a character array to the end of the buffer.
123      *
124      * @param str the character array to append
125      * @param offset the starting position in the array
126      * @param length the number of characters to copy
127      *
128      * @return this character buffer
129      */

130     public CharBuffer append(char[] str, int offset, int length) {
131         ensureCapacity(this.length + length);
132         System.arraycopy(str, offset, contents, this.length, length);
133         this.length += length;
134         return this;
135     }
136
137     /**
138      * Appends the string representation of a double value to the end
139      * of the buffer.
140      *
141      * @param d the double value to append
142      *
143      * @return this character buffer
144      */

145     public CharBuffer append (double d) {
146         return append(String.valueOf(d));
147     }
148
149     /**
150      * Appends the string representation of a float value to the end
151      * of the buffer.
152      *
153      * @param f the float value to append
154      *
155      * @return this character buffer
156      */

157     public CharBuffer append(float f) {
158         return append(String.valueOf(f));
159     }
160
161     /**
162      * Appends the string representation of an int value to the end of
163      * the buffer.
164      *
165      * @param i the int value to append
166      *
167      * @return this character buffer
168      */

169     public CharBuffer append(int i) {
170         return append(String.valueOf(i));
171     }
172
173     /**
174      * Appends the string representation of a long value to the end of
175      * the buffer.
176      *
177      * @param l the long value to append
178      *
179      * @return this character buffer
180      */

181     public CharBuffer append(long l) {
182         return append(String.valueOf(l));
183     }
184
185     /**
186      * Appends the string representation of an object to the end of
187      * the buffer.
188      *
189      * @param obj the object to append
190      *
191      * @return this character buffer
192      */

193     public CharBuffer append(Object JavaDoc obj) {
194         return append(obj.toString());
195     }
196
197     /**
198      * Appends a string to the end of the buffer.
199      *
200      * @param str the string to append
201      *
202      * @return this character buffer
203      */

204     public CharBuffer append(String JavaDoc str) {
205         ensureCapacity(length + str.length());
206         str.getChars(0, str.length(), contents, length);
207         length += str.length();
208         return this;
209     }
210
211     /**
212      * Appends a string buffer to the end of the buffer.
213      *
214      * @param str the string buffer to append
215      *
216      * @return this character buffer
217      */

218     public CharBuffer append(StringBuffer JavaDoc str) {
219         ensureCapacity(length + str.length());
220         str.getChars(0, str.length(), contents, length);
221         length += str.length();
222         return this;
223     }
224
225     /**
226      * Returns a character in the buffer.
227      *
228      * @param index the character position, 0 <= index < length
229      *
230      * @return the character found
231      *
232      * @throws StringIndexOutOfBoundsException if the character
233      * position was negative or higher or equal to the
234      * buffer length
235      */

236     public char charAt(int index) throws StringIndexOutOfBoundsException JavaDoc {
237         if (index < 0 || index >= length) {
238             throw new StringIndexOutOfBoundsException JavaDoc(index);
239         }
240         return contents[index];
241     }
242
243     /**
244      * Removes characters from this buffer.
245      *
246      * @param start the starting position (inclusive)
247      * @param end the ending position (exclusive)
248      *
249      * @return this character buffer
250      *
251      * @throws StringIndexOutOfBoundsException if the start or end
252      * indexes were out of bounds
253      */

254     public CharBuffer delete(int start, int end)
255         throws StringIndexOutOfBoundsException JavaDoc {
256
257         if (start < 0) {
258             throw new StringIndexOutOfBoundsException JavaDoc(start);
259         }
260         if (end > length) {
261             end = length;
262         }
263         if (start > end) {
264             throw new StringIndexOutOfBoundsException JavaDoc();
265         }
266         if (end - start > 0) {
267             System.arraycopy(contents, end, contents, start, length - end);
268             length -= (end - start);
269         }
270         return this;
271     }
272
273     /**
274      * Ensures that this buffer has at least the specified capacity.
275      *
276      * @param size the minimum buffer size
277      */

278     public void ensureCapacity(int size) {
279         char[] newContents;
280
281         if (contents.length >= size) {
282             return;
283         }
284         if (size < 2 * contents.length + 2) {
285             size = 2 * contents.length + 2;
286         }
287         newContents = new char[size];
288         System.arraycopy(contents, 0, newContents, 0, length);
289         contents = newContents;
290     }
291
292     /**
293      * Returns the number of characters in the buffer.
294      *
295      * @return the length of the buffer
296      */

297     public int length() {
298         return length;
299     }
300
301     /**
302      * Returns a string containing a sequence of characters from this
303      * buffer.
304      *
305      * @param start the start index, inclusive
306      *
307      * @return the new substring
308      *
309      * @throws StringIndexOutOfBoundsException if the start index was
310      * negative, or higher than the length of the string
311      */

312     public String JavaDoc substring(int start)
313         throws StringIndexOutOfBoundsException JavaDoc {
314
315         return substring(start, length);
316     }
317
318     /**
319      * Returns a string containing a sequence of characters from this
320      * buffer.
321      *
322      * @param start the start index, inclusive
323      * @param end end end index, exclusive
324      *
325      * @return the new substring
326      *
327      * @throws StringIndexOutOfBoundsException if the start index was
328      * negative, or higher than the length of the string
329      */

330     public String JavaDoc substring(int start, int end)
331         throws StringIndexOutOfBoundsException JavaDoc {
332
333         if (start < 0) {
334             throw new StringIndexOutOfBoundsException JavaDoc(start);
335         }
336         if (end > length) {
337             throw new StringIndexOutOfBoundsException JavaDoc(end);
338         }
339         if (start > end) {
340             throw new StringIndexOutOfBoundsException JavaDoc();
341         }
342         return new String JavaDoc(contents, start, end - start);
343     }
344
345     /**
346      * Returns a string containing all character in this buffer.
347      *
348      * @return a string containing the characters in this buffer
349      */

350     public String JavaDoc toString() {
351         return new String JavaDoc(contents, 0, length);
352     }
353 }
354
Popular Tags