1 | /* |
2 | * Copyright 2001-2005 Daniel F. Savarese |
3 | * Copyright 2006-2009 Savarese Software Research Corporation |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * https://www.savarese.com/software/ApacheLicense-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | package com.savarese.spatial; |
19 | |
20 | /** |
21 | * A Point implementation supporting k dimensions. |
22 | */ |
23 | public class GenericPoint<Coord extends Comparable<? super Coord>> |
24 | implements Point<Coord> |
25 | { |
26 | private Comparable<? super Coord>[] __coordinates; |
27 | |
28 | /** |
29 | * Constructs a GenericPoint with the specified dimensions. |
30 | * |
31 | * @param dimensions The number of dimensions in the point. Must be |
32 | * greater than 0. |
33 | */ |
34 | public GenericPoint(int dimensions) { |
35 | assert(dimensions > 0); |
36 | __coordinates = new Comparable[dimensions]; |
37 | } |
38 | |
39 | /** |
40 | * Two-dimensional convenience constructor. |
41 | * |
42 | * @param x The coordinate value of the first dimension. |
43 | * @param y The coordinate value of the second dimension. |
44 | */ |
45 | public GenericPoint(Coord x, Coord y) { |
46 | this(2); |
47 | setCoord(0, x); |
48 | setCoord(1, y); |
49 | } |
50 | |
51 | /** |
52 | * Three-dimensional convenience constructor. |
53 | * |
54 | * @param x The coordinate value of the first dimension. |
55 | * @param y The coordinate value of the second dimension. |
56 | * @param z The coordinate value of the third dimension. |
57 | */ |
58 | public GenericPoint(Coord x, Coord y, Coord z) { |
59 | this(3); |
60 | setCoord(0, x); |
61 | setCoord(1, y); |
62 | setCoord(2, z); |
63 | } |
64 | |
65 | /** |
66 | * Four-dimensional convenience constructor. |
67 | * |
68 | * @param x The coordinate value of the first dimension. |
69 | * @param y The coordinate value of the second dimension. |
70 | * @param z The coordinate value of the third dimension. |
71 | * @param w The coordinate value of the fourth dimension. |
72 | */ |
73 | public GenericPoint(Coord x, Coord y, Coord z, Coord w) { |
74 | this(4); |
75 | setCoord(0, x); |
76 | setCoord(1, y); |
77 | setCoord(2, z); |
78 | setCoord(3, w); |
79 | } |
80 | |
81 | /** |
82 | * Sets the value of the coordinate for the specified dimension. |
83 | * |
84 | * @param dimension The dimension (starting from 0) of the |
85 | * coordinate value to set. |
86 | * @param value The new value of the coordinate. |
87 | * @exception ArrayIndexOutOfBoundsException If the dimension is |
88 | * outside of the range [0,getDimensions()-1]. |
89 | */ |
90 | public void setCoord(int dimension, Coord value) |
91 | throws ArrayIndexOutOfBoundsException |
92 | { |
93 | __coordinates[dimension] = value; |
94 | } |
95 | |
96 | /** |
97 | * Returns the value of the coordinate for the specified dimension. |
98 | * |
99 | * @param dimension The dimension (starting from 0) of the |
100 | * coordinate value to retrieve. |
101 | * @return The value of the coordinate for the specified dimension. |
102 | * @exception ArrayIndexOutOfBoundsException If the dimension is |
103 | * outside of the range [0,getDimensions()-1]. |
104 | */ |
105 | public Coord getCoord(int dimension) { |
106 | return (Coord)__coordinates[dimension]; |
107 | } |
108 | |
109 | /** |
110 | * Returns the number of dimensions of the point. |
111 | * |
112 | * @return The number of dimensions of the point. |
113 | */ |
114 | public int getDimensions() { return __coordinates.length; } |
115 | |
116 | /** |
117 | * Returns the hash code value for this point. |
118 | * |
119 | * @return The hash code value for this point. |
120 | */ |
121 | public int hashCode() { |
122 | int hash = 0; |
123 | for(Comparable<? super Coord> c : __coordinates) |
124 | hash+=c.hashCode(); |
125 | return hash; |
126 | } |
127 | |
128 | /** |
129 | * Returns true if the specified object is equal to the GenericPoint. |
130 | * |
131 | * @param obj The object to test for equality. |
132 | * @return true if the specified object is equal to the |
133 | * GenericPoint, false if not. |
134 | */ |
135 | public boolean equals(Object obj) { |
136 | if(!(obj instanceof GenericPoint)) |
137 | return false; |
138 | |
139 | GenericPoint point = (GenericPoint)obj; |
140 | |
141 | for(int i = 0; i < __coordinates.length; ++i) |
142 | if(!__coordinates[i].equals(point.getCoord(i))) |
143 | return false; |
144 | |
145 | return true; |
146 | } |
147 | |
148 | /** |
149 | * Returns a copy of the point. |
150 | * |
151 | * @return A copy of the point. |
152 | */ |
153 | public Object clone() { |
154 | GenericPoint<Coord> point = |
155 | new GenericPoint<Coord>(__coordinates.length); |
156 | for(int i = 0; i < __coordinates.length; ++i) |
157 | point.setCoord(i, (Coord)__coordinates[i]); |
158 | return point; |
159 | } |
160 | |
161 | /** |
162 | * Returns a string representation of the point, listing its |
163 | * coordinate values in order. |
164 | * |
165 | * @return A string representation of the point. |
166 | */ |
167 | public String toString() { |
168 | StringBuffer buffer = new StringBuffer(); |
169 | |
170 | buffer.append("[ "); |
171 | buffer.append(__coordinates[0].toString()); |
172 | |
173 | for(int i = 1; i < __coordinates.length; ++i) { |
174 | buffer.append(", "); |
175 | buffer.append(__coordinates[i].toString()); |
176 | } |
177 | |
178 | buffer.append(" ]"); |
179 | |
180 | return buffer.toString(); |
181 | } |
182 | } |