001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
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     * ComparableObjectItem.java
029     * -------------------------
030     * (C) Copyright 2006, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: ComparableObjectItem.java,v 1.1.2.1 2006/10/20 15:23:22 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 19-Oct-2006 : New class, based on XYDataItem (DG);
040     *
041     */
042    
043    package org.jfree.data;
044    
045    import java.io.Serializable;
046    
047    import org.jfree.util.ObjectUtilities;
048    
049    /**
050     * Represents one (Comparable, Object) data item for use in a 
051     * {@link ComparableObjectSeries}.
052     *
053     * @since 1.0.3
054     */
055    public class ComparableObjectItem implements Cloneable, Comparable, 
056            Serializable {
057    
058        private static final long serialVersionUID = 2751513470325494890L;
059        
060        /** The x-value. */
061        private Comparable x;
062    
063        /** The y-value. */
064        private Object obj;
065    
066        /**
067         * Constructs a new data item.
068         *
069         * @param x  the x-value (<code>null</code> NOT permitted).
070         * @param y  the y-value (<code>null</code> permitted).
071         */
072        public ComparableObjectItem(Comparable x, Object y) {
073            if (x == null) {
074                throw new IllegalArgumentException("Null 'x' argument.");
075            }
076            this.x = x;
077            this.obj = y;
078        }
079    
080        /**
081         * Returns the x-value.
082         *
083         * @return The x-value (never <code>null</code>).
084         */
085        protected Comparable getComparable() {
086            return this.x;
087        }
088    
089        /**
090         * Returns the y-value.
091         *
092         * @return The y-value (possibly <code>null</code>).
093         */
094        protected Object getObject() {
095            return this.obj;
096        }
097    
098        /**
099         * Sets the y-value for this data item.  Note that there is no 
100         * corresponding method to change the x-value.
101         *
102         * @param y  the new y-value (<code>null</code> permitted).
103         */
104        protected void setObject(Object y) {
105            this.obj = y;
106        }
107    
108        /**
109         * Returns an integer indicating the order of this object relative to 
110         * another object.
111         * <P>
112         * For the order we consider only the x-value:
113         * negative == "less-than", zero == "equal", positive == "greater-than".
114         *
115         * @param o1  the object being compared to.
116         *
117         * @return An integer indicating the order of this data pair object
118         *      relative to another object.
119         */
120        public int compareTo(Object o1) {
121    
122            int result;
123    
124            // CASE 1 : Comparing to another ComparableObjectItem object
125            // ---------------------------------------------------------
126            if (o1 instanceof ComparableObjectItem) {
127                ComparableObjectItem that = (ComparableObjectItem) o1;
128                return this.x.compareTo(that.x);
129            }
130    
131            // CASE 2 : Comparing to a general object
132            // ---------------------------------------------
133            else {
134                // consider these to be ordered after general objects
135                result = 1;
136            }
137    
138            return result;
139    
140        }
141    
142        /**
143         * Returns a clone of this object.
144         *
145         * @return A clone.
146         * 
147         * @throws CloneNotSupportedException not thrown by this class, but 
148         *         subclasses may differ.
149         */
150        public Object clone() throws CloneNotSupportedException {
151            return super.clone();
152        }
153        
154        /**
155         * Tests if this object is equal to another.
156         *
157         * @param obj  the object to test against for equality (<code>null</code>
158         *             permitted).
159         *
160         * @return A boolean.
161         */
162        public boolean equals(Object obj) {
163            if (obj == this) {
164                return true;
165            }
166            if (!(obj instanceof ComparableObjectItem)) {
167                return false;
168            }
169            ComparableObjectItem that = (ComparableObjectItem) obj;
170            if (!this.x.equals(that.x)) {
171                return false;
172            }
173            if (!ObjectUtilities.equal(this.obj, that.obj)) {
174                return false;
175            }
176            return true;        
177        }
178    
179        /**
180         * Returns a hash code.
181         * 
182         * @return A hash code.
183         */
184        public int hashCode() {
185            int result;
186            result = this.x.hashCode();
187            result = 29 * result + (this.obj != null ? this.obj.hashCode() : 0);
188            return result;
189        }
190        
191    }