001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/RandomAccessCharList.java,v 1.3 2003/10/16 20:49:36 scolebourne Exp $
003: * ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: */
057:
058: package org.apache.commons.collections.primitives;
059:
060: import java.util.ConcurrentModificationException;
061: import java.util.NoSuchElementException;
062:
063: /**
064: * Abstract base class for {@link CharList}s backed
065: * by random access structures like arrays.
066: * <p />
067: * Read-only subclasses must override {@link #get}
068: * and {@link #size}. Mutable subclasses
069: * should also override {@link #set}. Variably-sized
070: * subclasses should also override {@link #add}
071: * and {@link #removeElementAt}. All other methods
072: * have at least some base implementation derived from
073: * these. Subclasses may choose to override these methods
074: * to provide a more efficient implementation.
075: *
076: * @since Commons Primitives 1.0
077: * @version $Revision: 1.3 $ $Date: 2003/10/16 20:49:36 $
078: *
079: * @author Rodney Waldhoff
080: */
081: public abstract class RandomAccessCharList extends
082: AbstractCharCollection implements CharList {
083:
084: // constructors
085: //-------------------------------------------------------------------------
086:
087: /** Constructs an empty list. */
088: protected RandomAccessCharList() {
089: }
090:
091: // fully abstract methods
092: //-------------------------------------------------------------------------
093:
094: public abstract char get(int index);
095:
096: public abstract int size();
097:
098: // unsupported in base
099: //-------------------------------------------------------------------------
100:
101: /**
102: * Unsupported in this implementation.
103: * @throws UnsupportedOperationException since this method is not supported
104: */
105: public char removeElementAt(int index) {
106: throw new UnsupportedOperationException();
107: }
108:
109: /**
110: * Unsupported in this implementation.
111: * @throws UnsupportedOperationException since this method is not supported
112: */
113: public char set(int index, char element) {
114: throw new UnsupportedOperationException();
115: }
116:
117: /**
118: * Unsupported in this implementation.
119: * @throws UnsupportedOperationException since this method is not supported
120: */
121: public void add(int index, char element) {
122: throw new UnsupportedOperationException();
123: }
124:
125: //-------------------------------------------------------------------------
126:
127: // javadocs here are inherited
128:
129: public boolean add(char element) {
130: add(size(), element);
131: return true;
132: }
133:
134: public boolean addAll(int index, CharCollection collection) {
135: boolean modified = false;
136: for (CharIterator iter = collection.iterator(); iter.hasNext();) {
137: add(index++, iter.next());
138: modified = true;
139: }
140: return modified;
141: }
142:
143: public int indexOf(char element) {
144: int i = 0;
145: for (CharIterator iter = iterator(); iter.hasNext();) {
146: if (iter.next() == element) {
147: return i;
148: } else {
149: i++;
150: }
151: }
152: return -1;
153: }
154:
155: public int lastIndexOf(char element) {
156: for (CharListIterator iter = listIterator(size()); iter
157: .hasPrevious();) {
158: if (iter.previous() == element) {
159: return iter.nextIndex();
160: }
161: }
162: return -1;
163: }
164:
165: public CharIterator iterator() {
166: return listIterator();
167: }
168:
169: public CharListIterator listIterator() {
170: return listIterator(0);
171: }
172:
173: public CharListIterator listIterator(int index) {
174: return new RandomAccessCharListIterator(this , index);
175: }
176:
177: public CharList subList(int fromIndex, int toIndex) {
178: return new RandomAccessCharSubList(this , fromIndex, toIndex);
179: }
180:
181: public boolean equals(Object that) {
182: if (this == that) {
183: return true;
184: } else if (that instanceof CharList) {
185: CharList thatList = (CharList) that;
186: if (size() != thatList.size()) {
187: return false;
188: }
189: for (CharIterator thatIter = thatList.iterator(), this Iter = iterator(); this Iter
190: .hasNext();) {
191: if (this Iter.next() != thatIter.next()) {
192: return false;
193: }
194: }
195: return true;
196: } else {
197: return false;
198: }
199: }
200:
201: public int hashCode() {
202: int hash = 1;
203: for (CharIterator iter = iterator(); iter.hasNext();) {
204: hash = 31 * hash + ((int) (iter.next()));
205: }
206: return hash;
207: }
208:
209: public String toString() {
210: // could cache these like StringBuffer does
211: return new String(toArray());
212: }
213:
214: // protected utilities
215: //-------------------------------------------------------------------------
216:
217: /** Get my count of structural modifications. */
218: protected int getModCount() {
219: return _modCount;
220: }
221:
222: /** Increment my count of structural modifications. */
223: protected void incrModCount() {
224: _modCount++;
225: }
226:
227: // attributes
228: //-------------------------------------------------------------------------
229:
230: private int _modCount = 0;
231:
232: // inner classes
233: //-------------------------------------------------------------------------
234:
235: private static class ComodChecker {
236: ComodChecker(RandomAccessCharList source) {
237: _source = source;
238: resyncModCount();
239: }
240:
241: protected RandomAccessCharList getList() {
242: return _source;
243: }
244:
245: protected void assertNotComodified()
246: throws ConcurrentModificationException {
247: if (_expectedModCount != getList().getModCount()) {
248: throw new ConcurrentModificationException();
249: }
250: }
251:
252: protected void resyncModCount() {
253: _expectedModCount = getList().getModCount();
254: }
255:
256: private RandomAccessCharList _source = null;
257: private int _expectedModCount = -1;
258: }
259:
260: protected static class RandomAccessCharListIterator extends
261: ComodChecker implements CharListIterator {
262: RandomAccessCharListIterator(RandomAccessCharList list,
263: int index) {
264: super (list);
265: if (index < 0 || index > getList().size()) {
266: throw new IndexOutOfBoundsException("Index " + index
267: + " not in [0," + getList().size() + ")");
268: } else {
269: _nextIndex = index;
270: resyncModCount();
271: }
272: }
273:
274: public boolean hasNext() {
275: assertNotComodified();
276: return _nextIndex < getList().size();
277: }
278:
279: public boolean hasPrevious() {
280: assertNotComodified();
281: return _nextIndex > 0;
282: }
283:
284: public int nextIndex() {
285: assertNotComodified();
286: return _nextIndex;
287: }
288:
289: public int previousIndex() {
290: assertNotComodified();
291: return _nextIndex - 1;
292: }
293:
294: public char next() {
295: assertNotComodified();
296: if (!hasNext()) {
297: throw new NoSuchElementException();
298: } else {
299: char val = getList().get(_nextIndex);
300: _lastReturnedIndex = _nextIndex;
301: _nextIndex++;
302: return val;
303: }
304: }
305:
306: public char previous() {
307: assertNotComodified();
308: if (!hasPrevious()) {
309: throw new NoSuchElementException();
310: } else {
311: char val = getList().get(_nextIndex - 1);
312: _lastReturnedIndex = _nextIndex - 1;
313: _nextIndex--;
314: return val;
315: }
316: }
317:
318: public void add(char value) {
319: assertNotComodified();
320: getList().add(_nextIndex, value);
321: _nextIndex++;
322: _lastReturnedIndex = -1;
323: resyncModCount();
324: }
325:
326: public void remove() {
327: assertNotComodified();
328: if (-1 == _lastReturnedIndex) {
329: throw new IllegalStateException();
330: } else {
331: getList().removeElementAt(_lastReturnedIndex);
332: _lastReturnedIndex = -1;
333: _nextIndex--;
334: resyncModCount();
335: }
336: }
337:
338: public void set(char value) {
339: assertNotComodified();
340: if (-1 == _lastReturnedIndex) {
341: throw new IllegalStateException();
342: } else {
343: getList().set(_lastReturnedIndex, value);
344: resyncModCount();
345: }
346: }
347:
348: private int _nextIndex = 0;
349: private int _lastReturnedIndex = -1;
350: }
351:
352: protected static class RandomAccessCharSubList extends
353: RandomAccessCharList implements CharList {
354: RandomAccessCharSubList(RandomAccessCharList list,
355: int fromIndex, int toIndex) {
356: if (fromIndex < 0 || toIndex > list.size()) {
357: throw new IndexOutOfBoundsException();
358: } else if (fromIndex > toIndex) {
359: throw new IllegalArgumentException();
360: } else {
361: _list = list;
362: _offset = fromIndex;
363: _limit = toIndex - fromIndex;
364: _comod = new ComodChecker(list);
365: _comod.resyncModCount();
366: }
367: }
368:
369: public char get(int index) {
370: checkRange(index);
371: _comod.assertNotComodified();
372: return _list.get(toUnderlyingIndex(index));
373: }
374:
375: public char removeElementAt(int index) {
376: checkRange(index);
377: _comod.assertNotComodified();
378: char val = _list.removeElementAt(toUnderlyingIndex(index));
379: _limit--;
380: _comod.resyncModCount();
381: incrModCount();
382: return val;
383: }
384:
385: public char set(int index, char element) {
386: checkRange(index);
387: _comod.assertNotComodified();
388: char val = _list.set(toUnderlyingIndex(index), element);
389: incrModCount();
390: _comod.resyncModCount();
391: return val;
392: }
393:
394: public void add(int index, char element) {
395: checkRangeIncludingEndpoint(index);
396: _comod.assertNotComodified();
397: _list.add(toUnderlyingIndex(index), element);
398: _limit++;
399: _comod.resyncModCount();
400: incrModCount();
401: }
402:
403: public int size() {
404: _comod.assertNotComodified();
405: return _limit;
406: }
407:
408: private void checkRange(int index) {
409: if (index < 0 || index >= size()) {
410: throw new IndexOutOfBoundsException("index " + index
411: + " not in [0," + size() + ")");
412: }
413: }
414:
415: private void checkRangeIncludingEndpoint(int index) {
416: if (index < 0 || index > size()) {
417: throw new IndexOutOfBoundsException("index " + index
418: + " not in [0," + size() + "]");
419: }
420: }
421:
422: private int toUnderlyingIndex(int index) {
423: return (index + _offset);
424: }
425:
426: private int _offset = 0;
427: private int _limit = 0;
428: private RandomAccessCharList _list = null;
429: private ComodChecker _comod = null;
430:
431: }
432: }
|