001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.lcdui;
027:
028: /**
029: * Similar to StringBuffer but does NOT resize when needed, only when
030: * requested
031: */
032: public class DynamicCharacterArray {
033:
034: /**
035: * buffer to store data. The capacity of this DynamicCharacterArray
036: * is equal to the length of this buffer
037: */
038: protected char buffer[];
039:
040: /**
041: * the length of the array currently in use
042: */
043: protected int length;
044:
045: /**
046: * Initializes the DCA with a capacity and an empty array
047: *
048: * @param capacity the maximum storage capacity
049: * @throws IllegalArgumentException if capacity <= 0
050: * @throws IllegalArgumentException if data.length > capacity
051: */
052: public DynamicCharacterArray(int capacity) {
053: this (null, capacity);
054: }
055:
056: /**
057: * Initializes the array with str and a capacity of str.length()
058: *
059: * @param str initial array data, may NOT be null
060: * @throws NullPointerException if str is null
061: */
062: public DynamicCharacterArray(String str) {
063: this (str.toCharArray());
064: }
065:
066: /**
067: * Initializes the array with data and a capacity of data.length
068: *
069: * @param data initial array data, may NOT be null
070: * @throws NullPointerException if data is null
071: */
072: public DynamicCharacterArray(char[] data) {
073: this (data, data.length);
074: }
075:
076: /**
077: * Initializes the array with data and a capacity of capacity
078: *
079: * @param data initial array data, may be null
080: * @param capacity initial maximum capacity
081: * @throws IllegalArgumentException if capacity <= 0
082: * @throws IllegalArgumentException if data.length > capacity
083: */
084: public DynamicCharacterArray(char[] data, int capacity) {
085: int len;
086:
087: if (capacity <= 0) {
088: throw new IllegalArgumentException();
089: }
090:
091: if (data != null) {
092: if (data.length > capacity) {
093: throw new IllegalArgumentException();
094: }
095:
096: this .length = data.length;
097:
098: buffer = new char[capacity];
099: System.arraycopy(data, 0, buffer, 0, this .length);
100:
101: } else {
102: buffer = new char[capacity];
103: }
104: }
105:
106: /**
107: * Inserts an subset of an array into the buffer.
108: * The offset parameter must be within the range [0..(size())], inclusive.
109: * The length parameter must be a non-negative integer such that
110: * (offset + length) <= size().
111: *
112: * @param data array to insert
113: * @param offset offset into data
114: * @param ins_length length of subset
115: * @param position index into the internal buffer to insert the subset
116: * @return the actual position the data was inserted
117: * @throws ArrayIndexOutOfBoundsException if offset and length
118: * specify an invalid range
119: * @throws IllegalArgumentException if the resulting array would exceed
120: * the capacity
121: * @throws NullPointerException if data is null
122: */
123: public int insert(char[] data, int offset, int ins_length,
124: int position) {
125:
126: if (position < 0) {
127: position = 0;
128: } else if (position > length) {
129: position = length;
130: }
131:
132: if (offset < 0 || offset > data.length || ins_length < 0
133: || ins_length > data.length
134: || (offset + ins_length) < 0
135: || (offset + ins_length) > data.length) {
136: throw new ArrayIndexOutOfBoundsException();
137: }
138:
139: if (length + ins_length > buffer.length) {
140: throw new IllegalArgumentException();
141: }
142:
143: if (data.length != 0) {
144:
145: System.arraycopy(buffer, position, buffer, position
146: + ins_length, length - position);
147: System
148: .arraycopy(data, offset, buffer, position,
149: ins_length);
150:
151: length += ins_length;
152: }
153:
154: return position;
155: }
156:
157: /**
158: * Inserts an character into the buffer.
159: *
160: * @param ch character to insert
161: * @param position index into the internal buffer to insert the subset
162: * @return the actual position the data was inserted
163: * @throws IllegalArgumentException if the resulting array would exceed
164: * the capacity
165: */
166: public int insert(int position, char ch) {
167: char arr[] = { ch };
168: return insert(arr, 0, 1, position);
169: }
170:
171: /**
172: * Inserts an String into the buffer.
173: *
174: * @param str String to insert
175: * @param position index into the internal buffer to insert the subset
176: * @return the actual position the data was inserted
177: * @throws ArrayIndexOutOfBoundsException if offset and length
178: * specify an invalid range
179: * @throws IllegalArgumentException if the resulting array would exceed
180: * the capacity
181: * @throws NullPointerException if data is null
182: */
183: public int insert(int position, String str) {
184: return insert(str.toCharArray(), 0, str.length(), position);
185: }
186:
187: /**
188: * Appends a character onto the end of the buffer.
189: *
190: * @param c character to append
191: * @throws IllegalArgumentException if the resulting array would exceed
192: * the capacity
193: */
194: public void append(char c) {
195: insert(length, c);
196: }
197:
198: /**
199: * Sets the internal buffer to the values of the subset specified
200: * The offset parameter must be within the range [0..(size())], inclusive.
201: * The length parameter must be a non-negative integer such that
202: * (offset + length) <= size(). If data is null the buffer is emptied.
203: *
204: * @param data the data to use to set the buffer
205: * @param offset offset into data
206: * @param set_length length of the subset
207: * @throws ArrayIndexOutOfBoundsException if offset and length
208: * specify an invalid range
209: * @throws IllegalArgumentException if length exceeds the capacity
210: */
211: public void set(char[] data, int offset, int set_length) {
212:
213: if (data == null) {
214: length = 0;
215: return;
216: }
217:
218: if (offset < 0 || offset > data.length || set_length < 0
219: || set_length > data.length
220: || (offset + set_length) < 0
221: || (offset + set_length) > data.length) {
222: throw new ArrayIndexOutOfBoundsException();
223: }
224:
225: if (set_length > buffer.length) {
226: throw new IllegalArgumentException();
227: }
228:
229: System.arraycopy(data, offset, buffer, 0, set_length);
230: length = set_length;
231: }
232:
233: /**
234: * Returns the internal buffer in the array provided. This must
235: * be at least length() long.
236: *
237: * @param data array to fill with the character of the internal buffer
238: * @throws IndexOutOfBoundsException if data cannot hold the contents
239: * @throws NullPointerException if data is null
240: */
241: public void get(char[] data) {
242: getChars(0, buffer.length, data, 0);
243: }
244:
245: /**
246: * Returns the internal buffer in the array provided.
247: *
248: * @param position index into the internal buffer to start copying
249: * @param get_length length of region to copy
250: * @param data array to fill with the character of the internal buffer
251: * @param offset offset into data to copy to
252: * @throws IndexOutOfBoundsException if data cannot hold the contents
253: * @throws NullPointerException if data is null
254: */
255: public void getChars(int position, int get_length, char[] data,
256: int offset) {
257: System.arraycopy(buffer, position, data, offset, get_length);
258: }
259:
260: /**
261: * Returns a copy of the active portion of the internal buffer.
262: *
263: * @return character array
264: */
265: public char[] toCharArray() {
266: char[] buf = new char[length];
267: System.arraycopy(buffer, 0, buf, 0, buf.length);
268: return buf;
269: }
270:
271: /**
272: * Deletes the specified range from the internal buffer
273: *
274: * @param offset offset to begin deleting
275: * @param del_length length of portion to delete
276: * @throws StringIndexOutOfBoundsException if offset and length do
277: * not specific a valid range in the internal buffer
278: */
279: public void delete(int offset, int del_length) {
280: if (offset < 0 || del_length < 0 || (offset + del_length) < 0
281: || (offset + del_length) > length) {
282: throw new StringIndexOutOfBoundsException();
283: }
284:
285: if ((offset + del_length) < length) {
286: System.arraycopy(buffer, offset + del_length, buffer,
287: offset, length - (offset + del_length));
288: }
289:
290: length -= del_length;
291: }
292:
293: /**
294: * Sets the maximum capacity to the specified value. the buffer may
295: * be truncated if the new capacity is less than the current capacity.
296: *
297: * @param capacity new maximum capacity
298: * @throws IllegalArgumentException is zero or less
299: */
300:
301: public void setCapacity(int capacity) {
302:
303: if (capacity <= 0) {
304: throw new IllegalArgumentException();
305: }
306:
307: if (buffer.length == capacity) {
308: return;
309: }
310:
311: if (length > capacity) {
312: length = capacity;
313: }
314:
315: char[] newBuffer = new char[capacity];
316: System.arraycopy(buffer, 0, newBuffer, 0, length);
317:
318: buffer = newBuffer;
319: }
320:
321: /**
322: * Returns the current capacity
323: *
324: * @return the maximum capacity
325: */
326: public int capacity() {
327: return buffer.length;
328: }
329:
330: /**
331: * Returns the length of current data
332: *
333: * @return current length
334: */
335: public int length() {
336: return length;
337: }
338:
339: /**
340: * Returns the character at the specified index of the internal buffer
341: *
342: * @param index index into the buffer
343: * @return the character at the specified index
344: * @throws IndexOutOfBoundsException if the 0 < index or index > length()
345: */
346: public char charAt(int index) {
347: if (index < 0 || index > length) {
348: throw new IndexOutOfBoundsException();
349: }
350:
351: return buffer[index];
352: }
353:
354: /**
355: * Sets the character at index to ch
356: *
357: * @param index index into the buffer
358: * @param ch character to set to
359: * @throws IndexOutOfBoundsException if the 0 < index or index > length()
360: */
361: public void setCharAt(int index, char ch) {
362: if (index < 0 || index > length) {
363: throw new IndexOutOfBoundsException();
364: }
365:
366: buffer[index] = ch;
367: }
368:
369: /**
370: * Return a String representation of the internal buffer
371: *
372: * @return a String object
373: */
374: public String toString() {
375: return String.valueOf(buffer, 0, length);
376: }
377: }
|