    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025 * Other names may be trademarks of their respective owners.]
    026 *
    027 * -----------------
    028 * XYCoordinate.java
    029 * -----------------
    030 * (C) Copyright 2007, 2008, by Object Refinery Limited and Contributors.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes
    036 * -------
    037 * 30-Jan-2007 : Version 1 (DG);
    038 * 25-May-2007 : Moved from experimental to the main source tree (DG);
    039 *
    040 */
    041
    042package org.jfree.data.xy;
    043
    044import java.io.Serializable;
    045
    046/**
    047 * Represents an (x, y) coordinate.
    048 *
    049 * @since 1.0.6
    050 */
    051public class XYCoordinate implements Comparable, Serializable {
    052
    053    /** The x-coordinate. */
    054    private double x;
    055
    056    /** The y-coordinate. */
    057    private double y;
    058
    059    /**
    060     * Creates a new coordinate for the point (0.0, 0.0).
    061     */
    062    public XYCoordinate() {
    063        this(0.0, 0.0);
    064    }
    065
    066    /**
    067     * Creates a new coordinate for the point (x, y).
    068     *
    069     * @param x  the x-coordinate.
    070     * @param y  the y-coordinate.
    071     */
    072    public XYCoordinate(double x, double y) {
    073        this.x = x;
    074        this.y = y;
    075    }
    076
    077    /**
    078     * Returns the x-coordinate.
    079     *
    080     * @return The x-coordinate.
    081     */
    082    public double getX() {
    083        return this.x;
    084    }
    085
    086    /**
    087     * Returns the y-coordinate.
    088     *
    089     * @return The y-coordinate.
    090     */
    091    public double getY() {
    092        return this.y;
    093    }
    094
    095    /**
    096     * Tests this coordinate for equality with an arbitrary object.
    097     *
    098     * @param obj  the object (<code>null</code> permitted).
    099     *
    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}