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 package org.jemmy;
  26 
  27 
  28 import java.io.Serializable;
  29 
  30 
  31 /**
  32  * Replacement for java.awt.Dimension
  33  * @author Alexander Kouznetsov <mrkam@mail.ru>
  34  */
  35 public class Dimension implements Serializable {
  36 
  37     /**
  38      * The width dimension; negative values can be used.
  39      *
  40      * @serial
  41      * @see #getSize
  42      * @see #setSize
  43      */
  44     public int width;
  45 
  46     /**
  47      * The height dimension; negative values can be used.
  48      *
  49      * @serial
  50      * @see #getSize
  51      * @see #setSize
  52      */
  53     public int height;
  54 
  55     /*
  56      * JDK 1.1 serialVersionUID
  57      */
  58     private static final long serialVersionUID = 4723952579491349524L;
  59 
  60     /**
  61      * Creates an instance of <code>Dimension</code> with a width
  62      * of zero and a height of zero.
  63      */
  64     public Dimension() {
  65         this(0, 0);
  66     }
  67 
  68     /**
  69      * Creates an instance of <code>Dimension</code> whose width
  70      * and height are the same as for the specified dimension.
  71      *
  72      * @param    d   the specified dimension for the
  73      *               <code>width</code> and
  74      *               <code>height</code> values
  75      */
  76     public Dimension(Dimension d) {
  77         this(d.width, d.height);
  78     }
  79 
  80     /**
  81      * Constructs a <code>Dimension</code> and initializes
  82      * it to the specified width and specified height.
  83      *
  84      * @param width the specified width
  85      * @param height the specified height
  86      */
  87     public Dimension(int width, int height) {
  88         this.width = width;
  89         this.height = height;
  90     }
  91 
  92     /**
  93      * Constructs a <code>Dimension</code> and initializes
  94      * it to the specified width and specified height. All {@code double}
  95      * values are rounded and stored as {@code int} values.
  96      *
  97      * @param width the specified width
  98      * @param height the specified height
  99      */
 100     public Dimension(double width, double height) {
 101         this.width = (int) Math.round(width);
 102         this.height = (int) Math.round(height);
 103     }
 104 
 105     /**
 106      * {@inheritDoc}
 107      * @return
 108      */
 109     public double getWidth() {
 110         return width;
 111     }
 112 
 113     /**
 114      * {@inheritDoc}
 115      * @return
 116      */
 117     public double getHeight() {
 118         return height;
 119     }
 120 
 121     /**
 122      * Sets the size of this <code>Dimension</code> object to
 123      * the specified width and height in double precision.
 124      * Note that if <code>width</code> or <code>height</code>
 125      * are larger than <code>Integer.MAX_VALUE</code>, they will
 126      * be reset to <code>Integer.MAX_VALUE</code>.
 127      *
 128      * @param width  the new width for the <code>Dimension</code> object
 129      * @param height the new height for the <code>Dimension</code> object
 130      */
 131     public void setSize(double width, double height) {
 132         this.width = (int) Math.ceil(width);
 133         this.height = (int) Math.ceil(height);
 134     }
 135 
 136     /**
 137      * Gets the size of this <code>Dimension</code> object.
 138      * @return   the size of this dimension, a new instance of
 139      *           <code>Dimension</code> with the same width and height
 140      * @see      #setSize
 141      */
 142     public Dimension getSize() {
 143         return new Dimension(width, height);
 144     }
 145 
 146     /**
 147      * Sets the size of this <code>Dimension</code> object to the specified size.
 148      * @param    d  the new size for this <code>Dimension</code> object
 149      * @see      Dimension#getSize
 150      */
 151     public void setSize(Dimension d) {
 152         setSize(d.width, d.height);
 153     }
 154 
 155     /**
 156      * Sets the size of this <code>Dimension</code> object
 157      * to the specified width and height.
 158      * @param    width   the new width for this <code>Dimension</code> object
 159      * @param    height  the new height for this <code>Dimension</code> object
 160      * @see      Dimension#getSize
 161      */
 162     public void setSize(int width, int height) {
 163         this.width = width;
 164         this.height = height;
 165     }
 166 
 167     /**
 168      * Checks whether two dimension objects have equal values.
 169      * @param obj
 170      * @return
 171      */
 172     @Override
 173     public boolean equals(Object obj) {
 174         if (obj instanceof Dimension) {
 175             Dimension d = (Dimension)obj;
 176             return (width == d.width) && (height == d.height);
 177         }
 178         return false;
 179     }
 180 
 181     /**
 182      * Returns the hash code for this <code>Dimension</code>.
 183      *
 184      * @return    a hash code for this <code>Dimension</code>
 185      */
 186     @Override
 187     public int hashCode() {
 188         int sum = width + height;
 189         return sum * (sum + 1)/2 + width;
 190     }
 191 
 192     /**
 193      * Returns a string representation of the values of this
 194      * <code>Dimension</code> object's <code>height</code> and
 195      * <code>width</code> fields. This method is intended to be used only
 196      * for debugging purposes, and the content and format of the returned
 197      * string may vary between implementations. The returned string may be
 198      * empty but may not be <code>null</code>.
 199      *
 200      * @return  a string representation of this <code>Dimension</code>
 201      *          object
 202      */
 203     @Override
 204     public String toString() {
 205         return getClass().getName() + "[width=" + width + ",height=" + height + "]";
 206     }
 207 }