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