001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     * 
007     * Project Info:  http://www.jfree.org/jcommon/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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     * 
027     * -------------------
028     * FloatDimension.java
029     * -------------------
030     * (C)opyright 2002-2005, by Thomas Morgner and Contributors.
031     *
032     * Original Author:  Thomas Morgner;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: FloatDimension.java,v 1.4 2005/11/03 09:26:51 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 05-Dec-2002 : Updated Javadocs (DG);
040     * 29-Apr-2003 : Moved to JCommon
041     * 
042     */
043    
044    package org.jfree.ui;
045    
046    import java.awt.geom.Dimension2D;
047    import java.io.Serializable;
048    
049    /**
050     * A dimension object specified using <code>float</code> values.
051     *
052     * @author Thomas Morgner
053     */
054    public class FloatDimension extends Dimension2D 
055                                implements Serializable {
056    
057        /** For serialization. */
058        private static final long serialVersionUID = 5367882923248086744L;
059        
060        /** The width. */
061        private float width;
062    
063        /** The height. */
064        private float height;
065    
066        /**
067         * Creates a new dimension object with width and height set to zero.
068         */
069        public FloatDimension() {
070            this.width = 0.0f;
071            this.height = 0.0f;
072        }
073    
074        /**
075         * Creates a new dimension that is a copy of another dimension.
076         *
077         * @param fd  the dimension to copy.
078         */
079        public FloatDimension(final FloatDimension fd) {
080            this.width = fd.width;
081            this.height = fd.height;
082        }
083    
084        /**
085         * Creates a new dimension.
086         *
087         * @param width  the width.
088         * @param height  the height.
089         */
090        public FloatDimension(final float width, final float height) {
091            this.width = width;
092            this.height = height;
093        }
094    
095        /**
096         * Returns the width.
097         *
098         * @return the width.
099         */
100        public double getWidth() {
101            return this.width;
102        }
103    
104        /**
105         * Returns the height.
106         *
107         * @return the height.
108         */
109        public double getHeight() {
110            return this.height;
111        }
112    
113        /**
114         * Sets the width.
115         *
116         * @param width  the width.
117         */
118        public void setWidth(final double width) {
119            this.width = (float) width;
120        }
121    
122        /**
123         * Sets the height.
124         *
125         * @param height  the height.
126         */
127        public void setHeight(final double height) {
128            this.height = (float) height;
129        }
130    
131        /**
132         * Sets the size of this <code>Dimension</code> object to the specified 
133         * width and height.  This method is included for completeness, to parallel
134         * the {@link java.awt.Component#getSize() getSize} method of
135         * {@link java.awt.Component}.
136         * 
137         * @param width  the new width for the <code>Dimension</code> object
138         * @param height  the new height for the <code>Dimension</code> object
139         */
140        public void setSize(final double width, final double height) {
141            setHeight((float) height);
142            setWidth((float) width);
143        }
144    
145        /**
146         * Creates and returns a copy of this object.
147         *
148         * @return     a clone of this instance.
149         * @see        java.lang.Cloneable
150         */
151        public Object clone() {
152            return super.clone();
153        }
154    
155        /**
156         * Returns a string representation of the object. In general, the
157         * <code>toString</code> method returns a string that
158         * "textually represents" this object. The result should
159         * be a concise but informative representation that is easy for a
160         * person to read.
161         * <p>
162         *
163         * @return  a string representation of the object.
164         */
165        public String toString() {
166            return getClass().getName() + ":={width=" + getWidth() + ", height=" 
167                    + getHeight() + "}";
168        }
169    
170        /**
171         * Tests this object for equality with another object.
172         *
173         * @param o  the other object.
174         *
175         * @return <code>true</code> or <code>false</code>.
176         */
177        public boolean equals(final Object o) {
178            if (this == o) {
179                return true;
180            }
181            if (!(o instanceof FloatDimension)) {
182                return false;
183            }
184    
185            final FloatDimension floatDimension = (FloatDimension) o;
186    
187            if (this.height != floatDimension.height) {
188                return false;
189            }
190            if (this.width != floatDimension.width) {
191                return false;
192            }
193    
194            return true;
195        }
196    
197        /**
198         * Returns a hash code.
199         *
200         * @return A hash code.
201         */
202        public int hashCode() {
203            int result;
204            result = Float.floatToIntBits(this.width);
205            result = 29 * result + Float.floatToIntBits(this.height);
206            return result;
207        }
208    }
209