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 * ShapeList.java
029 * --------------
030 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributors:     -;
034 */
035
036package org.jfree.chart.util;
037
038import java.awt.Shape;
039import java.io.IOException;
040import java.io.ObjectInputStream;
041import java.io.ObjectOutputStream;
042
043/**
044 * A table of {@link Shape} objects.
045 */
046public class ShapeList extends AbstractObjectList {
047
048    /**
049     * Creates a new list.
050     */
051    public ShapeList() {
052        super();
053    }
054
055    /**
056     * Returns a {@link Shape} object from the list.
057     *
058     * @param index the index (zero-based).
059     *
060     * @return The object.
061     */
062    public Shape getShape(int index) {
063        return (Shape) get(index);
064    }
065
066    /**
067     * Sets the {@link Shape} for an item in the list.  The list is expanded
068     * if necessary.
069     *
070     * @param index  the index (zero-based).
071     * @param shape  the {@link Shape}.
072     */
073    public void setShape(int index, Shape shape) {
074        set(index, shape);
075    }
076
077    /**
078     * Returns an independent copy of the list.
079     *
080     * @return A clone.
081     *
082     * @throws CloneNotSupportedException if an item in the list does not
083     *         support cloning.
084     */
085    @Override
086    public Object clone() throws CloneNotSupportedException {
087        return super.clone();
088    }
089
090    /**
091     * Tests the list for equality with another object (typically also a list).
092     *
093     * @param obj  the other object ({@code null} permitted).
094     *
095     * @return A boolean.
096     */
097    @Override
098    public boolean equals(Object obj) {
099
100        if (obj == this) {
101            return true;
102        }
103        if (!(obj instanceof ShapeList)) {
104            return false;
105        }
106        ShapeList that = (ShapeList) obj;
107        int listSize = size();
108        for (int i = 0; i < listSize; i++) {
109           if (!ShapeUtils.equal((Shape) get(i), (Shape) that.get(i))) {
110               return false;
111           }
112        }
113        return true;
114
115    }
116
117    /**
118     * Returns a hash code value for the object.
119     *
120     * @return the hashcode
121     */
122    @Override
123    public int hashCode() {
124        return super.hashCode();
125    }
126
127    /**
128     * Provides serialization support.
129     *
130     * @param stream  the output stream.
131     *
132     * @throws IOException  if there is an I/O error.
133     */
134    private void writeObject(ObjectOutputStream stream) throws IOException {
135
136        stream.defaultWriteObject();
137        final int count = size();
138        stream.writeInt(count);
139        for (int i = 0; i < count; i++) {
140            final Shape shape = getShape(i);
141            if (shape != null) {
142                stream.writeInt(i);
143                SerialUtils.writeShape(shape, stream);
144            }
145            else {
146                stream.writeInt(-1);
147            }
148        }
149
150    }
151
152    /**
153     * Provides serialization support.
154     *
155     * @param stream  the input stream.
156     *
157     * @throws IOException  if there is an I/O error.
158     * @throws ClassNotFoundException  if there is a classpath problem.
159     */
160    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
161
162        stream.defaultReadObject();
163        final int count = stream.readInt();
164        for (int i = 0; i < count; i++) {
165            final int index = stream.readInt();
166            if (index != -1) {
167                setShape(index, SerialUtils.readShape(stream));
168            }
169        }
170
171    }
172
173}
174
175