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