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