Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------- 28: * PaintMap.java 29: * ------------- 30: * (C) Copyright 2006, 2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes: 36: * -------- 37: * 27-Sep-2006 : Version 1 (DG); 38: * 17-Jan-2007 : Changed TreeMap to HashMap, so that different classes that 39: * implement Comparable can be used as keys (DG); 40: * 41: */ 42: 43: package org.jfree.chart; 44: 45: import java.awt.Paint; 46: import java.io.IOException; 47: import java.io.ObjectInputStream; 48: import java.io.ObjectOutputStream; 49: import java.io.Serializable; 50: import java.util.HashMap; 51: import java.util.Iterator; 52: import java.util.Map; 53: import java.util.Set; 54: 55: import org.jfree.io.SerialUtilities; 56: import org.jfree.util.PaintUtilities; 57: 58: /** 59: * A storage structure that maps <code>Comparable</code> instances with 60: * <code>Paint</code> instances. 61: * <br><br> 62: * To support cloning and serialization, you should only use keys that are 63: * cloneable and serializable. Special handling for the <code>Paint</code> 64: * instances is included in this class. 65: * 66: * @since 1.0.3 67: */ 68: public class PaintMap implements Cloneable, Serializable { 69: 70: /** For serialization. */ 71: static final long serialVersionUID = -4639833772123069274L; 72: 73: /** Storage for the keys and values. */ 74: private transient Map store; 75: 76: /** 77: * Creates a new (empty) map. 78: */ 79: public PaintMap() { 80: this.store = new HashMap(); 81: } 82: 83: /** 84: * Returns the paint associated with the specified key, or 85: * <code>null</code>. 86: * 87: * @param key the key (<code>null</code> not permitted). 88: * 89: * @return The paint, or <code>null</code>. 90: * 91: * @throws IllegalArgumentException if <code>key</code> is 92: * <code>null</code>. 93: */ 94: public Paint getPaint(Comparable key) { 95: if (key == null) { 96: throw new IllegalArgumentException("Null 'key' argument."); 97: } 98: return (Paint) this.store.get(key); 99: } 100: 101: /** 102: * Returns <code>true</code> if the map contains the specified key, and 103: * <code>false</code> otherwise. 104: * 105: * @param key the key. 106: * 107: * @return <code>true</code> if the map contains the specified key, and 108: * <code>false</code> otherwise. 109: */ 110: public boolean containsKey(Comparable key) { 111: return this.store.containsKey(key); 112: } 113: 114: /** 115: * Adds a mapping between the specified <code>key</code> and 116: * <code>paint</code> values. 117: * 118: * @param key the key (<code>null</code> not permitted). 119: * @param paint the paint. 120: * 121: * @throws IllegalArgumentException if <code>key</code> is 122: * <code>null</code>. 123: */ 124: public void put(Comparable key, Paint paint) { 125: if (key == null) { 126: throw new IllegalArgumentException("Null 'key' argument."); 127: } 128: this.store.put(key, paint); 129: } 130: 131: /** 132: * Resets the map to empty. 133: */ 134: public void clear() { 135: this.store.clear(); 136: } 137: 138: /** 139: * Tests this map for equality with an arbitrary object. 140: * 141: * @param obj the object (<code>null</code> permitted). 142: * 143: * @return A boolean. 144: */ 145: public boolean equals(Object obj) { 146: if (obj == this) { 147: return true; 148: } 149: if (!(obj instanceof PaintMap)) { 150: return false; 151: } 152: PaintMap that = (PaintMap) obj; 153: if (this.store.size() != that.store.size()) { 154: return false; 155: } 156: Set keys = this.store.keySet(); 157: Iterator iterator = keys.iterator(); 158: while (iterator.hasNext()) { 159: Comparable key = (Comparable) iterator.next(); 160: Paint p1 = getPaint(key); 161: Paint p2 = that.getPaint(key); 162: if (!PaintUtilities.equal(p1, p2)) { 163: return false; 164: } 165: } 166: return true; 167: } 168: 169: /** 170: * Returns a clone of this <code>PaintMap</code>. 171: * 172: * @return A clone of this instance. 173: * 174: * @throws CloneNotSupportedException if any key is not cloneable. 175: */ 176: public Object clone() throws CloneNotSupportedException { 177: // TODO: I think we need to make sure the keys are actually cloned, 178: // whereas the paint instances are always immutable so they're OK 179: return super.clone(); 180: } 181: 182: /** 183: * Provides serialization support. 184: * 185: * @param stream the output stream. 186: * 187: * @throws IOException if there is an I/O error. 188: */ 189: private void writeObject(ObjectOutputStream stream) throws IOException { 190: stream.defaultWriteObject(); 191: stream.writeInt(this.store.size()); 192: Set keys = this.store.keySet(); 193: Iterator iterator = keys.iterator(); 194: while (iterator.hasNext()) { 195: Comparable key = (Comparable) iterator.next(); 196: stream.writeObject(key); 197: Paint paint = getPaint(key); 198: SerialUtilities.writePaint(paint, stream); 199: } 200: } 201: 202: /** 203: * Provides serialization support. 204: * 205: * @param stream the input stream. 206: * 207: * @throws IOException if there is an I/O error. 208: * @throws ClassNotFoundException if there is a classpath problem. 209: */ 210: private void readObject(ObjectInputStream stream) 211: throws IOException, ClassNotFoundException { 212: stream.defaultReadObject(); 213: this.store = new HashMap(); 214: int keyCount = stream.readInt(); 215: for (int i = 0; i < keyCount; i++) { 216: Comparable key = (Comparable) stream.readObject(); 217: Paint paint = SerialUtilities.readPaint(stream); 218: this.store.put(key, paint); 219: } 220: } 221: 222: }