Source for org.jfree.data.xy.XYCoordinate

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * XYCoordinate.java
  29:  * -----------------
  30:  * (C) Copyright 2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 30-Jan-2007 : Version 1 (DG);
  38:  * 25-May-2007 : Moved from experimental to the main source tree (DG);
  39:  *
  40:  */
  41: 
  42: package org.jfree.data.xy;
  43: 
  44: import java.io.Serializable;
  45: 
  46: /**
  47:  * Represents an (x, y) coordinate.
  48:  * 
  49:  * @since 1.0.6
  50:  */
  51: public class XYCoordinate implements Comparable, Serializable {
  52: 
  53:     /** The x-coordinate. */
  54:     private double x;
  55:     
  56:     /** The y-coordinate. */
  57:     private double y;
  58:     
  59:     /**
  60:      * Creates a new coordinate for the point (0.0, 0.0).
  61:      */
  62:     public XYCoordinate() {
  63:         this(0.0, 0.0);
  64:     }
  65:     
  66:     /**
  67:      * Creates a new coordinate for the point (x, y).
  68:      * 
  69:      * @param x  the x-coordinate.
  70:      * @param y  the y-coordinate.
  71:      */
  72:     public XYCoordinate(double x, double y) {
  73:         this.x = x;
  74:         this.y = y;
  75:     }
  76:     
  77:     /**
  78:      * Returns the x-coordinate.
  79:      * 
  80:      * @return The x-coordinate.
  81:      */
  82:     public double getX() {
  83:         return this.x;
  84:     }
  85:     
  86:     /**
  87:      * Returns the y-coordinate.
  88:      * 
  89:      * @return The y-coordinate.
  90:      */
  91:     public double getY() {
  92:         return this.y;
  93:     }
  94: 
  95:     /**
  96:      * Tests this coordinate for equality with an arbitrary object.
  97:      * 
  98:      * @param obj  the object (<code>null</code> permitted).
  99:      * 
 100:      * @return A boolean.
 101:      */
 102:     public boolean equals(Object obj) {
 103:         if (obj == this) {
 104:             return true;
 105:         }
 106:         if (!(obj instanceof XYCoordinate)) {
 107:             return false;
 108:         }
 109:         XYCoordinate that = (XYCoordinate) obj;
 110:         if (this.x != that.x) {
 111:             return false;
 112:         }
 113:         if (this.y != that.y) {
 114:             return false;
 115:         }
 116:         return true;
 117:     }
 118: 
 119:     /**
 120:      * Returns a hash code for this instance.
 121:      * 
 122:      * @return A hash code.
 123:      */
 124:     public int hashCode() {
 125:         int result = 193;
 126:         long temp = Double.doubleToLongBits(this.x);
 127:         result = 37 * result + (int) (temp ^ (temp >>> 32));
 128:         temp = Double.doubleToLongBits(this.y);
 129:         result = 37 * result + (int) (temp ^ (temp >>> 32));
 130:         return result;
 131:     }
 132: 
 133:     /**
 134:      * Returns a string representation of this instance, primarily for 
 135:      * debugging purposes.
 136:      * 
 137:      * @return A string.
 138:      */
 139:     public String toString() {
 140:         return "(" + this.x + ", " + this.y + ")";
 141:     }
 142: 
 143:     /**
 144:      * Compares this instance against an arbitrary object. 
 145:      * 
 146:      * @param obj  the object (<code>null</code> not permitted).
 147:      * 
 148:      * @return An integer indicating the relative order of the items.
 149:      */
 150:     public int compareTo(Object obj) {
 151:         if (!(obj instanceof XYCoordinate)) {
 152:             throw new IllegalArgumentException("Incomparable object.");
 153:         }
 154:         XYCoordinate that = (XYCoordinate) obj;
 155:         if (this.x > that.x) {
 156:             return 1;
 157:         }
 158:         else if (this.x < that.x) {
 159:             return -1;
 160:         }
 161:         else {
 162:             if (this.y > that.y) {
 163:                 return 1;
 164:             }
 165:             else if (this.y < that.y) {
 166:                 return -1;
 167:             }
 168:         }
 169:         return 0;
 170:     }
 171:     
 172: }