001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/ArrayShortList.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.io.IOException;
061: import java.io.ObjectInputStream;
062: import java.io.ObjectOutputStream;
063: import java.io.Serializable;
064:
065: /**
066: * An {@link ShortList} backed by an array of <code>short</code>s.
067: * This implementation supports all optional methods.
068: *
069: * @since Commons Primitives 1.0
070: * @version $Revision: 1.3 $ $Date: 2003/10/16 20:49:36 $
071: *
072: * @author Rodney Waldhoff
073: */
074: public class ArrayShortList extends RandomAccessShortList implements
075: ShortList, Serializable {
076:
077: // constructors
078: //-------------------------------------------------------------------------
079:
080: /**
081: * Construct an empty list with the default
082: * initial capacity.
083: */
084: public ArrayShortList() {
085: this (8);
086: }
087:
088: /**
089: * Construct an empty list with the given
090: * initial capacity.
091: * @throws IllegalArgumentException when <i>initialCapacity</i> is negative
092: */
093: public ArrayShortList(int initialCapacity) {
094: if (initialCapacity < 0) {
095: throw new IllegalArgumentException("capacity "
096: + initialCapacity);
097: }
098: _data = new short[initialCapacity];
099: _size = 0;
100: }
101:
102: /**
103: * Constructs a list containing the elements of the given collection,
104: * in the order they are returned by that collection's iterator.
105: *
106: * @see ArrayShortList#addAll(org.apache.commons.collections.primitives.ShortCollection)
107: * @param that the non-<code>null</code> collection of <code>short</code>s
108: * to add
109: * @throws NullPointerException if <i>that</i> is <code>null</code>
110: */
111: public ArrayShortList(ShortCollection that) {
112: this (that.size());
113: addAll(that);
114: }
115:
116: // ShortList methods
117: //-------------------------------------------------------------------------
118:
119: public short get(int index) {
120: checkRange(index);
121: return _data[index];
122: }
123:
124: public int size() {
125: return _size;
126: }
127:
128: /**
129: * Removes the element at the specified position in
130: * (optional operation). Any subsequent elements
131: * are shifted to the left, subtracting one from their
132: * indices. Returns the element that was removed.
133: *
134: * @param index the index of the element to remove
135: * @return the value of the element that was removed
136: *
137: * @throws UnsupportedOperationException when this operation is not
138: * supported
139: * @throws IndexOutOfBoundsException if the specified index is out of range
140: */
141: public short removeElementAt(int index) {
142: checkRange(index);
143: incrModCount();
144: short oldval = _data[index];
145: int numtomove = _size - index - 1;
146: if (numtomove > 0) {
147: System.arraycopy(_data, index + 1, _data, index, numtomove);
148: }
149: _size--;
150: return oldval;
151: }
152:
153: /**
154: * Replaces the element at the specified
155: * position in me with the specified element
156: * (optional operation).
157: *
158: * @param index the index of the element to change
159: * @param element the value to be stored at the specified position
160: * @return the value previously stored at the specified position
161: *
162: * @throws UnsupportedOperationException when this operation is not
163: * supported
164: * @throws IndexOutOfBoundsException if the specified index is out of range
165: */
166: public short set(int index, short element) {
167: checkRange(index);
168: incrModCount();
169: short oldval = _data[index];
170: _data[index] = element;
171: return oldval;
172: }
173:
174: /**
175: * Inserts the specified element at the specified position
176: * (optional operation). Shifts the element currently
177: * at that position (if any) and any subsequent elements to the
178: * right, increasing their indices.
179: *
180: * @param index the index at which to insert the element
181: * @param element the value to insert
182: *
183: * @throws UnsupportedOperationException when this operation is not
184: * supported
185: * @throws IllegalArgumentException if some aspect of the specified element
186: * prevents it from being added to me
187: * @throws IndexOutOfBoundsException if the specified index is out of range
188: */
189: public void add(int index, short element) {
190: checkRangeIncludingEndpoint(index);
191: incrModCount();
192: ensureCapacity(_size + 1);
193: int numtomove = _size - index;
194: System.arraycopy(_data, index, _data, index + 1, numtomove);
195: _data[index] = element;
196: _size++;
197: }
198:
199: // capacity methods
200: //-------------------------------------------------------------------------
201:
202: /**
203: * Increases my capacity, if necessary, to ensure that I can hold at
204: * least the number of elements specified by the minimum capacity
205: * argument without growing.
206: */
207: public void ensureCapacity(int mincap) {
208: incrModCount();
209: if (mincap > _data.length) {
210: int newcap = (_data.length * 3) / 2 + 1;
211: short[] olddata = _data;
212: _data = new short[newcap < mincap ? mincap : newcap];
213: System.arraycopy(olddata, 0, _data, 0, _size);
214: }
215: }
216:
217: /**
218: * Reduce my capacity, if necessary, to match my
219: * current {@link #size size}.
220: */
221: public void trimToSize() {
222: incrModCount();
223: if (_size < _data.length) {
224: short[] olddata = _data;
225: _data = new short[_size];
226: System.arraycopy(olddata, 0, _data, 0, _size);
227: }
228: }
229:
230: // private methods
231: //-------------------------------------------------------------------------
232:
233: private void writeObject(ObjectOutputStream out) throws IOException {
234: out.defaultWriteObject();
235: out.writeInt(_data.length);
236: for (int i = 0; i < _size; i++) {
237: out.writeShort(_data[i]);
238: }
239: }
240:
241: private void readObject(ObjectInputStream in) throws IOException,
242: ClassNotFoundException {
243: in.defaultReadObject();
244: _data = new short[in.readInt()];
245: for (int i = 0; i < _size; i++) {
246: _data[i] = in.readShort();
247: }
248: }
249:
250: private final void checkRange(int index) {
251: if (index < 0 || index >= _size) {
252: throw new IndexOutOfBoundsException(
253: "Should be at least 0 and less than " + _size
254: + ", found " + index);
255: }
256: }
257:
258: private final void checkRangeIncludingEndpoint(int index) {
259: if (index < 0 || index > _size) {
260: throw new IndexOutOfBoundsException(
261: "Should be at least 0 and at most " + _size
262: + ", found " + index);
263: }
264: }
265:
266: // attributes
267: //-------------------------------------------------------------------------
268:
269: private transient short[] _data = null;
270: private int _size = 0;
271:
272: }
|