1 44 45 package org.jfree.util; 46 47 import java.io.IOException ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutputStream ; 50 import java.io.Serializable ; 51 import java.util.Arrays ; 52 53 58 public class AbstractObjectList implements Cloneable , Serializable { 59 60 61 private static final long serialVersionUID = 7789833772597351595L; 62 63 64 public static final int DEFAULT_INITIAL_CAPACITY = 8; 65 66 67 private transient Object [] objects; 68 69 70 private int size = 0; 71 72 73 private int increment = DEFAULT_INITIAL_CAPACITY; 74 75 78 protected AbstractObjectList() { 79 this(DEFAULT_INITIAL_CAPACITY); 80 } 81 82 87 protected AbstractObjectList(final int initialCapacity) { 88 this (initialCapacity, initialCapacity); 89 } 90 91 97 protected AbstractObjectList(final int initialCapacity, 98 final int increment) { 99 this.objects = new Object [initialCapacity]; 100 this.increment = increment; 101 } 102 103 111 protected Object get(final int index) { 112 Object result = null; 113 if (index >= 0 && index < this.size) { 114 result = this.objects[index]; 115 } 116 return result; 117 } 118 119 125 protected void set(final int index, final Object object) { 126 if (index < 0) { 127 throw new IllegalArgumentException ("Requires index >= 0."); 128 } 129 if (index >= this.objects.length) { 130 final Object [] enlarged = new Object [index + this.increment]; 131 System.arraycopy(this.objects, 0, enlarged, 0, this.objects.length); 132 this.objects = enlarged; 133 } 134 this.objects[index] = object; 135 this.size = Math.max(this.size, index + 1); 136 } 137 138 141 public void clear() { 142 Arrays.fill(this.objects, null); 143 this.size = 0; 144 } 145 146 151 public int size() { 152 return this.size; 153 } 154 155 163 protected int indexOf(final Object object) { 164 for (int index = 0; index < this.size; index++) { 165 if (this.objects[index] == object) { 166 return (index); 167 } 168 } 169 return -1; 170 } 171 172 179 public boolean equals(final Object obj) { 180 181 if (obj == null) { 182 return false; 183 } 184 185 if (obj == this) { 186 return true; 187 } 188 189 if (!(obj instanceof AbstractObjectList)) { 190 return false; 191 } 192 193 final AbstractObjectList other = (AbstractObjectList) obj; 194 final int listSize = size(); 195 for (int i = 0; i < listSize; i++) { 196 if (!ObjectUtilities.equal(get(i), other.get(i))) { 197 return false; 198 } 199 } 200 return true; 201 } 202 203 208 public int hashCode() { 209 return super.hashCode(); 210 } 211 212 221 public Object clone() throws CloneNotSupportedException { 222 223 final AbstractObjectList clone = (AbstractObjectList) super.clone(); 224 if (this.objects != null) { 225 clone.objects = new Object [this.objects.length]; 226 System.arraycopy( 227 this.objects, 0, clone.objects, 0, this.objects.length 228 ); 229 } 230 return clone; 231 232 } 233 234 241 private void writeObject(final ObjectOutputStream stream) 242 throws IOException { 243 244 stream.defaultWriteObject(); 245 final int count = size(); 246 stream.writeInt(count); 247 for (int i = 0; i < count; i++) { 248 final Object object = get(i); 249 if (object != null && object instanceof Serializable ) { 250 stream.writeInt(i); 251 stream.writeObject(object); 252 } 253 else { 254 stream.writeInt(-1); 255 } 256 } 257 258 } 259 260 268 private void readObject(final ObjectInputStream stream) 269 throws IOException , ClassNotFoundException { 270 271 stream.defaultReadObject(); 272 this.objects = new Object [this.size]; 273 final int count = stream.readInt(); 274 for (int i = 0; i < count; i++) { 275 final int index = stream.readInt(); 276 if (index != -1) { 277 set(index, stream.readObject()); 278 } 279 } 280 281 } 282 283 } 284 | Popular Tags |