001    /* ======================================================
002     * Orson : a free chart beans library based on JFreeChart
003     * ======================================================
004     *
005     * (C) Copyright 2007, by Object Refinery Limited.
006     *
007     * Project Info:  not-yet-released
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     * 
027     */
028    
029    package org.jfree.beans;
030    
031    import java.awt.Paint;
032    import java.io.Serializable;
033    
034    import org.jfree.util.ObjectUtilities;
035    import org.jfree.util.PaintUtilities;
036    import org.jfree.util.PublicCloneable;
037    
038    /**
039     * A (key, paint) pair for use by the {@link JPieChart} class (and possibly 
040     * others later).
041     */
042    public class KeyedPaint implements Cloneable, PublicCloneable, Serializable {
043    
044        /** For serialization. */
045        private static final long serialVersionUID = 0L;
046        
047        /** The key. */
048        private Comparable key;
049    
050        /** The paint. */
051        private Paint paint;
052    
053        /**
054         * Creates a new (key, paint) pair.
055         *
056         * @param key  the key.
057         * @param paint  the paint (<code>null</code> permitted).
058         */
059        public KeyedPaint(Comparable key, Paint paint) {
060            this.key = key;
061            this.paint = paint;
062        }
063    
064        /**
065         * Returns the key.
066         *
067         * @return The key.
068         */
069        public Comparable getKey() {
070            return this.key;
071        }
072    
073        /**
074         * Returns the paint.
075         *
076         * @return The paint (possibly <code>null</code>).
077         */
078        public Paint getPaint() {
079            return this.paint;
080        }
081    
082        /**
083         * Sets the paint.
084         *
085         * @param paint  the paint (<code>null</code> permitted).
086         */
087        public void setPaint(Paint paint) {
088            this.paint = paint;
089        }
090        
091        /**
092         * Returns a clone of this object.  It is assumed that the key is an 
093         * immutable object, so it is not deep-cloned.  The object is deep-cloned 
094         * if it implements {@link PublicCloneable}, otherwise a shallow clone is 
095         * made.
096         * 
097         * @return A clone.
098         * 
099         * @throws CloneNotSupportedException if there is a problem cloning.
100         */
101        public Object clone() throws CloneNotSupportedException {
102            KeyedPaint clone = (KeyedPaint) super.clone();
103            return clone;      
104        }
105        
106        /**
107         * Tests if this object is equal to another.
108         *
109         * @param obj  the other object.
110         *
111         * @return A boolean.
112         */
113        public boolean equals(Object obj) {
114    
115            if (obj == this) {
116                return true;
117            }
118    
119            if (!(obj instanceof KeyedPaint)) {
120                return false;
121            }
122            KeyedPaint that = (KeyedPaint) obj;
123            if (!ObjectUtilities.equal(this.key, that.key)) {
124                return false;
125            }
126    
127            if (!PaintUtilities.equal(this.paint, that.paint)) {
128                return false;
129            }
130    
131            return true;
132        }
133        
134    }