001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2021, 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;
036import java.util.Objects;
037
038/**
039 * A list of objects that can grow as required.
040 */
041public class AbstractObjectList implements Cloneable, Serializable {
042
043    /** For serialization. */
044    private static final long serialVersionUID = 7789833772597351595L;
045    
046    /** The default initial capacity of the list. */
047    public static final int DEFAULT_INITIAL_CAPACITY = 8;
048
049    /** Storage for the objects. */
050    private transient Object[] objects;
051
052    /** The current list size. */
053    private int size = 0;
054
055    /** The default increment. */
056    private int increment = DEFAULT_INITIAL_CAPACITY;
057
058    /**
059     * Creates a new list with the default initial capacity.
060     */
061    protected AbstractObjectList() {
062        this(DEFAULT_INITIAL_CAPACITY);
063    }
064
065    /**
066     * Creates a new list.
067     *
068     * @param initialCapacity  the initial capacity.
069     */
070    protected AbstractObjectList(int initialCapacity) {
071        this (initialCapacity, initialCapacity);
072    }
073
074    /**
075     * Creates a new list.
076     * 
077     * @param initialCapacity  the initial capacity.
078     * @param increment  the increment.
079     */
080    protected AbstractObjectList(int initialCapacity, int increment) {
081        this.objects = new Object[initialCapacity];
082        this.increment = increment;
083    }
084
085    /**
086     * Returns the object at the specified index, if there is one, or 
087     * {@code null}.
088     *
089     * @param index  the object index.
090     *
091     * @return The object or {@code null}.
092     */
093    protected Object get(int index) {
094        Object result = null;
095        if (index >= 0 && index < this.size) {
096            result = this.objects[index];
097        }
098        return result;
099    }
100
101    /**
102     * Sets an object reference (overwriting any existing object).
103     *
104     * @param index  the object index.
105     * @param object  the object ({@code null} permitted).
106     */
107    protected void set(int index, Object object) {
108        if (index < 0) {
109            throw new IllegalArgumentException("Requires index >= 0.");
110        }
111        if (index >= this.objects.length) {
112            Object[] enlarged = new Object[index + this.increment];
113            System.arraycopy(this.objects, 0, enlarged, 0, this.objects.length);
114            this.objects = enlarged;
115        }
116        this.objects[index] = object;
117        this.size = Math.max(this.size, index + 1);
118    }
119
120    /**
121     * Clears the list.
122     */
123    public void clear() {
124        Arrays.fill(this.objects, null);
125        this.size = 0;
126    }
127
128    /**
129     * Returns the size of the list.
130     *
131     * @return The size of the list.
132     */
133    public int size() {
134        return this.size;
135    }
136
137    /**
138     * Returns the index of the specified object, or -1 if the object is not in
139     * the list.
140     *
141     * @param object  the object.
142     *
143     * @return The index or -1.
144     */
145    protected int indexOf(Object object) {
146        for (int index = 0; index < this.size; index++) {
147            if (this.objects[index] == object) {
148                return (index);
149            }
150        }
151        return -1;
152    }
153
154    /**
155     * Tests this list for equality with another object.
156     *
157     * @param obj  the object to test.
158     * 
159     * @return A boolean.
160     */
161    @Override
162    public boolean equals(Object obj) {
163
164        if (obj == null) {
165            return false;
166        }
167
168        if (obj == this) {
169            return true;
170        }
171
172        if (!(obj instanceof AbstractObjectList)) {
173            return false;
174        }
175
176        final AbstractObjectList other = (AbstractObjectList) obj;
177        final int listSize = size();
178        for (int i = 0; i < listSize; i++) {
179           if (!Objects.equals(get(i), other.get(i))) {
180               return false;
181           }
182        }
183        return true;
184    }
185
186    /**
187     * Returns a hash code value for the object.
188     *
189     * @return the hashcode
190     */
191    @Override
192    public int hashCode() {
193        return super.hashCode();
194    }
195
196    /**
197     * Clones the list of objects.  The objects in the list are not cloned, so 
198     * this is method makes a 'shallow' copy of the list.
199     *
200     * @return A clone.
201     * 
202     * @throws CloneNotSupportedException if an item in the list does not 
203     *         support cloning.
204     */
205    @Override
206    public Object clone() throws CloneNotSupportedException {
207
208        final AbstractObjectList clone = (AbstractObjectList) super.clone();
209        if (this.objects != null) {
210            clone.objects = new Object[this.objects.length];
211            System.arraycopy(
212                this.objects, 0, clone.objects, 0, this.objects.length
213            );
214        }
215        return clone;
216
217    }
218
219    /**
220     * Provides serialization support.
221     *
222     * @param stream  the output stream.
223     *
224     * @throws IOException  if there is an I/O error.
225     */
226    private void writeObject(ObjectOutputStream stream) 
227        throws IOException {
228
229        stream.defaultWriteObject();
230        final int count = size();
231        stream.writeInt(count);
232        for (int i = 0; i < count; i++) {
233            final Object object = get(i);
234            if (object != null && object instanceof Serializable) {
235                stream.writeInt(i);
236                stream.writeObject(object);
237            }
238            else {
239                stream.writeInt(-1);
240            }
241        }
242
243    }
244    
245    /**
246     * Provides serialization support.
247     *
248     * @param stream  the input stream.
249     *
250     * @throws IOException  if there is an I/O error.
251     * @throws ClassNotFoundException  if there is a classpath problem.
252     */
253    private void readObject(ObjectInputStream stream) 
254        throws IOException, ClassNotFoundException {
255
256        stream.defaultReadObject();
257        this.objects = new Object[this.size];
258        final int count = stream.readInt();
259        for (int i = 0; i < count; i++) {
260            final int index = stream.readInt();
261            if (index != -1) {
262                set(index, stream.readObject());
263            }
264        }
265        
266    }
267  
268}
269