KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > PaintMap


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  * PaintMap.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: PaintMap.java,v 1.1.2.1 2006/09/27 17:06:58 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.Paint JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutputStream JavaDoc;
49 import java.io.Serializable JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Set JavaDoc;
53 import java.util.TreeMap JavaDoc;
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 JavaDoc, Serializable JavaDoc {
69
70     /** Storage for the keys and values. */
71     private transient Map JavaDoc store;
72     
73     /**
74      * Creates a new (empty) map.
75      */

76     public PaintMap() {
77         this.store = new TreeMap JavaDoc();
78     }
79     
80     /**
81      * Returns the paint 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 paint, or <code>null</code>.
87      *
88      * @throws IllegalArgumentException if <code>key</code> is
89      * <code>null</code>.
90      */

91     public Paint JavaDoc getPaint(Comparable JavaDoc key) {
92         if (key == null) {
93             throw new IllegalArgumentException JavaDoc("Null 'key' argument.");
94         }
95         return (Paint JavaDoc) 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 JavaDoc key) {
108         return this.store.containsKey(key);
109     }
110     
111     /**
112      * Adds a mapping between the specified <code>key</code> and
113      * <code>paint</code> values.
114      *
115      * @param key the key (<code>null</code> not permitted).
116      * @param paint the paint.
117      *
118      * @throws IllegalArgumentException if <code>key</code> is
119      * <code>null</code>.
120      */

121     public void put(Comparable JavaDoc key, Paint JavaDoc paint) {
122         if (key == null) {
123             throw new IllegalArgumentException JavaDoc("Null 'key' argument.");
124         }
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</code> permitted).
139      *
140      * @return A boolean.
141      */

142     public boolean equals(Object JavaDoc obj) {
143         if (obj == this) {
144             return true;
145         }
146         if (!(obj instanceof PaintMap)) {
147             return false;
148         }
149         PaintMap that = (PaintMap) obj;
150         if (this.store.size() != that.store.size()) {
151             return false;
152         }
153         Set JavaDoc keys = this.store.keySet();
154         Iterator JavaDoc iterator = keys.iterator();
155         while (iterator.hasNext()) {
156             Comparable JavaDoc key = (Comparable JavaDoc) iterator.next();
157             Paint JavaDoc p1 = getPaint(key);
158             Paint JavaDoc p2 = that.getPaint(key);
159             if (!PaintUtilities.equal(p1, p2)) {
160                 return false;
161             }
162         }
163         return true;
164     }
165     
166     /**
167      * Returns a clone of this <code>PaintMap</code>.
168      *
169      * @return A clone of this instance.
170      *
171      * @throws CloneNotSupportedException if any key is not cloneable.
172      */

173     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
174         // TODO: I think we need to make sure the keys are actually cloned,
175
// whereas the paint instances are always immutable so they're OK
176
return super.clone();
177     }
178     
179     /**
180      * Provides serialization support.
181      *
182      * @param stream the output stream.
183      *
184      * @throws IOException if there is an I/O error.
185      */

186     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
187         stream.defaultWriteObject();
188         stream.writeInt(this.store.size());
189         Set JavaDoc keys = this.store.keySet();
190         Iterator JavaDoc iterator = keys.iterator();
191         while (iterator.hasNext()) {
192             Comparable JavaDoc key = (Comparable JavaDoc) iterator.next();
193             stream.writeObject(key);
194             Paint JavaDoc paint = getPaint(key);
195             SerialUtilities.writePaint(paint, stream);
196         }
197     }
198
199     /**
200      * Provides serialization support.
201      *
202      * @param stream the input stream.
203      *
204      * @throws IOException if there is an I/O error.
205      * @throws ClassNotFoundException if there is a classpath problem.
206      */

207     private void readObject(ObjectInputStream JavaDoc stream)
208             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
209         stream.defaultReadObject();
210         this.store = new TreeMap JavaDoc();
211         int keyCount = stream.readInt();
212         for (int i = 0; i < keyCount; i++) {
213             Comparable JavaDoc key = (Comparable JavaDoc) stream.readObject();
214             Paint JavaDoc paint = SerialUtilities.readPaint(stream);
215             this.store.put(key, paint);
216         }
217     }
218     
219 }
220
Popular Tags