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