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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.jemmy;
  24 
  25 
  26 import java.io.Serializable;
  27 
  28 
  29 /**
  30  * Replacement for java.awt.Dimension
  31  * @author Alexander Kouznetsov <mrkam@mail.ru>
  32  */
  33 public class Dimension implements Serializable {
  34 
  35     /**
  36      * The width dimension; negative values can be used.
  37      *
  38      * @serial
  39      * @see #getSize
  40      * @see #setSize
  41      */
  42     public int width;
  43 
  44     /**
  45      * The height dimension; negative values can be used.
  46      *
  47      * @serial
  48      * @see #getSize
  49      * @see #setSize
  50      */
  51     public int height;
  52 
  53     /*
  54      * JDK 1.1 serialVersionUID
  55      */
  56     private static final long serialVersionUID = 4723952579491349524L;
  57 
  58     /**
  59      * Creates an instance of <code>Dimension</code> with a width
  60      * of zero and a height of zero.
  61      */
  62     public Dimension() {
  63         this(0, 0);
  64     }
  65 
  66     /**
  67      * Creates an instance of <code>Dimension</code> whose width
  68      * and height are the same as for the specified dimension.
  69      *
  70      * @param    d   the specified dimension for the
  71      *               <code>width</code> and
  72      *               <code>height</code> values
  73      */
  74     public Dimension(Dimension d) {
  75         this(d.width, d.height);
  76     }
  77 
  78     /**
  79      * Constructs a <code>Dimension</code> and initializes
  80      * it to the specified width and specified height.
  81      *
  82      * @param width the specified width
  83      * @param height the specified height
  84      */
  85     public Dimension(int width, int height) {
  86         this.width = width;
  87         this.height = height;
  88     }
  89 
  90     /**
  91      * Constructs a <code>Dimension</code> and initializes
  92      * it to the specified width and specified height. All {@code double}
  93      * values are rounded and stored as {@code int} values.
  94      *
  95      * @param width the specified width
  96      * @param height the specified height
  97      */
  98     public Dimension(double width, double height) {
  99         this.width = (int) Math.round(width);
 100         this.height = (int) Math.round(height);
 101     }
 102 
 103     /**
 104      * {@inheritDoc}
 105      * @return
 106      */
 107     public double getWidth() {
 108         return width;
 109     }
 110 
 111     /**
 112      * {@inheritDoc}
 113      * @return 
 114      */
 115     public double getHeight() {
 116         return height;
 117     }
 118 
 119     /**
 120      * Sets the size of this <code>Dimension</code> object to
 121      * the specified width and height in double precision.
 122      * Note that if <code>width</code> or <code>height</code>
 123      * are larger than <code>Integer.MAX_VALUE</code>, they will
 124      * be reset to <code>Integer.MAX_VALUE</code>.
 125      *
 126      * @param width  the new width for the <code>Dimension</code> object
 127      * @param height the new height for the <code>Dimension</code> object
 128      */
 129     public void setSize(double width, double height) {
 130         this.width = (int) Math.ceil(width);
 131         this.height = (int) Math.ceil(height);
 132     }
 133 
 134     /**
 135      * Gets the size of this <code>Dimension</code> object.
 136      * @return   the size of this dimension, a new instance of
 137      *           <code>Dimension</code> with the same width and height
 138      * @see      #setSize
 139      */
 140     public Dimension getSize() {
 141         return new Dimension(width, height);
 142     }
 143 
 144     /**
 145      * Sets the size of this <code>Dimension</code> object to the specified size.
 146      * @param    d  the new size for this <code>Dimension</code> object
 147      * @see      Dimension#getSize
 148      */
 149     public void setSize(Dimension d) {
 150         setSize(d.width, d.height);
 151     }
 152 
 153     /**
 154      * Sets the size of this <code>Dimension</code> object
 155      * to the specified width and height.
 156      * @param    width   the new width for this <code>Dimension</code> object
 157      * @param    height  the new height for this <code>Dimension</code> object
 158      * @see      Dimension#getSize
 159      */
 160     public void setSize(int width, int height) {
 161         this.width = width;
 162         this.height = height;
 163     }
 164 
 165     /**
 166      * Checks whether two dimension objects have equal values.
 167      * @param obj 
 168      * @return
 169      */
 170     @Override
 171     public boolean equals(Object obj) {
 172         if (obj instanceof Dimension) {
 173             Dimension d = (Dimension)obj;
 174             return (width == d.width) && (height == d.height);
 175         }
 176         return false;
 177     }
 178 
 179     /**
 180      * Returns the hash code for this <code>Dimension</code>.
 181      *
 182      * @return    a hash code for this <code>Dimension</code>
 183      */
 184     @Override
 185     public int hashCode() {
 186         int sum = width + height;
 187         return sum * (sum + 1)/2 + width;
 188     }
 189 
 190     /**
 191      * Returns a string representation of the values of this
 192      * <code>Dimension</code> object's <code>height</code> and
 193      * <code>width</code> fields. This method is intended to be used only
 194      * for debugging purposes, and the content and format of the returned
 195      * string may vary between implementations. The returned string may be
 196      * empty but may not be <code>null</code>.
 197      *
 198      * @return  a string representation of this <code>Dimension</code>
 199      *          object
 200      */
 201     @Override
 202     public String toString() {
 203         return getClass().getName() + "[width=" + width + ",height=" + height + "]";
 204     }
 205 }