1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.jemmy;
  27 
  28 
  29 import java.io.Serializable;
  30 
  31 
  32 /**
  33  * Replacement for java.awt.Point
  34  * @author mrkam
  35  */
  36 public class Point implements Serializable{
  37     /**
  38      * The X coordinate of this <code>Point</code>.
  39      * If no X coordinate is set it will default to 0.
  40      *
  41      * @serial
  42      * @see #getLocation()
  43      * @see #move(int, int)
  44      */
  45     public int x;
  46 
  47     /**
  48      * The Y coordinate of this <code>Point</code>.
  49      * If no Y coordinate is set it will default to 0.
  50      *
  51      * @serial
  52      * @see #getLocation()
  53      * @see #move(int, int)
  54      */
  55     public int y;
  56 
  57     /*
  58      * JDK 1.1 serialVersionUID
  59      */
  60     private static final long serialVersionUID = -5276940640259749850L;
  61 
  62     /**
  63      * Constructs and initializes a point at the origin
  64      * (0,&nbsp;0) of the coordinate space.
  65      */
  66     public Point() {
  67         this(0, 0);
  68     }
  69 
  70     /**
  71      * Constructs and initializes a point with the same location as
  72      * the specified <code>Point</code> object.
  73      * @param       p a point
  74      */
  75     public Point(Point p) {
  76         this(p.x, p.y);
  77     }
  78 
  79     /**
  80      * Constructs and initializes a point at the specified
  81      * {@code (x,y)} location in the coordinate space.
  82      * @param x the X coordinate of the newly constructed <code>Point</code>
  83      * @param y the Y coordinate of the newly constructed <code>Point</code>
  84      */
  85     public Point(int x, int y) {
  86         this.x = x;
  87         this.y = y;
  88     }
  89 
  90     /**
  91      * Constructs and initializes a point at the specified
  92      * {@code (x,y)} location in the coordinate space. All {@code double}
  93      * values are rounded and stored as {@code int} values.
  94      * @param x the X coordinate of the newly constructed <code>Point</code>
  95      * @param y the Y coordinate of the newly constructed <code>Point</code>
  96      */
  97     public Point(double x, double y) {
  98         this.x = (int) Math.round(x);
  99         this.y = (int) Math.round(y);
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     public int getX() {
 106         return x;
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     public int getY() {
 113         return y;
 114     }
 115 
 116     /**
 117      * Returns the location of this point.
 118      * @return      a copy of this point, at the same location
 119      * @see         org.jemmy.Point#setLocation(org.jemmy.Point)
 120      * @see         org.jemmy.Point#setLocation(int, int)
 121      */
 122     public Point getLocation() {
 123         return new Point(x, y);
 124     }
 125 
 126     /**
 127      * Sets the location of the point to the specified location.
 128      * @param       p  a point, the new location for this point
 129      * @return      self
 130      * @see         org.jemmy.Point#getLocation
 131      */
 132     public Point setLocation(Point p) {
 133         setLocation(p.x, p.y);
 134         return this;
 135     }
 136 
 137     /**
 138      * Changes the point to have the specified location.
 139      * <p>
 140      * Its behavior is identical with <code>move(int,&nbsp;int)</code>.
 141      * @param       x the X coordinate of the new location
 142      * @param       y the Y coordinate of the new location
 143      * @return      self
 144      * @see         org.jemmy.Point#getLocation
 145      * @see         org.jemmy.Point#move(int, int)
 146      */
 147     public Point setLocation(int x, int y) {
 148         move(x, y);
 149         return this;
 150     }
 151 
 152     /**
 153      * Sets the location of this point to the specified double coordinates.
 154      * The double values will be rounded to integer values.
 155      * Any number smaller than <code>Integer.MIN_VALUE</code>
 156      * will be reset to <code>MIN_VALUE</code>, and any number
 157      * larger than <code>Integer.MAX_VALUE</code> will be
 158      * reset to <code>MAX_VALUE</code>.
 159      *
 160      * @param x the X coordinate of the new location
 161      * @param y the Y coordinate of the new location
 162      * @return self
 163      * @see #getLocation
 164      */
 165     public Point setLocation(double x, double y) {
 166         this.x = (int) Math.round(x);
 167         this.y = (int) Math.round(y);
 168         return this;
 169     }
 170 
 171     /**
 172      * Moves this point to the specified location in the
 173      * {@code (x,y)} coordinate plane. This method
 174      * is identical with <code>setLocation(int,&nbsp;int)</code>.
 175      * @param       x the X coordinate of the new location
 176      * @param       y the Y coordinate of the new location
 177      * @return self
 178      */
 179     public Point move(int x, int y) {
 180         this.x = x;
 181         this.y = y;
 182         return this;
 183     }
 184 
 185     /**
 186      * Translates this point, at location {@code (x,y)},
 187      * by {@code dx} along the {@code x} axis and {@code dy}
 188      * along the {@code y} axis so that it now represents the point
 189      * {@code (x + dx,y + dy)}.
 190      *
 191      * @param       dx   the distance to move this point
 192      *                            along the X axis
 193      * @param       dy    the distance to move this point
 194      *                            along the Y axis
 195      * @return self
 196      */
 197     public Point translate(int dx, int dy) {
 198         this.x += dx;
 199         this.y += dy;
 200         return this;
 201     }
 202 
 203     /**
 204      * Translates the current point according to the given vector.
 205      *
 206      * @param v vector by which to translate the point 
 207      * @return self
 208      */
 209     public Point translate(Vector v) {
 210         this.x = (int) Math.round(x + v.getX());
 211         this.y = (int) Math.round(y + v.getY());
 212         return this;
 213     }
 214     /**
 215      * Determines whether or not two points are equal. Two instances of
 216      * <code>Point</code> are equal if the values of their
 217      * <code>x</code> and <code>y</code> member fields, representing
 218      * their position in the coordinate space, are the same.
 219      * @param obj an object to be compared with this <code>Point</code>
 220      * @return <code>true</code> if the object to be compared is
 221      *         an instance of <code>Point</code> and has
 222      *         the same values; <code>false</code> otherwise.
 223      */
 224     @Override
 225     public boolean equals(Object obj) {
 226         if (obj instanceof Point) {
 227             Point pt = (Point)obj;
 228             return (x == pt.x) && (y == pt.y);
 229         }
 230         return super.equals(obj);
 231     }
 232 
 233     /**
 234      * {@inheritDoc}
 235      */
 236     @Override
 237     public int hashCode() {
 238         int hash = 7;
 239         hash = 89 * hash + this.x;
 240         hash = 89 * hash + this.y;
 241         return hash;
 242     }
 243 
 244     /**
 245      * Returns a string representation of this point and its location
 246      * in the {@code (x,y)} coordinate space. This method is
 247      * intended to be used only for debugging purposes, and the content
 248      * and format of the returned string may vary between implementations.
 249      * The returned string may be empty but may not be <code>null</code>.
 250      *
 251      * @return  a string representation of this point
 252      */
 253     @Override
 254     public String toString() {
 255         return getClass().getName() + "[x=" + x + ",y=" + y + "]";
 256     }
 257 }