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: * StrokeMap.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: * 39: */ 40: 41: package org.jfree.chart; 42: 43: import java.awt.Stroke; 44: import java.io.IOException; 45: import java.io.ObjectInputStream; 46: import java.io.ObjectOutputStream; 47: import java.io.Serializable; 48: import java.util.Iterator; 49: import java.util.Map; 50: import java.util.Set; 51: import java.util.TreeMap; 52: 53: import org.jfree.io.SerialUtilities; 54: import org.jfree.util.ObjectUtilities; 55: 56: /** 57: * A storage structure that maps <code>Comparable</code> instances with 58: * <code>Stroke</code> instances. 59: * <br><br> 60: * To support cloning and serialization, you should only use keys that are 61: * cloneable and serializable. Special handling for the <code>Stroke</code> 62: * instances is included in this class. 63: * 64: * @since 1.0.3 65: */ 66: public class StrokeMap implements Cloneable, Serializable { 67: 68: /** For serialization. */ 69: static final long serialVersionUID = -8148916785963525169L; 70: 71: /** Storage for the keys and values. */ 72: private transient Map store; 73: 74: /** 75: * Creates a new (empty) map. 76: */ 77: public StrokeMap() { 78: this.store = new TreeMap(); 79: } 80: 81: /** 82: * Returns the stroke associated with the specified key, or 83: * <code>null</code>. 84: * 85: * @param key the key (<code>null</code> not permitted). 86: * 87: * @return The stroke, or <code>null</code>. 88: * 89: * @throws IllegalArgumentException if <code>key</code> is 90: * <code>null</code>. 91: */ 92: public Stroke getStroke(Comparable key) { 93: if (key == null) { 94: throw new IllegalArgumentException("Null 'key' argument."); 95: } 96: return (Stroke) this.store.get(key); 97: } 98: 99: /** 100: * Returns <code>true</code> if the map contains the specified key, and 101: * <code>false</code> otherwise. 102: * 103: * @param key the key. 104: * 105: * @return <code>true</code> if the map contains the specified key, and 106: * <code>false</code> otherwise. 107: */ 108: public boolean containsKey(Comparable key) { 109: return this.store.containsKey(key); 110: } 111: 112: /** 113: * Adds a mapping between the specified <code>key</code> and 114: * <code>stroke</code> values. 115: * 116: * @param key the key (<code>null</code> not permitted). 117: * @param stroke the stroke. 118: */ 119: public void put(Comparable key, Stroke stroke) { 120: if (key == null) { 121: throw new IllegalArgumentException("Null 'key' argument."); 122: } 123: this.store.put(key, stroke); 124: } 125: 126: /** 127: * Resets the map to empty. 128: */ 129: public void clear() { 130: this.store.clear(); 131: } 132: 133: /** 134: * Tests this map for equality with an arbitrary object. 135: * 136: * @param obj the object (<code>null</code> permitted). 137: * 138: * @return A boolean. 139: */ 140: public boolean equals(Object obj) { 141: if (obj == this) { 142: return true; 143: } 144: if (!(obj instanceof StrokeMap)) { 145: return false; 146: } 147: StrokeMap that = (StrokeMap) obj; 148: if (this.store.size() != that.store.size()) { 149: return false; 150: } 151: Set keys = this.store.keySet(); 152: Iterator iterator = keys.iterator(); 153: while (iterator.hasNext()) { 154: Comparable key = (Comparable) iterator.next(); 155: Stroke s1 = getStroke(key); 156: Stroke s2 = that.getStroke(key); 157: if (!ObjectUtilities.equal(s1, s2)) { 158: return false; 159: } 160: } 161: return true; 162: } 163: 164: /** 165: * Returns a clone of this <code>StrokeMap</code>. 166: * 167: * @return A clone of this instance. 168: * 169: * @throws CloneNotSupportedException if any key is not cloneable. 170: */ 171: public Object clone() throws CloneNotSupportedException { 172: // TODO: I think we need to make sure the keys are actually cloned, 173: // whereas the stroke instances are always immutable so they're OK 174: return super.clone(); 175: } 176: 177: /** 178: * Provides serialization support. 179: * 180: * @param stream the output stream. 181: * 182: * @throws IOException if there is an I/O error. 183: */ 184: private void writeObject(ObjectOutputStream stream) throws IOException { 185: stream.defaultWriteObject(); 186: stream.writeInt(this.store.size()); 187: Set keys = this.store.keySet(); 188: Iterator iterator = keys.iterator(); 189: while (iterator.hasNext()) { 190: Comparable key = (Comparable) iterator.next(); 191: stream.writeObject(key); 192: Stroke stroke = getStroke(key); 193: SerialUtilities.writeStroke(stroke, stream); 194: } 195: } 196: 197: /** 198: * Provides serialization support. 199: * 200: * @param stream the input stream. 201: * 202: * @throws IOException if there is an I/O error. 203: * @throws ClassNotFoundException if there is a classpath problem. 204: */ 205: private void readObject(ObjectInputStream stream) 206: throws IOException, ClassNotFoundException { 207: stream.defaultReadObject(); 208: this.store = new TreeMap(); 209: int keyCount = stream.readInt(); 210: for (int i = 0; i < keyCount; i++) { 211: Comparable key = (Comparable) stream.readObject(); 212: Stroke stroke = SerialUtilities.readStroke(stream); 213: this.store.put(key, stroke); 214: } 215: } 216: 217: }