Source for org.jfree.chart.PaintMap

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