Skip to content

Commit 70980c4

Browse files
author
monkstone
committed
hashcode and equals stuff
1 parent 60331d6 commit 70980c4

File tree

5 files changed

+58
-74
lines changed

5 files changed

+58
-74
lines changed

ext/toxi/geom/Line2D.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import java.util.ArrayList;
3131
import java.util.List;
32-
import java.util.Objects;
3332

3433
import javax.xml.bind.annotation.XmlElement;
3534

@@ -205,7 +204,7 @@ public boolean equals(Object obj) {
205204
}
206205
return false;
207206
}
208-
207+
209208
/**
210209
* Computes a hash code ignoring the directionality of the line.
211210
*
@@ -218,8 +217,7 @@ public boolean equals(Object obj) {
218217
@Override
219218
public int hashCode() {
220219
int hash = 5;
221-
hash = 37 * hash + Objects.hashCode(this.a);
222-
hash = 37 * hash + Objects.hashCode(this.b);
220+
hash = 37 * hash + (a.hashCode() + b.hashCode());
223221
return hash;
224222
}
225223

@@ -275,10 +273,10 @@ public boolean hasEndPoint(Vec2D p) {
275273
* @see #hashCode()
276274
*/
277275
public int hashCodeWithDirection() {
278-
long bits = 1L;
279-
bits = 31L * bits + a.hashCode();
280-
bits = 31L * bits + b.hashCode();
281-
return (int) (bits ^ (bits >> 32));
276+
int hash = 5;
277+
hash = 89 * hash + a.hashCode();
278+
hash = 89 * hash + b.hashCode();
279+
return hash;
282280
}
283281

284282
/**

ext/toxi/geom/Line3D.java

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@
2424
* License along with this library; if not, write to the Free Software
2525
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
2626
*/
27-
2827
package toxi.geom;
29-
3028
import java.util.ArrayList;
3129
import java.util.List;
32-
import java.util.Objects;
33-
3430
import javax.xml.bind.annotation.XmlElement;
35-
3631
import toxi.geom.Line3D.LineIntersection.Type;
3732
import toxi.math.MathUtils;
3833

