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 * StrokeMap.java
029 * --------------
030 * (C) Copyright 2006-2016, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 27-Sep-2006 : Version 1 (DG);
038 * 02-Jul-2013 : Use ParamChecks (DG);
039 *
040 */
041
042package org.jfree.chart;
043
044import java.awt.Stroke;
045import java.io.IOException;
046import java.io.ObjectInputStream;
047import java.io.ObjectOutputStream;
048import java.io.Serializable;
049import java.util.Iterator;
050import java.util.Map;
051import java.util.Set;
052import java.util.TreeMap;
053import org.jfree.chart.util.ObjectUtils;
054import org.jfree.chart.util.Args;
055import org.jfree.chart.util.SerialUtils;
056
057/**
058 * A storage structure that maps {@code Comparable} instances with
059 * {@code Stroke} instances.
060 * <br><br>
061 * To support cloning and serialization, you should only use keys that are
062 * cloneable and serializable.  Special handling for the {@code Stroke}
063 * instances is included in this class.
064 *
065 * @since 1.0.3
066 */
067public class StrokeMap implements Cloneable, Serializable {
068
069    /** For serialization. */
070    static final long serialVersionUID = -8148916785963525169L;
071
072    /** Storage for the keys and values. */
073    private transient Map store;
074
075    /**
076     * Creates a new (empty) map.
077     */
078    public StrokeMap() {
079        this.store = new TreeMap();
080    }
081
082    /**
083     * Returns the stroke associated with the specified key, or
084     * {@code null}.
085     *
086     * @param key  the key ({@code null} not permitted).
087     *
088     * @return The stroke, or {@code null}.
089     *
090     * @throws IllegalArgumentException if {@code key} is
091     *     {@code null}.
092     */
093    public Stroke getStroke(Comparable key) {
094        Args.nullNotPermitted(key, "key");
095        return (Stroke) this.store.get(key);
096    }
097
098    /**
099     * Returns {@code true} if the map contains the specified key, and
100     * {@code false} otherwise.
101     *
102     * @param key  the key.
103     *
104     * @return {@code true} if the map contains the specified key, and
105     * {@code false} otherwise.
106     */
107    public boolean containsKey(Comparable key) {
108        return this.store.containsKey(key);
109    }
110
111    /**
112     * Adds a mapping between the specified {@code key} and
113     * {@code stroke} values.
114     *
115     * @param key  the key ({@code null} not permitted).
116     * @param stroke  the stroke.
117     */
118    public void put(Comparable key, Stroke stroke) {
119        Args.nullNotPermitted(key, "key");
120        this.store.put(key, stroke);
121    }
122
123    /**
124     * Resets the map to empty.
125     */
126    public void clear() {
127        this.store.clear();
128    }
129
130    /**
131     * Tests this map for equality with an arbitrary object.
132     *
133     * @param obj  the object ({@code null} permitted).
134     *
135     * @return A boolean.
136     */
137    @Override
138    public boolean equals(Object obj) {
139        if (obj == this) {
140            return true;
141        }
142        if (!(obj instanceof StrokeMap)) {
143            return false;
144        }
145        StrokeMap that = (StrokeMap) obj;
146        if (this.store.size() != that.store.size()) {
147            return false;
148        }
149        Set keys = this.store.keySet();
150        Iterator iterator = keys.iterator();
151        while (iterator.hasNext()) {
152            Comparable key = (Comparable) iterator.next();
153            Stroke s1 = getStroke(key);
154            Stroke s2 = that.getStroke(key);
155            if (!ObjectUtils.equal(s1, s2)) {
156                return false;
157            }
158        }
159        return true;
160    }
161
162    /**
163     * Returns a clone of this {@code StrokeMap}.
164     *
165     * @return A clone of this instance.
166     *
167     * @throws CloneNotSupportedException if any key is not cloneable.
168     */
169    @Override
170    public Object clone() throws CloneNotSupportedException {
171        StrokeMap clone = (StrokeMap) super.clone();
172        clone.store = new TreeMap();
173        clone.store.putAll(this.store);
174        // TODO: I think we need to make sure the keys are actually cloned,
175        // whereas the stroke instances are always immutable so they're OK
176        return clone;
177    }
178
179    /**
180     * Provides serialization support.
181     *
182     * @param stream  the output stream.
183     *
184     * @throws IOException  if there is an I/O error.
185     */
186    private void writeObject(ObjectOutputStream stream) throws IOException {
187        stream.defaultWriteObject();
188        stream.writeInt(this.store.size());
189        Set keys = this.store.keySet();
190        Iterator iterator = keys.iterator();
191        while (iterator.hasNext()) {
192            Comparable key = (Comparable) iterator.next();
193            stream.writeObject(key);
194            Stroke stroke = getStroke(key);
195            SerialUtils.writeStroke(stroke, stream);
196        }
197    }
198
199    /**
200     * Provides serialization support.
201     *
202     * @param stream  the input stream.
203     *
204     * @throws IOException  if there is an I/O error.
205     * @throws ClassNotFoundException  if there is a classpath problem.
206     */
207    private void readObject(ObjectInputStream stream)
208            throws IOException, ClassNotFoundException {
209        stream.defaultReadObject();
210        this.store = new TreeMap();
211        int keyCount = stream.readInt();
212        for (int i = 0; i < keyCount; i++) {
213            Comparable key = (Comparable) stream.readObject();
214            Stroke stroke = SerialUtils.readStroke(stream);
215            this.store.put(key, stroke);
216        }
217    }
218
219}