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