@@ -41,6 +36,7 @@ public class Line3D {
4136
public static class LineIntersection {
4237

4338
public static enum Type {
39+
4440
NON_INTERSECTING,
4541
INTERSECTING
4642
}
@@ -56,8 +52,8 @@ private LineIntersection(Type type) {
5652
private LineIntersection(Type type, Line3D line, float mua, float mub) {
5753
this.type = type;
5854
this.line = line;
59-
this.coeff = new float[] {
60-
mua, mub
55+
this.coeff = new float[]{
56+
mua, mub
6157
};
6258
}
6359

@@ -101,17 +97,12 @@ public String toString() {
10197
* last segment has a shorter length than the step length requested. The
10298
* first point (A) can be omitted and not be added to the list if so
10399
* desired.
104-
*
105-
* @param a
106-
* start point
107-
* @param b
108-
* end point (always added to results)
109-
* @param stepLength
110-
* desired distance between points
111-
* @param segments
112-
* existing array list for results (or a new list, if null)
113-
* @param addFirst
114-
* false, if A is NOT to be added to results
100+
*
101+
* @param a start point
102+
* @param b end point (always added to results)
103+
* @param stepLength desired distance between points
104+
* @param segments existing array list for results (or a new list, if null)
105+
* @param addFirst false, if A is NOT to be added to results
115106
* @return list of result vectors
116107
*/
117108
public static final List<Vec3D> splitIntoSegments(Vec3D a, Vec3D b,
@@ -161,13 +152,14 @@ public Line3D(Vec3D a, Vec3D b) {
161152
* within the 0.0 .. 1.0 interval the endpoints of the intersection line are
162153
* within the given line segments, if not then the intersection line is
163154
* outside.
164-
*
155+
*
165156
* <p>
166157
* Code based on original by Paul Bourke:<br/>
167158
* http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/
168159
* </p>
160+
*
169161
* @param l
170-
* @return
162+
* @return
171163
*/
172164
public LineIntersection closestLineTo(Line3D l) {
173165
Vec3D p43 = l.a.sub(l.b);
@@ -202,9 +194,8 @@ public LineIntersection closestLineTo(Line3D l) {
202194

203195
/**
204196
* Computes the closest point on this line to the given one.
205-
*
206-
* @param p
207-
* point to check against
197+
*
198+
* @param p point to check against
208199
* @return closest point on the line
209200
*/
210201
public Vec3D closestPointTo(ReadonlyVec3D p) {
@@ -226,31 +217,22 @@ public Line3D copy() {
226217

227218
@Override
228219
public boolean equals(Object obj) {
229-
if (obj == null) {
230-
return false;
231-
}
232-
if (this == obj) {
233-
return true;
220+
if (obj instanceof Line3D) {
221+
Line3D l = (Line3D) obj;
222+
return (a.equals(l.a) || a.equals(l.b))
223+
&& (b.equals(l.b) || b.equals(l.a));
234224
}
235-
if (!(obj instanceof Line3D)) {
236-
return false;
237-
}
238-
Line3D l = (Line3D) obj;
239-
return (a.equals(l.a) || a.equals(l.b))
240-
&& (b.equals(l.b) || b.equals(l.a));
225+
return false;
241226
}
242227

243228
@Override
244229
public int hashCode() {
245-
int hash = 7;
246-
hash = 53 * hash + Objects.hashCode(this.a);
247-
hash = 53 * hash + Objects.hashCode(this.b);
248-
return hash;
230+
return a.hashCode() + b.hashCode();
249231
}
250232

251233
/**
252234
* Returns the line's axis-aligned bounding box.
253-
*
235+
*
254236
* @return aabb
255237
* @see toxi.geom.AABB
256238
*/
@@ -282,22 +264,21 @@ public boolean hasEndPoint(Vec3D p) {
282264
return a.equals(p) || b.equals(p);
283265
}
284266

285-
286267
/**
287268
* Computes the hash code for this instance taking directionality into
288269
* account. A->B will produce a different hash code than B->A. If
289270
* directionality is not required or desired use the default
290271
* {@link #hashCode()} method.
291-
*
272+
*
292273
* @return hash code
293-
*
274+
*
294275
* @see #hashCode()
295276
*/
296277
public int hashCodeWithDirection() {
297-
long bits = 1L;
298-
bits = 31L * bits + a.hashCode();
299-
bits = 31L * bits + b.hashCode();
300-
return (int) (bits ^ (bits >> 32));
278+
int hash = 7;
279+
hash = 53 * hash + a.hashCode();
280+
hash = 53 * hash + b.hashCode();
281+
return hash;
301282
}
302283

303284
public Line3D offsetAndGrowBy(float offset, float scale, Vec3D ref) {

ext/toxi/geom/Vec2D.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,4 +1146,4 @@ public final float x() {
11461146
public final float y() {
11471147
return y;
11481148
}
1149-
}
1149+
}

ext/toxi/geom/Vec3D.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,4 +1653,4 @@ public final float y() {
16531653
public final float z() {
16541654
return z;
16551655
}
1656-
}
1656+
}

ext/toxi/geom/Vec4D.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,9 @@ public final float angleBetween(ReadonlyVec4D v) {
149149
return (float) (Math.acos(vDot));
150150
}
151151

152-
// public Object clone() {
153-
// try {
154-
// return super.clone();
155-
// } catch (CloneNotSupportedException e) {
156-
// throw new InternalError();
157-
// }
158-
// }
159-
160152
@Override
161153
public int compareTo(ReadonlyVec4D v) {
162-
if (x == v.x() && y == v.y() && z == v.z() && w == v.w()) {
154+
if (this.equals(v)) {
163155
return 0;
164156
}
165157
float a = magSquared();
@@ -218,7 +210,16 @@ public final float dot(ReadonlyVec4D v) {
218210
public boolean equals(Object v) {
219211
if (v instanceof ReadonlyVec4D) {
220212
ReadonlyVec4D vv = (ReadonlyVec4D) v;
221-
return (x == vv.x() && y == vv.y() && z == vv.z() && w == vv.w());
213+
if (!((Float) x).equals(vv.x())) {
214+
return false;
215+
}
216+
if (!((Float) y).equals(vv.y())) {
217+
return false;
218+
}
219+
if (!((Float) z).equals(vv.z())) {
220+
return false;
221+
}
222+
return ((Float) w).equals(vv.w());
222223
}
223224
return false;
224225
}
@@ -252,16 +253,21 @@ public int hashCode() {
252253
* @return true or false
253254
*/
254255
public boolean equals(ReadonlyVec4D v) {
255-
try {
256-
return (x == v.x() && y == v.y() && z == v.z() && w == v.w());
257-
} catch (NullPointerException e) {
258-
return false;
259-
}
256+
if (!((Float) x).equals(v.x())) {
257+
return false;
258+
}
259+
if (!((Float) y).equals(v.y())) {
260+
return false;
261+
}
262+
if (!((Float) z).equals(v.z())) {
263+
return false;
264+
}
265+
return ((Float) w).equals(v.w());
260266
}
261267

262268
@Override
263269
public boolean equalsWithTolerance(ReadonlyVec4D v, float tolerance) {
264-
try {
270+
if (v instanceof ReadonlyVec4D) {
265271
float diff = x - v.x();
266272
if (Float.isNaN(diff)) {
267273
return false;
@@ -288,9 +294,8 @@ public boolean equalsWithTolerance(ReadonlyVec4D v, float tolerance) {
288294
return false;
289295
}
290296
return ((diff > 0 ? -diff : diff) < tolerance);
291-
} catch (NullPointerException e) {
292-
return false;
293297
}
298+
return false;
294299
}
295300

296301
@Override

0 commit comments

Comments
 (0)