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