001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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     * XYDataItem.java
029     * ---------------
030     * (C) Copyright 2003-2005, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: XYDataItem.java,v 1.6.2.1 2005/10/25 21:36:51 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
040     * 03-Feb-2004 : Fixed bug in equals() method (DG);
041     * 21-Feb-2005 : Added setY(double) method (DG);
042     *
043     */
044    
045    package org.jfree.data.xy;
046    
047    import java.io.Serializable;
048    
049    import org.jfree.util.ObjectUtilities;
050    
051    /**
052     * Represents one (x, y) data item for an {@link XYSeries}.
053     */
054    public class XYDataItem implements Cloneable, Comparable, Serializable {
055    
056        private static final long serialVersionUID = 2751513470325494890L;
057        
058        /** The x-value. */
059        private Number x;
060    
061        /** The y-value. */
062        private Number y;
063    
064        /**
065         * Constructs a new data item.
066         *
067         * @param x  the x-value (<code>null</code> NOT permitted).
068         * @param y  the y-value (<code>null</code> permitted).
069         */
070        public XYDataItem(Number x, Number y) {
071            if (x == null) {
072                throw new IllegalArgumentException("Null 'x' argument.");
073            }
074            this.x = x;
075            this.y = y;
076        }
077    
078        /**
079         * Constructs a new data item.
080         *
081         * @param x  the x-value.
082         * @param y  the y-value.
083         */
084        public XYDataItem(double x, double y) {
085            this(new Double(x), new Double(y));
086        }
087    
088        /**
089         * Returns the x-value.
090         *
091         * @return The x-value (never <code>null</code>).
092         */
093        public Number getX() {
094            return this.x;
095        }
096    
097        /**
098         * Returns the y-value.
099         *
100         * @return The y-value (possibly <code>null</code>).
101         */
102        public Number getY() {
103            return this.y;
104        }
105    
106        /**
107         * Sets the y-value for this data item.  Note that there is no 
108         * corresponding method to change the x-value.
109         *
110         * @param y  the new y-value.
111         */
112        public void setY(double y) {
113            setY(new Double(y));   
114        }
115        
116        /**
117         * Sets the y-value for this data item.  Note that there is no 
118         * corresponding method to change the x-value.
119         *
120         * @param y  the new y-value (<code>null</code> permitted).
121         */
122        public void setY(Number y) {
123            this.y = y;
124        }
125    
126        /**
127         * Returns an integer indicating the order of this object relative to 
128         * another object.
129         * <P>
130         * For the order we consider only the x-value:
131         * negative == "less-than", zero == "equal", positive == "greater-than".
132         *
133         * @param o1  the object being compared to.
134         *
135         * @return An integer indicating the order of this data pair object
136         *      relative to another object.
137         */
138        public int compareTo(Object o1) {
139    
140            int result;
141    
142            // CASE 1 : Comparing to another TimeSeriesDataPair object
143            // -------------------------------------------------------
144            if (o1 instanceof XYDataItem) {
145                XYDataItem dataItem = (XYDataItem) o1;
146                double compare = this.x.doubleValue() 
147                                 - dataItem.getX().doubleValue();
148                if (compare > 0.0) {
149                    result = 1;
150                }
151                else {
152                    if (compare < 0.0) {
153                        result = -1;
154                    }
155                    else {
156                        result = 0;
157                    }
158                }
159            }
160    
161            // CASE 2 : Comparing to a general object
162            // ---------------------------------------------
163            else {
164                // consider time periods to be ordered after general objects
165                result = 1;
166            }
167    
168            return result;
169    
170        }
171    
172        /**
173         * Returns a clone of this object.
174         *
175         * @return A clone.
176         * 
177         * @throws CloneNotSupportedException not thrown by this class, but 
178         *         subclasses may differ.
179         */
180        public Object clone() throws CloneNotSupportedException {
181            return super.clone();
182        }
183        
184        /**
185         * Tests if this object is equal to another.
186         *
187         * @param obj  the object to test against for equality (<code>null</code>
188         *             permitted).
189         *
190         * @return A boolean.
191         */
192        public boolean equals(Object obj) {
193            if (obj == this) {
194                return true;
195            }
196            if (!(obj instanceof XYDataItem)) {
197                return false;
198            }
199            XYDataItem that = (XYDataItem) obj;
200            if (!this.x.equals(that.x)) {
201                return false;
202            }
203            if (!ObjectUtilities.equal(this.y, that.y)) {
204                return false;
205            }
206            return true;        
207        }
208    
209        /**
210         * Returns a hash code.
211         * 
212         * @return A hash code.
213         */
214        public int hashCode() {
215            int result;
216            result = this.x.hashCode();
217            result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
218            return result;
219        }
220        
221    }