diff options
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgWaypoint.java | 33 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeodetail.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypoint.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypointadd.java | 4 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/test/cgWaypointTest.java | 33 |
7 files changed, 69 insertions, 19 deletions
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index aa6ad37..2345d5a 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -1570,7 +1570,7 @@ public class cgBase { try { final Matcher matcherWpPrefix = patternWpPrefixOrLookupOrLatlon.matcher(wp[4]); if (matcherWpPrefix.find() && matcherWpPrefix.groupCount() > 1) { - waypoint.prefix = matcherWpPrefix.group(2).trim(); + waypoint.setPrefix(matcherWpPrefix.group(2).trim()); } } catch (Exception e) { // failed to parse prefix diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index c24d0ff..2d7a653 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1368,7 +1368,7 @@ public class cgData { values.put("geocode", geocode); values.put("updated", timeStamp); values.put("type", oneWaypoint.type); - values.put("prefix", oneWaypoint.prefix); + values.put("prefix", oneWaypoint.getPrefix()); values.put("lookup", oneWaypoint.lookup); values.put("name", oneWaypoint.name); values.put("latlon", oneWaypoint.latlon); @@ -1456,7 +1456,7 @@ public class cgData { values.put("geocode", geocode); values.put("updated", System.currentTimeMillis()); values.put("type", waypoint.type); - values.put("prefix", waypoint.prefix); + values.put("prefix", waypoint.getPrefix()); values.put("lookup", waypoint.lookup); values.put("name", waypoint.name); values.put("latlon", waypoint.latlon); @@ -2166,7 +2166,7 @@ public class cgData { waypoint.id = (int) cursor.getInt(cursor.getColumnIndex("_id")); waypoint.geocode = (String) cursor.getString(cursor.getColumnIndex("geocode")); waypoint.type = (String) cursor.getString(cursor.getColumnIndex("type")); - waypoint.prefix = (String) cursor.getString(cursor.getColumnIndex("prefix")); + waypoint.setPrefix((String) cursor.getString(cursor.getColumnIndex("prefix"))); waypoint.lookup = (String) cursor.getString(cursor.getColumnIndex("lookup")); waypoint.name = (String) cursor.getString(cursor.getColumnIndex("name")); waypoint.latlon = (String) cursor.getString(cursor.getColumnIndex("latlon")); diff --git a/main/src/cgeo/geocaching/cgWaypoint.java b/main/src/cgeo/geocaching/cgWaypoint.java index 05c24b7..1291bba 100644 --- a/main/src/cgeo/geocaching/cgWaypoint.java +++ b/main/src/cgeo/geocaching/cgWaypoint.java @@ -14,7 +14,7 @@ public class cgWaypoint implements Comparable<cgWaypoint> { public Integer id = 0; public String geocode = "geocode"; public String type = "waypoint"; - public String prefix = ""; + private String prefix = ""; public String lookup = ""; public String name = ""; public String latlon = ""; @@ -22,6 +22,7 @@ public class cgWaypoint implements Comparable<cgWaypoint> { public String longitudeString = ""; public Geopoint coords = null; public String note = ""; + private Integer cachedOrder = null; public void setIcon(Resources res, cgBase base, TextView nameView) { int iconId = R.drawable.waypoint_waypoint; @@ -35,14 +36,14 @@ public class cgWaypoint implements Comparable<cgWaypoint> { } public void merge(final cgWaypoint old) { - if (StringUtils.isBlank(prefix)) { - prefix = old.prefix; + if (StringUtils.isBlank(getPrefix())) { + setPrefix(old.getPrefix()); } if (StringUtils.isBlank(lookup)) { lookup = old.lookup; } if (StringUtils.isBlank(name)) { - name = old.name; + this.name = old.name; } if (StringUtils.isBlank(latlon)) { latlon = old.latlon; @@ -95,18 +96,18 @@ public class cgWaypoint implements Comparable<cgWaypoint> { return type != null && type.equalsIgnoreCase("own"); } - private int order() { - if (StringUtils.isEmpty(prefix)) { + private int computeOrder() { + if (StringUtils.isEmpty(getPrefix())) { return 0; } // check only the first character. sometimes there are inconsistencies like FI or FN for the FINAL - char firstLetter = Character.toUpperCase(prefix.charAt(0)); + char firstLetter = Character.toUpperCase(getPrefix().charAt(0)); switch (firstLetter) { case 'P': return -100; // parking case 'S': { // stage N try { - Integer stageNumber = Integer.valueOf(prefix.substring(1)); + Integer stageNumber = Integer.valueOf(getPrefix().substring(1)); return stageNumber; } catch (NumberFormatException e) { // nothing @@ -121,8 +122,24 @@ public class cgWaypoint implements Comparable<cgWaypoint> { return 0; } + private int order() { + if (cachedOrder == null) { + cachedOrder = computeOrder(); + } + return cachedOrder; + } + @Override public int compareTo(cgWaypoint other) { return order() - other.order(); } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + cachedOrder = null; + } }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/cgeodetail.java b/main/src/cgeo/geocaching/cgeodetail.java index 26ec4f1..0298d41 100644 --- a/main/src/cgeo/geocaching/cgeodetail.java +++ b/main/src/cgeo/geocaching/cgeodetail.java @@ -1012,8 +1012,8 @@ public class cgeodetail extends AbstractActivity { final TextView identification = (TextView) waypointView.findViewById(R.id.identification); ((TextView) waypointView.findViewById(R.id.type)).setText(cgBase.waypointTypes.get(wpt.type)); - if (wpt.prefix.equalsIgnoreCase("OWN") == false) { - identification.setText(wpt.prefix.trim() + "/" + wpt.lookup.trim()); + if (wpt.getPrefix().equalsIgnoreCase("OWN") == false) { + identification.setText(wpt.getPrefix().trim() + "/" + wpt.lookup.trim()); } else { identification.setText(res.getString(R.string.waypoint_custom)); } diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index e6dcb4f..1bceba7 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -63,8 +63,8 @@ public class cgeowaypoint extends AbstractActivity { setTitle(res.getString(R.string.waypoint_title)); } - if (waypoint.prefix.equalsIgnoreCase("OWN") == false) { - identification.setText(waypoint.prefix.trim() + "/" + waypoint.lookup.trim()); + if (waypoint.getPrefix().equalsIgnoreCase("OWN") == false) { + identification.setText(waypoint.getPrefix().trim() + "/" + waypoint.lookup.trim()); } else { identification.setText(res.getString(R.string.waypoint_custom)); } @@ -316,7 +316,7 @@ public class cgeowaypoint extends AbstractActivity { Intent navigateIntent = new Intent(this, cgeonavigate.class); navigateIntent.putExtra("latitude", waypoint.coords.getLatitude()); navigateIntent.putExtra("longitude", waypoint.coords.getLongitude()); - navigateIntent.putExtra("geocode", waypoint.prefix.trim() + "/" + waypoint.lookup.trim()); + navigateIntent.putExtra("geocode", waypoint.getPrefix().trim() + "/" + waypoint.lookup.trim()); navigateIntent.putExtra("name", waypoint.name); cgeonavigate.coordinates.clear(); diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java index 2977407..0ffaa2b 100644 --- a/main/src/cgeo/geocaching/cgeowaypointadd.java +++ b/main/src/cgeo/geocaching/cgeowaypointadd.java @@ -54,7 +54,7 @@ public class cgeowaypointadd extends AbstractActivity { } else { geocode = waypoint.geocode; type = waypoint.type; - prefix = waypoint.prefix; + prefix = waypoint.getPrefix(); lookup = waypoint.lookup; app.setAction(geocode); @@ -331,7 +331,7 @@ public class cgeowaypointadd extends AbstractActivity { final cgWaypoint waypoint = new cgWaypoint(); waypoint.type = type; waypoint.geocode = geocode; - waypoint.prefix = prefix; + waypoint.setPrefix(prefix); waypoint.lookup = lookup; waypoint.name = name; waypoint.coords = new Geopoint(coords.get(0), coords.get(1)); diff --git a/tests/src/cgeo/geocaching/test/cgWaypointTest.java b/tests/src/cgeo/geocaching/test/cgWaypointTest.java new file mode 100644 index 0000000..387e456 --- /dev/null +++ b/tests/src/cgeo/geocaching/test/cgWaypointTest.java @@ -0,0 +1,33 @@ +package cgeo.geocaching.test; + +import junit.framework.Assert; +import android.test.AndroidTestCase; +import cgeo.geocaching.cgWaypoint; + +public class cgWaypointTest extends AndroidTestCase { + + public void testOrder() { + final cgWaypoint wp1 = new cgWaypoint(); + final cgWaypoint wp2 = new cgWaypoint(); + + wp1.setPrefix("PK"); + wp2.setPrefix("X"); + Assert.assertTrue(wp1.compareTo(wp2) < 0); + + wp1.setPrefix("S1"); + Assert.assertTrue(wp1.compareTo(wp2) > 0); + + wp2.setPrefix("S3"); + Assert.assertTrue(wp1.compareTo(wp2) < 0); + + wp1.setPrefix("S10"); + Assert.assertTrue(wp1.compareTo(wp2) > 0); + + wp2.setPrefix("FI"); + Assert.assertTrue(wp1.compareTo(wp2) < 0); + + wp1.setPrefix("OWN"); + Assert.assertTrue(wp1.compareTo(wp2) > 0); + } + +} |
