KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > ComparableObjectItem


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  * ComparableObjectItem.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: ComparableObjectItem.java,v 1.1.2.1 2006/10/20 15:23:22 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 19-Oct-2006 : New class, based on XYDataItem (DG);
40  *
41  */

42
43 package org.jfree.data;
44
45 import java.io.Serializable JavaDoc;
46
47 import org.jfree.util.ObjectUtilities;
48
49 /**
50  * Represents one (Comparable, Object) data item for use in a
51  * {@link ComparableObjectSeries}.
52  *
53  * @since 1.0.3
54  */

55 public class ComparableObjectItem implements Cloneable JavaDoc, Comparable JavaDoc,
56         Serializable JavaDoc {
57
58     private static final long serialVersionUID = 2751513470325494890L;
59     
60     /** The x-value. */
61     private Comparable JavaDoc x;
62
63     /** The y-value. */
64     private Object JavaDoc obj;
65
66     /**
67      * Constructs a new data item.
68      *
69      * @param x the x-value (<code>null</code> NOT permitted).
70      * @param y the y-value (<code>null</code> permitted).
71      */

72     public ComparableObjectItem(Comparable JavaDoc x, Object JavaDoc y) {
73         if (x == null) {
74             throw new IllegalArgumentException JavaDoc("Null 'x' argument.");
75         }
76         this.x = x;
77         this.obj = y;
78     }
79
80     /**
81      * Returns the x-value.
82      *
83      * @return The x-value (never <code>null</code>).
84      */

85     protected Comparable JavaDoc getComparable() {
86         return this.x;
87     }
88
89     /**
90      * Returns the y-value.
91      *
92      * @return The y-value (possibly <code>null</code>).
93      */

94     protected Object JavaDoc getObject() {
95         return this.obj;
96     }
97
98     /**
99      * 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 JavaDoc 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 JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
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 JavaDoc 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 }
192
Popular Tags