Skip to content

Commit

Permalink
Merge pull request #16 from mihyaeru21/add_equals_hash_code
Browse files Browse the repository at this point in the history
Add equals() and hashCode()
  • Loading branch information
mattak committed Jan 29, 2016
2 parents 906454b + f0edeb7 commit 955de3b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/geohex/geohex4j/GeoHex.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/*
Expand Down Expand Up @@ -67,6 +68,16 @@ public Loc[] getHexCoords() {
new Loc(h_btm, h_cl)
};
}

@Override
public boolean equals(Object other) {
return other instanceof Zone && this.code.equals(((Zone)other).code);
}

@Override
public int hashCode() {
return this.code.hashCode();
}
}

public static final Zone getZoneByLocation(double lat, double lon, int level) {
Expand Down Expand Up @@ -302,6 +313,18 @@ public XY(double x, double y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object other) {
if (!(other instanceof XY)) return false;
final XY otherXy = (XY)other;
return this.x == otherXy.x && this.y == otherXy.y;
}

@Override
public int hashCode() {
return Objects.hash(this.x, this.y);
}
}

public static final class Loc {
Expand All @@ -311,6 +334,18 @@ public Loc(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}

@Override
public boolean equals(Object other) {
if (!(other instanceof Loc)) return false;
final Loc otherLoc = (Loc)other;
return this.lat == otherLoc.lat && this.lon == otherLoc.lon;
}

@Override
public int hashCode() {
return Objects.hash(this.lat, this.lon);
}
}

public static final String encode(double lat, double lon, int level) {
Expand Down
65 changes: 62 additions & 3 deletions src/test/java/org/geohex/geohex4j/GeoHexTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.geohex.geohex4j;

import java.io.*;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class GeoHexTest {
private double LOCATION_PRECISION = 0.0000000000001;
Expand Down Expand Up @@ -175,6 +180,60 @@ public void getHexSize() throws IOException {
}
}

@Test
public void equalsZone() {
GeoHex.Zone zone = GeoHex.getZoneByXY(1, 2, 6);
GeoHex.Zone same = GeoHex.getZoneByXY(1, 2, 6);
GeoHex.Zone other = GeoHex.getZoneByXY(1, 3, 6);
assertTrue(zone.equals(same));
assertFalse(zone.equals(other));
}

@Test
public void hashCodeZone() {
GeoHex.Zone zone = GeoHex.getZoneByXY(1, 2, 6);
GeoHex.Zone same = GeoHex.getZoneByXY(1, 2, 6);
GeoHex.Zone other = GeoHex.getZoneByXY(1, 3, 6);
assertEquals(zone.hashCode(), same.hashCode());
assertNotEquals(zone.hashCode(), other.hashCode());
}

@Test
public void equalsXY() {
GeoHex.XY xy = new GeoHex.XY(1, 1);
GeoHex.XY same = new GeoHex.XY(1, 1);
GeoHex.XY other = new GeoHex.XY(1, 2);
assertTrue(xy.equals(same));
assertFalse(xy.equals(other));
}

@Test
public void hashCodeXY() {
GeoHex.XY xy = new GeoHex.XY(1, 1);
GeoHex.XY same = new GeoHex.XY(1, 1);
GeoHex.XY other = new GeoHex.XY(1, 2);
assertEquals(xy.hashCode(), same.hashCode());
assertNotEquals(xy.hashCode(), other.hashCode());
}

@Test
public void equalsLoc() {
GeoHex.Loc loc = new GeoHex.Loc(1.f, 1.f);
GeoHex.Loc same = new GeoHex.Loc(1.f, 1.f);
GeoHex.Loc other = new GeoHex.Loc(1.f, 2.f);
assertTrue(loc.equals(same));
assertFalse(loc.equals(other));
}

@Test
public void hashCodeLoc() {
GeoHex.Loc loc = new GeoHex.Loc(1.f, 1.f);
GeoHex.Loc same = new GeoHex.Loc(1.f, 1.f);
GeoHex.Loc other = new GeoHex.Loc(1.f, 2.f);
assertEquals(loc.hashCode(), same.hashCode());
assertNotEquals(loc.hashCode(), other.hashCode());
}

private void assertPolygon(double[][] expected_polygon, GeoHex.Loc[] polygon) {
for (int i = 0; i < expected_polygon.length; i++) {
double[] latlon = expected_polygon[i];
Expand Down

0 comments on commit 955de3b

Please sign in to comment.