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 Alexander Kouznetsov <mrkam@mail.ru>
  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      * @return
 105      */
 106     public int getX() {
 107         return x;
 108     }
 109 
 110     /**
 111      * {@inheritDoc}
 112      * @return
 113      */
 114     public int getY() {
 115         return y;
 116     }
 117 
 118     /**
 119      * Returns the location of this point.
 120      * @return      a copy of this point, at the same location
 121      * @see         org.jemmy.Point#setLocation(org.jemmy.Point)
 122      * @see         org.jemmy.Point#setLocation(int, int)
 123      */
 124     public Point getLocation() {
 125         return new Point(x, y);
 126     }
 127 
 128     /**
 129      * Sets the location of the point to the specified location.
 130      * @param       p  a point, the new location for this point
 131      * @return
 132      * @see         org.jemmy.Point#getLocation
 133      */
 134     public Point setLocation(Point p) {
 135         setLocation(p.x, p.y);
 136         return this;
 137     }
 138 
 139     /**
 140      * Changes the point to have the specified location.
 141      * <p>
 142      * Its behavior is identical with <code>move(int,&nbsp;int)</code>.
 143      * @param       x the X coordinate of the new location
 144      * @param       y the Y coordinate of the new location
 145      * @return self
 146      * @see         org.jemmy.Point#getLocation
 147      * @see         org.jemmy.Point#move(int, int)
 148      */
 149     public Point setLocation(int x, int y) {
 150         move(x, y);
 151         return this;
 152     }
 153 
 154     /**
 155      * Sets the location of this point to the specified double coordinates.
 156      * The double values will be rounded to integer values.
 157      * Any number smaller than <code>Integer.MIN_VALUE</code>
 158      * will be reset to <code>MIN_VALUE</code>, and any number
 159      * larger than <code>Integer.MAX_VALUE</code> will be
 160      * reset to <code>MAX_VALUE</code>.
 161      *
 162      * @param x the X coordinate of the new location
 163      * @param y the Y coordinate of the new location
 164      * @return self
 165      * @see #getLocation
 166      */
 167     public Point setLocation(double x, double y) {
 168         this.x = (int) Math.round(x);
 169         this.y = (int) Math.round(y);
 170         return this;
 171     }
 172 
 173     /**
 174      * Moves this point to the specified location in the
 175      * {@code (x,y)} coordinate plane. This method
 176      * is identical with <code>setLocation(int,&nbsp;int)</code>.
 177      * @param       x the X coordinate of the new location
 178      * @param       y the Y coordinate of the new location
 179      * @return self
 180      */
 181     public Point move(int x, int y) {
 182         this.x = x;
 183         this.y = y;
 184         return this;
 185     }
 186 
 187     /**
 188      * Translates this point, at location {@code (x,y)},
 189      * by {@code dx} along the {@code x} axis and {@code dy}
 190      * along the {@code y} axis so that it now represents the point
 191      * {@code (x + dx,y + dy)}.
 192      *
 193      * @param       dx   the distance to move this point
 194      *                            along the X axis
 195      * @param       dy    the distance to move this point
 196      *                            along the Y axis
 197      * @return self
 198      */
 199     public Point translate(int dx, int dy) {
 200         this.x += dx;
 201         this.y += dy;
 202         return this;
 203     }
 204 
 205     /**
 206      *
 207      * @param v
 208      * @return self
 209      */
 210     public Point translate(Vector v) {
 211         this.x = (int) Math.round(x + v.getX());
 212         this.y = (int) Math.round(y + v.getY());
 213         return this;
 214     }
 215     /**
 216      * Determines whether or not two points are equal. Two instances of
 217      * <code>Point</code> are equal if the values of their
 218      * <code>x</code> and <code>y</code> member fields, representing
 219      * their position in the coordinate space, are the same.
 220      * @param obj an object to be compared with this <code>Point</code>
 221      * @return <code>true</code> if the object to be compared is
 222      *         an instance of <code>Point</code> and has
 223      *         the same values; <code>false</code> otherwise.
 224      */
 225     @Override
 226     public boolean equals(Object obj) {
 227         if (obj instanceof Point) {
 228             Point pt = (Point)obj;
 229             return (x == pt.x) && (y == pt.y);
 230         }
 231         return super.equals(obj);
 232     }
 233 
 234     /**
 235      * {@inheritDoc}
 236      * @return
 237      */
 238     @Override
 239     public int hashCode() {
 240         int hash = 7;
 241         hash = 89 * hash + this.x;
 242         hash = 89 * hash + this.y;
 243         return hash;
 244     }
 245 
 246     /**
 247      * Returns a string representation of this point and its location
 248      * in the {@code (x,y)} coordinate space. This method is
 249      * intended to be used only for debugging purposes, and the content
 250      * and format of the returned string may vary between implementations.
 251      * The returned string may be empty but may not be <code>null</code>.
 252      *
 253      * @return  a string representation of this point
 254      */
 255     @Override
 256     public String toString() {
 257         return getClass().getName() + "[x=" + x + ",y=" + y + "]";
 258     }
 259 }