1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* -----------------------
28
* AbstractObjectList.java
29
* -----------------------
30
* (C)opyright 2003, 2004, by Object Refinery Limited and Contributors.
32
* Original Author: David Gilbert (for Object Refinery Limited);
33
* Contributor(s): Bill Kelemen;
36
* $Id: AbstractObjectList.java,v 1.5 2005/10/18 13:24:19 mungady Exp $
40
* 13-Aug-2003 : Version 1, based on ObjectList (DG);
41
* 24-Aug-2003 : Fixed size (BK);
42
* 15-Sep-2003 : Fix serialization for subclasses (ShapeList, PaintList) (NB);
45
package org.jfree.util;
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;
54
* A list of objects that can grow as required.
56
* @author David Gilbert
58
public class AbstractObjectList implements Cloneable, Serializable {
60
/** For serialization. */
61
private static final long serialVersionUID = 7789833772597351595L;
63
/** The default initial capacity of the list. */
64
public static final int DEFAULT_INITIAL_CAPACITY = 8;
66
/** Storage for the objects. */
67
private transient Object[] objects;
69
/** The current list size. */
72
/** The default increment. */
73
private int increment = DEFAULT_INITIAL_CAPACITY;
76
* Creates a new list with the default initial capacity.
78
protected AbstractObjectList() {
79
this(DEFAULT_INITIAL_CAPACITY);
85
* @param initialCapacity the initial capacity.
87
protected AbstractObjectList(final int initialCapacity) {
88
this (initialCapacity, initialCapacity);
94
* @param initialCapacity the initial capacity.
95
* @param increment the increment.
97
protected AbstractObjectList(final int initialCapacity,
98
final int increment) {
99
this.objects = new Object[initialCapacity];
100
this.increment = increment;
104
* Returns the object at the specified index, if there is one, or
107
* @param index the object index.
109
* @return The object or <code>null</code>.
111
protected Object get(final int index) {
112
Object result = null;
113
if (index >= 0 && index < this.size) {
114
result = this.objects[index];
120
* Sets an object reference (overwriting any existing object).
122
* @param index the object index.
123
* @param object the object (<code>null</code> permitted).
125
protected void set(final int index, final Object object) {
127
throw new IllegalArgumentException("Requires index >= 0.");
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;
134
this.objects[index] = object;
135
this.size = Math.max(this.size, index + 1);
141
public void clear() {
142
Arrays.fill(this.objects, null);
147
* Returns the size of the list.
149
* @return The size of the list.
156
* Returns the index of the specified object, or -1 if the object is not in
159
* @param object the object.
161
* @return The index or -1.
163
protected int indexOf(final Object object) {
164
for (int index = 0; index < this.size; index++) {
165
if (this.objects[index] == object) {
173
* Tests this list for equality with another object.
175
* @param obj the object to test.
179
public boolean equals(final Object obj) {
189
if (!(obj instanceof AbstractObjectList)) {
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))) {
204
* Returns a hash code value for the object.
206
* @return the hashcode
208
public int hashCode() {
209
return super.hashCode();
213
* Clones the list of objects. The objects in the list are not cloned, so
214
* this is method makes a 'shallow' copy of the list.
218
* @throws CloneNotSupportedException if an item in the list does not
221
public Object clone() throws CloneNotSupportedException {
223
final AbstractObjectList clone = (AbstractObjectList) super.clone();
224
if (this.objects != null) {
225
clone.objects = new Object[this.objects.length];
227
this.objects, 0, clone.objects, 0, this.objects.length
235
* Provides serialization support.
237
* @param stream the output stream.
239
* @throws IOException if there is an I/O error.
241
private void writeObject(final ObjectOutputStream stream)
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) {
251
stream.writeObject(object);
261
* Provides serialization support.
263
* @param stream the input stream.
265
* @throws IOException if there is an I/O error.
266
* @throws ClassNotFoundException if there is a classpath problem.
268
private void readObject(final ObjectInputStream stream)
269
throws IOException, ClassNotFoundException {
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();
277
set(index, stream.readObject());