Source for org.jfree.chart.StrokeMap

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