001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 @Override 103 public boolean equals(Object obj) { 104 if (obj == this) { 105 return true; 106 } 107 if (!(obj instanceof XYCoordinate)) { 108 return false; 109 } 110 XYCoordinate that = (XYCoordinate) obj; 111 if (this.x != that.x) { 112 return false; 113 } 114 if (this.y != that.y) { 115 return false; 116 } 117 return true; 118 } 119 120 /** 121 * Returns a hash code for this instance. 122 * 123 * @return A hash code. 124 */ 125 @Override 126 public int hashCode() { 127 int result = 193; 128 long temp = Double.doubleToLongBits(this.x); 129 result = 37 * result + (int) (temp ^ (temp >>> 32)); 130 temp = Double.doubleToLongBits(this.y); 131 result = 37 * result + (int) (temp ^ (temp >>> 32)); 132 return result; 133 } 134 135 /** 136 * Returns a string representation of this instance, primarily for 137 * debugging purposes. 138 * 139 * @return A string. 140 */ 141 @Override 142 public String toString() { 143 return "(" + this.x + ", " + this.y + ")"; 144 } 145 146 /** 147 * Compares this instance against an arbitrary object. 148 * 149 * @param obj the object (<code>null</code> not permitted). 150 * 151 * @return An integer indicating the relative order of the items. 152 */ 153 @Override 154 public int compareTo(Object obj) { 155 if (!(obj instanceof XYCoordinate)) { 156 throw new IllegalArgumentException("Incomparable object."); 157 } 158 XYCoordinate that = (XYCoordinate) obj; 159 if (this.x > that.x) { 160 return 1; 161 } 162 else if (this.x < that.x) { 163 return -1; 164 } 165 else { 166 if (this.y > that.y) { 167 return 1; 168 } 169 else if (this.y < that.y) { 170 return -1; 171 } 172 } 173 return 0; 174 } 175 176}