001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/RandomAccessIntList.java,v 1.3 2003/10/16 20:49:35 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 IntList}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:35 $
078: *
079: * @author Rodney Waldhoff
080: */
081: public abstract class RandomAccessIntList extends AbstractIntCollection
082: implements IntList {
083:
084: // constructors
085: //-------------------------------------------------------------------------
086:
087: /** Constructs an empty list. */
088: protected RandomAccessIntList() {
089: }
090:
091: // fully abstract methods
092: //-------------------------------------------------------------------------
093:
094: public abstract int 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 int 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 int set(int index, int 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, int element) {
122: throw new UnsupportedOperationException();
123: }
124:
125: //-------------------------------------------------------------------------
126:
127: // javadocs here are inherited
128:
129: public boolean add(int element) {
130: add(size(), element);
131: return true;
132: }
133:
134: public boolean addAll(int index, IntCollection collection) {
135: boolean modified = false;
136: for (IntIterator iter = collection.iterator(); iter.hasNext();) {
137: add(index++, iter.next());
138: modified = true;
139: }
140: return modified;
141: }
142:
143: public int indexOf(int element) {
144: int i = 0;
145: for (IntIterator 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(int element) {
156: for (IntListIterator iter = listIterator(size()); iter
157: .hasPrevious();) {
158: if (iter.previous() == element) {
159: return iter.nextIndex();
160: }
161: }
162: return -1;
163: }
164:
165: public IntIterator iterator() {
166: return listIterator();
167: }
168:
169: public IntListIterator listIterator() {
170: return listIterator(0);
171: }
172:
173: public IntListIterator listIterator(int index) {
174: return new RandomAccessIntListIterator(this , index);
175: }
176:
177: public IntList subList(int fromIndex, int toIndex) {
178: return new RandomAccessIntSubList(this , fromIndex, toIndex);
179: }
180:
181: public boolean equals(Object that) {
182: if (this == that) {
183: return true;
184: } else if (that instanceof IntList) {
185: IntList thatList = (IntList) that;
186: if (size() != thatList.size()) {
187: return false;
188: }
189: for (IntIterator 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 (IntIterator iter = iterator(); iter.hasNext();) {
204: hash = 31 * hash + iter.next();
205: }
206: return hash;
207: }
208:
209: public String toString() {
210: StringBuffer buf = new StringBuffer();
211: buf.append("[");
212: for (IntIterator iter = iterator(); iter.hasNext();) {
213: buf.append(iter.next());
214: if (iter.hasNext()) {
215: buf.append(", ");
216: }
217: }
218: buf.append("]");
219: return buf.toString();
220: }
221:
222: // protected utilities
223: //-------------------------------------------------------------------------
224:
225: /** Get my count of structural modifications. */
226: protected int getModCount() {
227: return _modCount;
228: }
229:
230: /** Increment my count of structural modifications. */
231: protected void incrModCount() {
232: _modCount++;
233: }
234:
235: // attributes
236: //-------------------------------------------------------------------------
237:
238: private int _modCount = 0;
239:
240: // inner classes
241: //-------------------------------------------------------------------------
242:
243: private static class ComodChecker {
244: ComodChecker(RandomAccessIntList source) {
245: _source = source;
246: resyncModCount();
247: }
248:
249: protected RandomAccessIntList getList() {
250: return _source;
251: }
252:
253: protected void assertNotComodified()
254: throws ConcurrentModificationException {
255: if (_expectedModCount != getList().getModCount()) {
256: throw new ConcurrentModificationException();
257: }
258: }
259:
260: protected void resyncModCount() {
261: _expectedModCount = getList().getModCount();
262: }
263:
264: private RandomAccessIntList _source = null;
265: private int _expectedModCount = -1;
266: }
267:
268: protected static class RandomAccessIntListIterator extends
269: ComodChecker implements IntListIterator {
270: RandomAccessIntListIterator(RandomAccessIntList list, int index) {
271: super (list);
272: if (index < 0 || index > getList().size()) {
273: throw new IndexOutOfBoundsException("Index " + index
274: + " not in [0," + getList().size() + ")");
275: } else {
276: _nextIndex = index;
277: resyncModCount();
278: }
279: }
280:
281: public boolean hasNext() {
282: assertNotComodified();
283: return _nextIndex < getList().size();
284: }
285:
286: public boolean hasPrevious() {
287: assertNotComodified();
288: return _nextIndex > 0;
289: }
290:
291: public int nextIndex() {
292: assertNotComodified();
293: return _nextIndex;
294: }
295:
296: public int previousIndex() {
297: assertNotComodified();
298: return _nextIndex - 1;
299: }
300:
301: public int next() {
302: assertNotComodified();
303: if (!hasNext()) {
304: throw new NoSuchElementException();
305: } else {
306: int val = getList().get(_nextIndex);
307: _lastReturnedIndex = _nextIndex;
308: _nextIndex++;
309: return val;
310: }
311: }
312:
313: public int previous() {
314: assertNotComodified();
315: if (!hasPrevious()) {
316: throw new NoSuchElementException();
317: } else {
318: int val = getList().get(_nextIndex - 1);
319: _lastReturnedIndex = _nextIndex - 1;
320: _nextIndex--;
321: return val;
322: }
323: }
324:
325: public void add(int value) {
326: assertNotComodified();
327: getList().add(_nextIndex, value);
328: _nextIndex++;
329: _lastReturnedIndex = -1;
330: resyncModCount();
331: }
332:
333: public void remove() {
334: assertNotComodified();
335: if (-1 == _lastReturnedIndex) {
336: throw new IllegalStateException();
337: } else {
338: getList().removeElementAt(_lastReturnedIndex);
339: _lastReturnedIndex = -1;
340: _nextIndex--;
341: resyncModCount();
342: }
343: }
344:
345: public void set(int value) {
346: assertNotComodified();
347: if (-1 == _lastReturnedIndex) {
348: throw new IllegalStateException();
349: } else {
350: getList().set(_lastReturnedIndex, value);
351: resyncModCount();
352: }
353: }
354:
355: private int _nextIndex = 0;
356: private int _lastReturnedIndex = -1;
357: }
358:
359: protected static class RandomAccessIntSubList extends
360: RandomAccessIntList implements IntList {
361: RandomAccessIntSubList(RandomAccessIntList list, int fromIndex,
362: int toIndex) {
363: if (fromIndex < 0 || toIndex > list.size()) {
364: throw new IndexOutOfBoundsException();
365: } else if (fromIndex > toIndex) {
366: throw new IllegalArgumentException();
367: } else {
368: _list = list;
369: _offset = fromIndex;
370: _limit = toIndex - fromIndex;
371: _comod = new ComodChecker(list);
372: _comod.resyncModCount();
373: }
374: }
375:
376: public int get(int index) {
377: checkRange(index);
378: _comod.assertNotComodified();
379: return _list.get(toUnderlyingIndex(index));
380: }
381:
382: public int removeElementAt(int index) {
383: checkRange(index);
384: _comod.assertNotComodified();
385: int val = _list.removeElementAt(toUnderlyingIndex(index));
386: _limit--;
387: _comod.resyncModCount();
388: incrModCount();
389: return val;
390: }
391:
392: public int set(int index, int element) {
393: checkRange(index);
394: _comod.assertNotComodified();
395: int val = _list.set(toUnderlyingIndex(index), element);
396: incrModCount();
397: _comod.resyncModCount();
398: return val;
399: }
400:
401: public void add(int index, int element) {
402: checkRangeIncludingEndpoint(index);
403: _comod.assertNotComodified();
404: _list.add(toUnderlyingIndex(index), element);
405: _limit++;
406: _comod.resyncModCount();
407: incrModCount();
408: }
409:
410: public int size() {
411: _comod.assertNotComodified();
412: return _limit;
413: }
414:
415: private void checkRange(int index) {
416: if (index < 0 || index >= size()) {
417: throw new IndexOutOfBoundsException("index " + index
418: + " not in [0," + size() + ")");
419: }
420: }
421:
422: private void checkRangeIncludingEndpoint(int index) {
423: if (index < 0 || index > size()) {
424: throw new IndexOutOfBoundsException("index " + index
425: + " not in [0," + size() + "]");
426: }
427: }
428:
429: private int toUnderlyingIndex(int index) {
430: return (index + _offset);
431: }
432:
433: private int _offset = 0;
434: private int _limit = 0;
435: private RandomAccessIntList _list = null;
436: private ComodChecker _comod = null;
437:
438: }
439: }
|