1 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 42 public class ArrayUnsignedByteList extends RandomAccessShortList implements ShortList, Serializable { 43 44 47 51 public ArrayUnsignedByteList() { 52 this(8); 53 } 54 55 60 public ArrayUnsignedByteList(int initialCapacity) { 61 if(initialCapacity < 0) { 62 throw new IllegalArgumentException ("capacity " + initialCapacity); 63 } 64 _data = new byte[initialCapacity]; 65 _size = 0; 66 } 67 68 77 public ArrayUnsignedByteList(ShortCollection that) { 78 this(that.size()); 79 addAll(that); 80 } 81 82 88 public ArrayUnsignedByteList(short[] array) { 89 this(array.length); 90 for(int i=0;i<array.length;i++) { 91 _data[i] = fromShort(array[i]); 92 } 93 _size = array.length; 94 } 95 96 99 109 public short get(int index) { 110 checkRange(index); 111 return toShort(_data[index]); 112 } 113 114 public int size() { 115 return _size; 116 } 117 118 133 public short removeElementAt(int index) { 134 checkRange(index); 135 incrModCount(); 136 short oldval = toShort(_data[index]); 137 int numtomove = _size - index - 1; 138 if(numtomove > 0) { 139 System.arraycopy(_data,index+1,_data,index,numtomove); 140 } 141 _size--; 142 return oldval; 143 } 144 145 160 public short set(int index, short element) { 161 assertValidUnsignedByte(element); 162 checkRange(index); 163 incrModCount(); 164 short oldval = toShort(_data[index]); 165 _data[index] = fromShort(element); 166 return oldval; 167 } 168 169 186 public void add(int index, short element) { 187 assertValidUnsignedByte(element); 188 checkRangeIncludingEndpoint(index); 189 incrModCount(); 190 ensureCapacity(_size+1); 191 int numtomove = _size-index; 192 System.arraycopy(_data,index,_data,index+1,numtomove); 193 _data[index] = fromShort(element); 194 _size++; 195 } 196 197 public void clear() { 198 incrModCount(); 199 _size = 0; 200 } 201 202 205 210 public void ensureCapacity(int mincap) { 211 incrModCount(); 212 if(mincap > _data.length) { 213 int newcap = (_data.length * 3)/2 + 1; 214 byte[] olddata = _data; 215 _data = new byte[newcap < mincap ? mincap : newcap]; 216 System.arraycopy(olddata,0,_data,0,_size); 217 } 218 } 219 220 224 public void trimToSize() { 225 incrModCount(); 226 if(_size < _data.length) { 227 byte[] olddata = _data; 228 _data = new byte[_size]; 229 System.arraycopy(olddata,0,_data,0,_size); 230 } 231 } 232 233 236 private final short toShort(byte value) { 237 return (short)(value & MAX_VALUE); 238 } 239 240 private final byte fromShort(short value) { 241 return (byte)(value&MAX_VALUE); 242 } 243 244 private final void assertValidUnsignedByte(short value) throws IllegalArgumentException { 245 if(value > MAX_VALUE) { 246 throw new IllegalArgumentException (value + " > " + MAX_VALUE); 247 } 248 if(value < MIN_VALUE) { 249 throw new IllegalArgumentException (value + " < " + MIN_VALUE); 250 } 251 } 252 253 private void writeObject(ObjectOutputStream out) throws IOException { 254 out.defaultWriteObject(); 255 out.writeInt(_data.length); 256 for(int i=0;i<_size;i++) { 257 out.writeByte(_data[i]); 258 } 259 } 260 261 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 262 in.defaultReadObject(); 263 _data = new byte[in.readInt()]; 264 for(int i=0;i<_size;i++) { 265 _data[i] = in.readByte(); 266 } 267 } 268 269 private final void checkRange(int index) { 270 if(index < 0 || index >= _size) { 271 throw new IndexOutOfBoundsException ("Should be at least 0 and less than " + _size + ", found " + index); 272 } 273 } 274 275 private final void checkRangeIncludingEndpoint(int index) { 276 if(index < 0 || index > _size) { 277 throw new IndexOutOfBoundsException ("Should be at least 0 and at most " + _size + ", found " + index); 278 } 279 } 280 281 private transient byte[] _data = null; 282 private int _size = 0; 283 284 287 public static final short MAX_VALUE = 0xFF; 288 289 292 public static final short MIN_VALUE = 0; 293 294 } 295 | Popular Tags |