001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.util;
030
031import java.io.IOException;
032import java.io.ObjectInputStream;
033import java.io.ObjectOutputStream;
034import java.io.Serializable;
035import java.util.Arrays;
036
037/**
038 * A list of objects that can grow as required.
039 */
040public class AbstractObjectList implements Cloneable, Serializable {
041
042    /** For serialization. */
043    private static final long serialVersionUID = 7789833772597351595L;
044    
045    /** The default initial capacity of the list. */
046    public static final int DEFAULT_INITIAL_CAPACITY = 8;
047
048    /** Storage for the objects. */
049    private transient Object[] objects;
050
051    /** The current list size. */
052    private int size = 0;
053
054    /** The default increment. */
055    private int increment = DEFAULT_INITIAL_CAPACITY;
056
057    /**
058     * Creates a new list with the default initial capacity.
059     */
060    protected AbstractObjectList() {
061        this(DEFAULT_INITIAL_CAPACITY);
062    }
063
064    /**
065     * Creates a new list.
066     *
067     * @param initialCapacity  the initial capacity.
068     */
069    protected AbstractObjectList(int initialCapacity) {
070        this (initialCapacity, initialCapacity);
071    }
072
073    /**
074     * Creates a new list.
075     * 
076     * @param initialCapacity  the initial capacity.
077     * @param increment  the increment.
078     */
079    protected AbstractObjectList(int initialCapacity, int increment) {
080        this.objects = new Object[initialCapacity];
081        this.increment = increment;
082    }
083
084    /**
085     * Returns the object at the specified index, if there is one, or 
086     * {@code null}.
087     *
088     * @param index  the object index.
089     *
090     * @return The object or {@code null}.
091     */
092    protected Object get(int index) {
093        Object result = null;
094        if (index >= 0 && index < this.size) {
095            result = this.objects[index];
096        }
097        return result;
098    }
099
100    /**
101     * Sets an object reference (overwriting any existing object).
102     *
103     * @param index  the object index.
104     * @param object  the object ({@code null} permitted).
105     */
106    protected void set(int index, Object object) {
107        if (index < 0) {
108            throw new IllegalArgumentException("Requires index >= 0.");
109        }
110        if (index >= this.objects.length) {
111            Object[] enlarged = new Object[index + this.increment];
112            System.arraycopy(this.objects, 0, enlarged, 0, this.objects.length);
113            this.objects = enlarged;
114        }
115        this.objects[index] = object;
116        this.size = Math.max(this.size, index + 1);
117    }
118
119    /**
120     * Clears the list.
121     */
122    public void clear() {
123        Arrays.fill(this.objects, null);
124        this.size = 0;
125    }
126
127    /**
128     * Returns the size of the list.
129     *
130     * @return The size of the list.
131     */
132    public int size() {
133        return this.size;
134    }
135
136    /**
137     * Returns the index of the specified object, or -1 if the object is not in
138     * the list.
139     *
140     * @param object  the object.
141     *
142     * @return The index or -1.
143     */
144    protected int indexOf(Object object) {
145        for (int index = 0; index < this.size; index++) {
146            if (this.objects[index] == object) {
147                return (index);
148            }
149        }
150        return -1;
151    }
152
153    /**
154     * Tests this list for equality with another object.
155     *
156     * @param obj  the object to test.
157     * 
158     * @return A boolean.
159     */
160    @Override
161    public boolean equals(Object obj) {
162
163        if (obj == null) {
164            return false;
165        }
166
167        if (obj == this) {
168            return true;
169        }
170
171        if (!(obj instanceof AbstractObjectList)) {
172            return false;
173        }
174
175        final AbstractObjectList other = (AbstractObjectList) obj;
176        final int listSize = size();
177        for (int i = 0; i < listSize; i++) {
178           if (!ObjectUtils.equal(get(i), other.get(i))) {
179               return false;
180           }
181        }
182        return true;
183    }
184
185    /**
186     * Returns a hash code value for the object.
187     *
188     * @return the hashcode
189     */
190    @Override
191    public int hashCode() {
192        return super.hashCode();
193    }
194
195    /**
196     * Clones the list of objects.  The objects in the list are not cloned, so 
197     * this is method makes a 'shallow' copy of the list.
198     *
199     * @return A clone.
200     * 
201     * @throws CloneNotSupportedException if an item in the list does not 
202     *         support cloning.
203     */
204    @Override
205    public Object clone() throws CloneNotSupportedException {
206
207        final AbstractObjectList clone = (AbstractObjectList) super.clone();
208        if (this.objects != null) {
209            clone.objects = new Object[this.objects.length];
210            System.arraycopy(
211                this.objects, 0, clone.objects, 0, this.objects.length
212            );
213        }
214        return clone;
215
216    }
217
218    /**
219     * Provides serialization support.
220     *
221     * @param stream  the output stream.
222     *
223     * @throws IOException  if there is an I/O error.
224     */
225    private void writeObject(ObjectOutputStream stream) 
226        throws IOException {
227
228        stream.defaultWriteObject();
229        final int count = size();
230        stream.writeInt(count);
231        for (int i = 0; i < count; i++) {
232            final Object object = get(i);
233            if (object != null && object instanceof Serializable) {
234                stream.writeInt(i);
235                stream.writeObject(object);
236            }
237            else {
238                stream.writeInt(-1);
239            }
240        }
241
242    }
243    
244    /**
245     * Provides serialization support.
246     *
247     * @param stream  the input stream.
248     *
249     * @throws IOException  if there is an I/O error.
250     * @throws ClassNotFoundException  if there is a classpath problem.
251     */
252    private void readObject(ObjectInputStream stream) 
253        throws IOException, ClassNotFoundException {
254
255        stream.defaultReadObject();
256        this.objects = new Object[this.size];
257        final int count = stream.readInt();
258        for (int i = 0; i < count; i++) {
259            final int index = stream.readInt();
260            if (index != -1) {
261                set(index, stream.readObject());
262            }
263        }
264        
265    }
266  
267}
268