File: StrictBounds.java

package info (click to toggle)
liblayout 0.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,332 kB
  • sloc: java: 51,125; xml: 138; makefile: 17
file content (416 lines) | stat: -rw-r--r-- 11,525 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
 * ===========================================
 * LibLayout : a free Java layouting library
 * ===========================================
 *
 * Project Info:  http://reporting.pentaho.org/liblayout/
 *
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * $Id: StrictBounds.java 3524 2007-10-16 11:26:31Z tmorgner $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */
package org.jfree.layouting.util.geom;

import java.io.Serializable;

/**
 * The StrictBounds class is a replacement for the Rectangle2D classes.
 * This class uses integer mathematics instead of floating point values
 * to achive a higher degree of stability.
 *
 * @author Thomas Morgner
 */
public class StrictBounds implements Serializable, Cloneable
{
  /** The x-coordinate of the upper left corner. */
  private long x;
  /** The y-coordinate of the upper left corner. */
  private long y;
  /** The width of this rectangle. */
  private long width;
  /** The height of this rectangle. */
  private long height;
  /** A flag indicating whether attempts to change this rectangle should trigger Exceptions. */
  private boolean locked;

  /**
   * DefaultConstructor.
   */
  public StrictBounds ()
  {
  }

  /**
   * Creates a StrictBounds object with the given coordinates, width
   * and height.
   *
   * @param x the x-coordinate
   * @param y the y-coordinate
   * @param width the width of the rectangle
   * @param height the height of the rectangle
   */
  public StrictBounds (final long x, final long y,
                       final long width, final long height)
  {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  /**
   * Checks, whether this bounds object is locked.
   *
   * @return true, if the bounds are locked and therefore immutable, false otherwise.
   */
  public boolean isLocked ()
  {
    return locked;
  }


  /**
   * Returns a copy of this bounds object which cannot be modified anymore.
   *
   * @return a locked copy.
   */
  public StrictBounds getLockedInstance ()
  {
    if (locked)
    {
      return this;
    }

    final StrictBounds retval = (StrictBounds) clone();
    retval.locked = true;
    return retval;
  }

  /**
   * Returns a copy of this bounds object which can be modified later.
   *
   * @return an unlocked copy.
   */
  public StrictBounds getUnlockedInstance ()
  {
    final StrictBounds retval = (StrictBounds) clone();
    retval.locked = false;
    return retval;
  }

  /**
   * Sets the location and size of this <code>StrictBounds</code> to the specified double
   * values.
   *
   * @param x the coordinates to which to set the location of the upper left corner of
   *          this <code>StrictBounds</code>
   * @param y the coordinates to which to set the location of the upper left corner of
   *          this <code>StrictBounds</code>
   * @param w the value to use to set the width of this <code>StrictBounds</code>
   * @param h the value to use to set the height of this <code>StrictBounds</code>
   */
  public void setRect (final long x, final long y,
                       final long w, final long h)
  {
    if (locked)
    {
      throw new IllegalStateException("This object is locked");
    }
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
  }

  /**
   * Returns the height of the framing rectangle in micro points.
   *
   * @return the height of the framing rectangle.
   */
  public long getHeight ()
  {
    return height;
  }

  /**
   * Returns the width of the framing rectangle in micro points.
   *
   * @return the width of the framing rectangle.
   */
  public long getWidth ()
  {
    return width;
  }

  /**
   * Returns the X coordinate of the upper left corner of the framing
   * rectangle in micro points.
   *
   * @return the x coordinate of the upper left corner of the framing rectangle.
   */
  public long getX ()
  {
    return x;
  }

  /**
   * Returns the Y coordinate of the upper left corner of the framing
   * rectangle in micro points.
   *
   * @return the y coordinate of the upper left corner of the framing rectangle.
   */
  public long getY ()
  {
    return y;
  }

  /**
   * Determines whether the <code>RectangularShape</code> is empty. When the
   * <code>RectangularShape</code> is empty, it encloses no area.
   *
   * @return <code>true</code> if the <code>RectangularShape</code> is empty;
   *         <code>false</code> otherwise.
   */
  public boolean isEmpty ()
  {
    return width == 0 || height == 0;
  }

  /**
   * Returns a copy of this bounds object. This method will never throw a
   * 'CloneNotSupportedException'.
   *
   * @return the cloned instance.
   */
  public Object clone ()
  {
    try
    {
      return super.clone();
    }
    catch (CloneNotSupportedException e)
    {
      throw new InternalError("Clone must always be supported.");
    }
  }

  /**
   * Checks, whether this rectangle contains the given point.
   *
   * @param x the x-coordinate of the point.
   * @param y the y-coordinate of the point.
   * @return true, if the point is inside or directly on the border of this
   * rectangle, false otherwise.
   */
  public boolean contains (final long x, final long y)
  {
    if (x < this.x)
    {
      return false;
    }
    if (y < this.y)
    {
      return false;
    }
    if (x > (this.x + this.width))
    {
      return false;
    }
    return y <= (this.y + this.height);
  }


  /**
   * Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2
   * has a height or width of zero!).
   *
   * @param rect1 the first rectangle.
   * @param rect2 the second rectangle.
   * @return true, if the rectangles intersect each other, false otherwise.
   */
  public static boolean intersects (final StrictBounds rect1,
                                    final StrictBounds rect2)
  {

    final double x0 = rect1.getX();
    final double y0 = rect1.getY();

    final double x = rect2.getX();
    final double width = rect2.getWidth();
    final double y = rect2.getY();
    final double height = rect2.getHeight();
    return (x + width >= x0 &&
            y + height >= y0 &&
            x <= x0 + rect1.getWidth() &&
            y <= y0 + rect1.getHeight());
  }

  /**
   * Adds the given bounds to this bounds instance. The resulting rectangle
   * will fully contain both rectangles.
   *
   * @param bounds the rectangle that should be added.
   */
  public void add (final StrictBounds bounds)
  {
    if (locked)
    {
      throw new IllegalStateException("This object is locked");
    }

    final long x1 = Math.min(getX(), bounds.getX());
    final long y1 = Math.min(getY(), bounds.getY());
    final long x2 = Math.max(getX() + getWidth(), bounds.getX() + bounds.getWidth());
    final long y2 = Math.max(getY() + getHeight(), bounds.getY() + bounds.getHeight());
    setRect(x1, y1, Math.max (0, x2 - x1), Math.max (0, y2 - y1));
  }

  /**
   * Intersects this rectangle with the given bounds. The resulting rectangle
   * will cover the space, that is occupied by both rectangles at the same time.
   *
   * @param bounds the other rectangle.
   * @return the resulting intersection.
   */
  public StrictBounds createIntersection (final StrictBounds bounds)
  {
    final long x1 = Math.max(getX(), bounds.getX());
    final long y1 = Math.max(getY(), bounds.getY());
    final long x2 = Math.min(getX() + getWidth(), bounds.getX() + bounds.getWidth());
    final long y2 = Math.min(getY() + getHeight(), bounds.getY() + bounds.getHeight());

    return new StrictBounds(x1, y1, Math.max(0, x2 - x1), Math.max(0, y2 - y1));
  }


  /**
   * Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2
   * has a height or width of zero!).
   *
   * @param rect1 the first rectangle.
   * @param rect2 the second rectangle.
   * @return A boolean.
   */
  public static boolean contains (final StrictBounds rect1,
                                  final StrictBounds rect2)
  {

    final long x0 = rect1.getX();
    final long y0 = rect1.getY();
    final long x = rect2.getX();
    final long y = rect2.getY();
    final long w = rect2.getWidth();
    final long h = rect2.getHeight();

    return ((x >= x0) && (y >= y0) &&
            ((x + w) <= (x0 + rect1.getWidth())) &&
            ((y + h) <= (y0 + rect1.getHeight())));

  }

  /**
   * Checks, whether the given object is a StrictBounds instance convering the
   * same area as these bounds.
   *
   * @param o the other object.
   * @return true, if the other object is equal to this object, false otherwise.
   */
  public boolean equals (final Object o)
  {
    if (this == o)
    {
      return true;
    }
    if (!(o instanceof StrictBounds))
    {
      return false;
    }

    final StrictBounds strictBounds = (StrictBounds) o;

    if (height != strictBounds.height)
    {
      return false;
    }
    if (width != strictBounds.width)
    {
      return false;
    }
    if (x != strictBounds.x)
    {
      return false;
    }
    return y == strictBounds.y;

  }

  /**
   * Computes the hashcode for this rectangle.
   *
   * @return the computed hashcode.
   */
  public int hashCode ()
  {
    int result = (int) (x ^ (x >>> 32));
    result = 29 * result + (int) (y ^ (y >>> 32));
    result = 29 * result + (int) (width ^ (width >>> 32));
    result = 29 * result + (int) (height ^ (height >>> 32));
    return result;
  }


  /**
   * Returns a string representation of these bounds.
   *
   * @return the string representing this object.
   */
  public String toString ()
  {
    return new StringBuffer().append("org.jfree.report.util.geom.StrictBounds{").append("x=")
            .append(x)
            .append(", y=")
            .append(y)
            .append(", width=")
            .append(width)
            .append(", height=")
            .append(height)
            .append('}')
            .toString();
  }

  /**
   * Creates a union from this and the given rectangle. This is similiar to
   * calling 'add'. Calling this method does not modify the original and there
   * are no guarantees, that the resulting rectangle has a positive width or
   * height.
   *
   * @param bg the other rectangle.
   * @return the resulting union rectangle.
   */
  public StrictBounds createUnion (final StrictBounds bg)
  {
    final long x = Math.min(getX(), bg.getX());
    final long y = Math.min(getY(), bg.getY());
    final long w = Math.max(getX() + getWidth(), bg.getX() + bg.getWidth()) - x;
    final long h = Math.max(getY() + getHeight(), bg.getY() + bg.getHeight()) - y;
    return new StrictBounds(x, y, w, h);
  }
}