aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java2
-rw-r--r--main/src/cgeo/geocaching/Waypoint.java21
-rw-r--r--tests/src/cgeo/geocaching/WaypointTest.java38
3 files changed, 34 insertions, 27 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java
index 89e78d6..d0273c5 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -1826,7 +1826,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
// sort waypoints: PP, Sx, FI, OWN
final List<Waypoint> sortedWaypoints = new ArrayList<Waypoint>(cache.getWaypoints());
- Collections.sort(sortedWaypoints);
+ Collections.sort(sortedWaypoints, Waypoint.WAYPOINT_COMPARATOR);
view = (ListView) getLayoutInflater().inflate(R.layout.cachedetail_waypoints_page, null);
view.setClickable(true);
diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java
index 6f28aa1..e1a8b2f 100644
--- a/main/src/cgeo/geocaching/Waypoint.java
+++ b/main/src/cgeo/geocaching/Waypoint.java
@@ -5,14 +5,12 @@ import cgeo.geocaching.geopoint.Geopoint;
import org.apache.commons.lang3.StringUtils;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-/**
- * Note: this class has a natural ordering that is inconsistent with equals.
- */
-public class Waypoint implements IWaypoint, Comparable<Waypoint> {
+public class Waypoint implements IWaypoint {
public static final String PREFIX_OWN = "OWN";
private static final int ORDER_UNDEFINED = -2;
@@ -135,11 +133,6 @@ public class Waypoint implements IWaypoint, Comparable<Waypoint> {
return cachedOrder;
}
- @Override
- public int compareTo(Waypoint other) {
- return order() - other.order();
- }
-
public String getPrefix() {
return prefix;
}
@@ -254,4 +247,14 @@ public class Waypoint implements IWaypoint, Comparable<Waypoint> {
return (int) hash;
}
+ /**
+ * Sort waypoints by their probable order (e.g. parking first, final last).
+ */
+ public static final Comparator<? super Waypoint> WAYPOINT_COMPARATOR = new Comparator<Waypoint>() {
+
+ @Override
+ public int compare(Waypoint left, Waypoint right) {
+ return left.order() - right.order();
+ }
+ };
}
diff --git a/tests/src/cgeo/geocaching/WaypointTest.java b/tests/src/cgeo/geocaching/WaypointTest.java
index dc2853a..3ddc32c 100644
--- a/tests/src/cgeo/geocaching/WaypointTest.java
+++ b/tests/src/cgeo/geocaching/WaypointTest.java
@@ -14,23 +14,27 @@ public class WaypointTest extends AndroidTestCase {
final Waypoint own = new Waypoint("own", WaypointType.OWN, true);
final Waypoint parking = new Waypoint("parking", WaypointType.PARKING, false);
- assertTrue(trailhead.compareTo(puzzle) < 0);
- assertTrue(trailhead.compareTo(stage) < 0);
- assertTrue(trailhead.compareTo(cache) < 0);
-
- assertTrue(stage.compareTo(cache) < 0);
- assertTrue(puzzle.compareTo(cache) < 0);
-
- assertTrue(trailhead.compareTo(own) < 0);
- assertTrue(puzzle.compareTo(own) < 0);
- assertTrue(stage.compareTo(own) < 0);
- assertTrue(cache.compareTo(own) < 0);
-
- assertTrue(parking.compareTo(puzzle) < 0);
- assertTrue(parking.compareTo(stage) < 0);
- assertTrue(parking.compareTo(cache) < 0);
- assertTrue(parking.compareTo(own) < 0);
- assertTrue(parking.compareTo(trailhead) < 0);
+ assertOrdered(trailhead, puzzle);
+ assertOrdered(trailhead, stage);
+ assertOrdered(trailhead, cache);
+
+ assertOrdered(stage, cache);
+ assertOrdered(puzzle, cache);
+
+ assertOrdered(trailhead, own);
+ assertOrdered(puzzle, own);
+ assertOrdered(stage, own);
+ assertOrdered(cache, own);
+
+ assertOrdered(parking, puzzle);
+ assertOrdered(parking, stage);
+ assertOrdered(parking, cache);
+ assertOrdered(parking, own);
+ assertOrdered(parking, trailhead);
+ }
+
+ private static void assertOrdered(Waypoint first, Waypoint second) {
+ assertTrue(Waypoint.WAYPOINT_COMPARATOR.compare(first, second) < 0);
}
public static void testGeocode() {