01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.support;
16:
17: /**
18: * A point implementation. Each point for a given location is a singleton and immutable so points can be
19: * compared with ==.
20: *
21: * @author jones
22: * @since 1.1.0
23: */
24: public class Point {
25:
26: private Point(int x, int y) {
27: this .x = x;
28: this .y = y;
29: hashCode = (17 + x) * 37 + y;
30: }
31:
32: private final int x;
33: private final int y;
34: private final int hashCode;
35:
36: public int getX() {
37: return x;
38: }
39:
40: public int getY() {
41: return y;
42: }
43:
44: private static final Point[][] cache = new Point[256][256];
45:
46: /**
47: * Get a point for location x,y
48: *
49: * @return a point for location x,y
50: */
51: public synchronized static Point valueOf(int x, int y) {
52: if (x > -1 && x < 256 && y > -1 && y < 256) {
53: if (cache[x][y] == null) {
54: cache[x][y] = new Point(x, y);
55: }
56: return cache[x][y];
57: }
58:
59: return new Point(x, y);
60:
61: }
62:
63: @Override
64: public boolean equals(Object obj) {
65: if (obj == this )
66: return true;
67:
68: if (!(obj instanceof Point))
69: return false;
70: Point p = (Point) obj;
71: return x == p.x && y == p.y;
72: }
73:
74: @Override
75: public int hashCode() {
76: return hashCode;
77: }
78:
79: @Override
80: public String toString() {
81: return "(" + x + "," + y + ")"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
82: }
83: }
|