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