001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.xy;
038
039import java.io.Serializable;
040
041/**
042 * Represents an (x, y) coordinate.
043 */
044public class XYCoordinate implements Comparable, Serializable {
045
046    /** The x-coordinate. */
047    private double x;
048
049    /** The y-coordinate. */
050    private double y;
051
052    /**
053     * Creates a new coordinate for the point (0.0, 0.0).
054     */
055    public XYCoordinate() {
056        this(0.0, 0.0);
057    }
058
059    /**
060     * Creates a new coordinate for the point (x, y).
061     *
062     * @param x  the x-coordinate.
063     * @param y  the y-coordinate.
064     */
065    public XYCoordinate(double x, double y) {
066        this.x = x;
067        this.y = y;
068    }
069
070    /**
071     * Returns the x-coordinate.
072     *
073     * @return The x-coordinate.
074     */
075    public double getX() {
076        return this.x;
077    }
078
079    /**
080     * Returns the y-coordinate.
081     *
082     * @return The y-coordinate.
083     */
084    public double getY() {
085        return this.y;
086    }
087
088    /**
089     * Tests this coordinate for equality with an arbitrary object.
090     *
091     * @param obj  the object ({@code null} permitted).
092     *
093     * @return A boolean.
094     */
095    @Override
096    public boolean equals(Object obj) {
097        if (obj == this) {
098            return true;
099        }
100        if (!(obj instanceof XYCoordinate)) {
101            return false;
102        }
103        XYCoordinate that = (XYCoordinate) obj;
104        if (this.x != that.x) {
105            return false;
106        }
107        if (this.y != that.y) {
108            return false;
109        }
110        return true;
111    }
112
113    /**
114     * Returns a hash code for this instance.
115     *
116     * @return A hash code.
117     */
118    @Override
119    public int hashCode() {
120        int result = 193;
121        long temp = Double.doubleToLongBits(this.x);
122        result = 37 * result + (int) (temp ^ (temp >>> 32));
123        temp = Double.doubleToLongBits(this.y);
124        result = 37 * result + (int) (temp ^ (temp >>> 32));
125        return result;
126    }
127
128    /**
129     * Returns a string representation of this instance, primarily for
130     * debugging purposes.
131     *
132     * @return A string.
133     */
134    @Override
135    public String toString() {
136        return "(" + this.x + ", " + this.y + ")";
137    }
138
139    /**
140     * Compares this instance against an arbitrary object.
141     *
142     * @param obj  the object ({@code null} not permitted).
143     *
144     * @return An integer indicating the relative order of the items.
145     */
146    @Override
147    public int compareTo(Object obj) {
148        if (!(obj instanceof XYCoordinate)) {
149            throw new IllegalArgumentException("Incomparable object.");
150        }
151        XYCoordinate that = (XYCoordinate) obj;
152        if (this.x > that.x) {
153            return 1;
154        }
155        else if (this.x < that.x) {
156            return -1;
157        }
158        else {
159            if (this.y > that.y) {
160                return 1;
161            }
162            else if (this.y < that.y) {
163                return -1;
164            }
165        }
166        return 0;
167    }
168
169}