comparison dwt/graphics/Rectangle.d @ 30:93b13b15f0b1

Ported a couple of modules in dwt.graphics
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Thu, 11 Sep 2008 23:16:53 +0200
parents a9ab4c738ed8
children 7d135fe0caf2
comparison
equal deleted inserted replaced
29:d408fc12b991 30:93b13b15f0b1
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.graphics.Rectangle; 14 module dwt.graphics.Rectangle;
12 15
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT; 16 import dwt.DWT;
16 import dwt.internal.SerializableCompatibility; 17 import dwt.internal.SerializableCompatibility;
18
19 import tango.text.convert.Format;
20
21 import dwt.dwthelper.utils;
17 22
18 /** 23 /**
19 * Instances of this class represent rectangular areas in an 24 * Instances of this class represent rectangular areas in an
20 * (x, y) coordinate system. The top left corner of the rectangle 25 * (x, y) coordinate system. The top left corner of the rectangle
21 * is specified by its x and y values, and the extent of the 26 * is specified by its x and y values, and the extent of the
61 /** 66 /**
62 * the height of the rectangle 67 * the height of the rectangle
63 */ 68 */
64 public int height; 69 public int height;
65 70
66 static final long serialVersionUID = 3256439218279428914L; 71 static const long serialVersionUID = 3256439218279428914L;
67 72
68 /** 73 /**
69 * Construct a new instance of this class given the 74 * Construct a new instance of this class given the
70 * x, y, width and height values. 75 * x, y, width and height values.
71 * 76 *
148 * @param object the object to compare with this object 153 * @param object the object to compare with this object
149 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise 154 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
150 * 155 *
151 * @see #hashCode() 156 * @see #hashCode()
152 */ 157 */
153 public bool equals (Object object) { 158 public bool opEquals (Object object) {
154 if (object is this) return true; 159 if (object is this) return true;
155 if (!( null !is cast(Rectangle)object )) return false; 160 if (!( null !is cast(Rectangle)object )) return false;
156 Rectangle r = cast(Rectangle)object; 161 Rectangle r = cast(Rectangle)object;
157 return (r.x is this.x) && (r.y is this.y) && (r.width is this.width) && (r.height is this.height); 162 return (r.x is this.x) && (r.y is this.y) && (r.width is this.width) && (r.height is this.height);
158 } 163 }
159 164
165 alias opEquals equals;
166
160 /** 167 /**
161 * Returns an integer hash code for the receiver. Any two 168 * Returns an integer hash code for the receiver. Any two
162 * objects that return <code>true</code> when passed to 169 * objects that return <code>true</code> when passed to
163 * <code>equals</code> must return the same value for this 170 * <code>equals</code> must return the same value for this
164 * method. 171 * method.
165 * 172 *
166 * @return the receiver's hash 173 * @return the receiver's hash
167 * 174 *
168 * @see #equals(Object) 175 * @see #equals(Object)
169 */ 176 */
170 public int hashCode () { 177 public hash_t toHash () {
171 return x ^ y ^ width ^ height; 178 return x ^ y ^ width ^ height;
172 } 179 }
180
181 alias toHash hashCode;
173 182
174 /** 183 /**
175 * Destructively replaces the x, y, width and height values 184 * Destructively replaces the x, y, width and height values
176 * in the receiver with ones which represent the intersection of the 185 * in the receiver with ones which represent the intersection of the
177 * rectangles specified by the receiver and the given rectangle. 186 * rectangles specified by the receiver and the given rectangle.
307 * description of the receiver. 316 * description of the receiver.
308 * 317 *
309 * @return a string representation of the rectangle 318 * @return a string representation of the rectangle
310 */ 319 */
311 public String toString () { 320 public String toString () {
312 return "Rectangle {" + x + ", " + y + ", " + width + ", " + height + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ 321 return Format( "Rectangle {{{}, {}, {}, {}}", x, y, width, height ); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
313 } 322 }
314 323
315 /** 324 /**
316 * Returns a new rectangle which represents the union of 325 * Returns a new rectangle which represents the union of
317 * the receiver and the given rectangle. 326 * the receiver and the given rectangle.
328 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> 337 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
329 * </ul> 338 * </ul>
330 * 339 *
331 * @see #add(Rectangle) 340 * @see #add(Rectangle)
332 */ 341 */
333 public Rectangle union (Rectangle rect) { 342 public Rectangle unionn (Rectangle rect) {
334 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 343 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
335 int left = x < rect.x ? x : rect.x; 344 int left = x < rect.x ? x : rect.x;
336 int top = y < rect.y ? y : rect.y; 345 int top = y < rect.y ? y : rect.y;
337 int lhs = x + width; 346 int lhs = x + width;
338 int rhs = rect.x + rect.width; 347 int rhs = rect.x + rect.width;