Coverage Report - org.apache.commons.collections.primitives.ArrayShortList
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayShortList
98%
85/86
96%
27/28
2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.collections.primitives;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.ObjectInputStream;
 21  
 import java.io.ObjectOutputStream;
 22  
 import java.io.Serializable;
 23  
 
 24  
 /**
 25  
  * An {@link ShortList} backed by an array of <code>short</code>s.
 26  
  * This implementation supports all optional methods.
 27  
  * 
 28  
  * @since Commons Primitives 1.0
 29  
  * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $
 30  
  * 
 31  
  * @author Rodney Waldhoff 
 32  
  */
 33  
 public class ArrayShortList extends RandomAccessShortList implements ShortList, Serializable {
 34  
 
 35  
     // constructors
 36  
     //-------------------------------------------------------------------------
 37  
 
 38  
     /** 
 39  
      * Construct an empty list with the default
 40  
      * initial capacity.
 41  
      */
 42  
     public ArrayShortList() {
 43  600
         this(8);
 44  600
     }    
 45  
 
 46  
     /**
 47  
      * Construct an empty list with the given
 48  
      * initial capacity.
 49  
      * @throws IllegalArgumentException when <i>initialCapacity</i> is negative
 50  
      */
 51  608
     public ArrayShortList(int initialCapacity) {
 52  608
         if(initialCapacity < 0) {
 53  1
             throw new IllegalArgumentException("capacity " + initialCapacity);
 54  
         }
 55  607
         _data = new short[initialCapacity];
 56  607
         _size = 0;
 57  607
     }    
 58  
 
 59  
     /** 
 60  
      * Constructs a list containing the elements of the given collection, 
 61  
      * in the order they are returned by that collection's iterator.
 62  
      * 
 63  
      * @see ArrayShortList#addAll(org.apache.commons.collections.primitives.ShortCollection)
 64  
      * @param that the non-<code>null</code> collection of <code>short</code>s 
 65  
      *        to add
 66  
      * @throws NullPointerException if <i>that</i> is <code>null</code>
 67  
      */
 68  
     public ArrayShortList(ShortCollection that) { 
 69  2
         this(that.size());
 70  1
         addAll(that);
 71  1
     }    
 72  
 
 73  
     /**
 74  
      * Constructs a list by copying the specified array.
 75  
      * 
 76  
      * @param array  the array to initialize the collection with
 77  
      * @throws NullPointerException if the array is <code>null</code>
 78  
      */
 79  
     public ArrayShortList(short[] array) { 
 80  2
         this(array.length);
 81  1
         System.arraycopy(array, 0, _data, 0, array.length);
 82  1
         _size = array.length;
 83  1
     }
 84  
 
 85  
     // ShortList methods
 86  
     //-------------------------------------------------------------------------
 87  
 
 88  
     public short get(int index) {
 89  122364
         checkRange(index);
 90  122346
         return _data[index];
 91  
     }
 92  
     
 93  
     public int size() {
 94  183159
         return _size;
 95  
     }
 96  
     
 97  
     /** 
 98  
      * Removes the element at the specified position in 
 99  
      * (optional operation).  Any subsequent elements 
 100  
      * are shifted to the left, subtracting one from their 
 101  
      * indices.  Returns the element that was removed.
 102  
      * 
 103  
      * @param index the index of the element to remove
 104  
      * @return the value of the element that was removed
 105  
      * 
 106  
      * @throws UnsupportedOperationException when this operation is not 
 107  
      *         supported
 108  
      * @throws IndexOutOfBoundsException if the specified index is out of range
 109  
      */
 110  
     public short removeElementAt(int index) {
 111  434
         checkRange(index);
 112  416
         incrModCount();
 113  416
         short oldval = _data[index];
 114  416
         int numtomove = _size - index - 1;
 115  416
         if(numtomove > 0) {
 116  382
             System.arraycopy(_data,index+1,_data,index,numtomove);
 117  
         }
 118  416
         _size--;
 119  416
         return oldval;
 120  
     }
 121  
     
 122  
     /** 
 123  
      * Replaces the element at the specified 
 124  
      * position in me with the specified element
 125  
      * (optional operation). 
 126  
      * 
 127  
      * @param index the index of the element to change
 128  
      * @param element the value to be stored at the specified position
 129  
      * @return the value previously stored at the specified position
 130  
      * 
 131  
      * @throws UnsupportedOperationException when this operation is not 
 132  
      *         supported
 133  
      * @throws IndexOutOfBoundsException if the specified index is out of range
 134  
      */
 135  
     public short set(int index, short element) {
 136  110
         checkRange(index);
 137  92
         incrModCount();
 138  92
         short oldval = _data[index];
 139  92
         _data[index] = element;
 140  92
         return oldval;
 141  
     }
 142  
         
 143  
     /** 
 144  
      * Inserts the specified element at the specified position 
 145  
      * (optional operation). Shifts the element currently 
 146  
      * at that position (if any) and any subsequent elements to the 
 147  
      * right, increasing their indices.
 148  
      * 
 149  
      * @param index the index at which to insert the element
 150  
      * @param element the value to insert
 151  
      * 
 152  
      * @throws UnsupportedOperationException when this operation is not 
 153  
      *         supported
 154  
      * @throws IllegalArgumentException if some aspect of the specified element 
 155  
      *         prevents it from being added to me
 156  
      * @throws IndexOutOfBoundsException if the specified index is out of range
 157  
      */
 158  
     public void add(int index, short element) {
 159  2973
         checkRangeIncludingEndpoint(index);
 160  2957
         incrModCount();
 161  2957
         ensureCapacity(_size+1);
 162  2957
         int numtomove = _size-index;
 163  2957
         System.arraycopy(_data,index,_data,index+1,numtomove);
 164  2957
         _data[index] = element;
 165  2957
         _size++;
 166  2957
     }
 167  
 
 168  
     public void clear() {
 169  8
         incrModCount();
 170  8
         _size = 0;
 171  8
     }
 172  
 
 173  
     public boolean addAll(ShortCollection collection) {
 174  384
         return addAll(size(), collection);
 175  
     }
 176  
 
 177  
     public boolean addAll(int index, ShortCollection collection) {
 178  387
         if (collection.size() == 0) {
 179  0
             return false;
 180  
         }
 181  387
         checkRangeIncludingEndpoint(index);
 182  387
         incrModCount();
 183  387
         ensureCapacity(_size + collection.size());
 184  387
         if (index != _size) {
 185  
             // Need to move some elements
 186  3
             System.arraycopy(_data, index, _data, index + collection.size(), _size - index);
 187  
         }
 188  387
         for (ShortIterator it = collection.iterator(); it.hasNext();) {
 189  6414
             _data[index] = it.next();
 190  6414
             index++;
 191  
         }
 192  387
         _size += collection.size();
 193  387
         return true;
 194  
     }
 195  
 
 196  
     // capacity methods
 197  
     //-------------------------------------------------------------------------
 198  
 
 199  
     /** 
 200  
      * Increases my capacity, if necessary, to ensure that I can hold at 
 201  
      * least the number of elements specified by the minimum capacity 
 202  
      * argument without growing.
 203  
      */
 204  
     public void ensureCapacity(int mincap) {
 205  3344
         incrModCount();
 206  3344
         if(mincap > _data.length) {
 207  627
             int newcap = (_data.length * 3)/2 + 1;
 208  627
             short[] olddata = _data;
 209  627
             _data = new short[newcap < mincap ? mincap : newcap];
 210  627
             System.arraycopy(olddata,0,_data,0,_size);
 211  
         }
 212  3344
     }
 213  
 
 214  
     /** 
 215  
      * Reduce my capacity, if necessary, to match my
 216  
      * current {@link #size size}.
 217  
      */
 218  
     public void trimToSize() {
 219  12
         incrModCount();
 220  12
         if(_size < _data.length) {
 221  9
             short[] olddata = _data;
 222  9
             _data = new short[_size];
 223  9
             System.arraycopy(olddata,0,_data,0,_size);
 224  
         }
 225  12
     }
 226  
 
 227  
     // private methods
 228  
     //-------------------------------------------------------------------------
 229  
     
 230  
     private void writeObject(ObjectOutputStream out) throws IOException{
 231  13
         out.defaultWriteObject();
 232  13
         out.writeInt(_data.length);
 233  119
         for(int i=0;i<_size;i++) {
 234  106
             out.writeShort(_data[i]);
 235  
         }
 236  13
     }
 237  
 
 238  
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
 239  13
         in.defaultReadObject();
 240  13
         _data = new short[in.readInt()];
 241  119
         for(int i=0;i<_size;i++) {
 242  106
             _data[i] = in.readShort();
 243  
         }
 244  13
     }
 245  
     
 246  
     private final void checkRange(int index) {
 247  122908
         if(index < 0 || index >= _size) {
 248  54
             throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);
 249  
         }
 250  122854
     }
 251  
 
 252  
     private final void checkRangeIncludingEndpoint(int index) {
 253  3360
         if(index < 0 || index > _size) {
 254  16
             throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index);
 255  
         }
 256  3344
     }
 257  
 
 258  
     // attributes
 259  
     //-------------------------------------------------------------------------
 260  
     
 261  608
     private transient short[] _data = null;
 262  608
     private int _size = 0;
 263  
 
 264  
 }