Skip to content

Commit 64aa687

Browse files
gab1onectrueden
authored andcommitted
AbstractLocation: add hashCode() and equals()
Locations need to be properly distinguishable. This change makes it feasible to put Location objects into a HashMap and to compare them using the equals() method.
1 parent b87dd69 commit 64aa687

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main/java/org/scijava/io/location/AbstractLocation.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,30 @@
3131

3232
package org.scijava.io.location;
3333

34+
import java.util.Objects;
35+
3436
/**
3537
* Abstract base class for {@link Location} implementations.
3638
*
3739
* @author Curtis Rueden
3840
*/
3941
public abstract class AbstractLocation implements Location {
40-
// NB: No implementation needed.
42+
43+
@Override
44+
public int hashCode() {
45+
final int prime = 31;
46+
int result = 1;
47+
result = prime * result + ((getURI() == null) ? 0 : getURI().hashCode());
48+
return result;
49+
}
50+
51+
@Override
52+
public boolean equals(final Object obj) {
53+
if (obj == this) return true;
54+
if (obj == null) return false;
55+
if (getClass() != obj.getClass()) return false;
56+
final Location other = (Location) obj;
57+
return Objects.equals(getURI(), other.getURI());
58+
}
59+
4160
}

0 commit comments

Comments
 (0)