aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/cgBase.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2011-09-10 22:03:12 +0200
committerSamuel Tardieu <sam@rfc1149.net>2011-09-10 23:03:07 +0200
commitb3f1702e4e7e57e52402be69c10a84e5caa85f82 (patch)
tree4695d62018029ea88c1fe18da39ddb04e2c18a31 /src/cgeo/geocaching/cgBase.java
parent533de08de704f514b9beab60aac722c2591cdfd2 (diff)
downloadcgeo-b3f1702e4e7e57e52402be69c10a84e5caa85f82.zip
cgeo-b3f1702e4e7e57e52402be69c10a84e5caa85f82.tar.gz
cgeo-b3f1702e4e7e57e52402be69c10a84e5caa85f82.tar.bz2
Factor distance parser into its own class and static method
Also, match against one regexp to increase efficiency. Named constants have been used instead of hardcoded ones to convert various lengths to kilometers.
Diffstat (limited to 'src/cgeo/geocaching/cgBase.java')
-rw-r--r--src/cgeo/geocaching/cgBase.java34
1 files changed, 12 insertions, 22 deletions
diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java
index 775b267..72743b9 100644
--- a/src/cgeo/geocaching/cgBase.java
+++ b/src/cgeo/geocaching/cgBase.java
@@ -71,6 +71,7 @@ import android.util.Log;
import android.widget.EditText;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.files.LocParser;
+import cgeo.geocaching.geopoint.DistanceParser;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.utils.CollectionUtils;
@@ -172,7 +173,9 @@ public class cgBase {
private static final Pattern patternViewstateFieldCount = Pattern.compile("id=\"__VIEWSTATEFIELDCOUNT\"[^(value)]+value=\"(\\d+)\"[^>]+>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
private static final Pattern patternViewstates = Pattern.compile("id=\"__VIEWSTATE(\\d*)\"[^(value)]+value=\"([^\"]+)\"[^>]+>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
private static final Pattern patternIsPremium = Pattern.compile("<span id=\"ctl00_litPMLevel\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
- public static final double kmInMiles = 1 / 1.609344;
+ public static final double miles2km = 1.609344;
+ public static final double feet2km = 0.0003048;
+ public static final double yards2km = 0.0009144;
public static final double deg2rad = Math.PI / 180;
public static final double rad2deg = 180 / Math.PI;
public static final float erad = 6371.0f;
@@ -2172,7 +2175,12 @@ public class cgBase {
try {
final Matcher matcherDistance = PATTERN_TRACKABLE_Distance.matcher(page);
if (matcherDistance.find() && matcherDistance.groupCount() > 0) {
- trackable.distance = parseDistance(matcherDistance.group(1));
+ try {
+ trackable.distance = DistanceParser.parseDistance(matcherDistance.group(1), settings.units);
+ } catch (NumberFormatException e) {
+ trackable.distance = null;
+ throw e;
+ }
}
} catch (Exception e) {
// failed to parse trackable distance
@@ -2407,24 +2415,6 @@ public class cgBase {
return text.trim();
}
- public static Double parseDistance(String dst) {
- Double distance = null;
-
- final Pattern pattern = Pattern.compile("([0-9\\.,]+)[ ]*(km|mi)", Pattern.CASE_INSENSITIVE);
- final Matcher matcher = pattern.matcher(dst);
- while (matcher.find()) {
- if (matcher.groupCount() > 1) {
- if (matcher.group(2).equalsIgnoreCase("km")) {
- distance = Double.valueOf(matcher.group(1));
- } else {
- distance = Double.valueOf(matcher.group(1)) / kmInMiles;
- }
- }
- }
-
- return distance;
- }
-
public static double getDistance(final Geopoint coords1, final Geopoint coords2) {
if (coords1 == null || coords2 == null) {
return 0;
@@ -2455,7 +2445,7 @@ public class cgBase {
}
if (settings.units == cgSettings.unitsImperial) {
- distance *= kmInMiles;
+ distance /= miles2km;
if (distance > 100) {
return String.format(Locale.getDefault(), "%.0f", Double.valueOf(Math.round(distance))) + " mi";
} else if (distance > 0.5) {
@@ -2491,7 +2481,7 @@ public class cgBase {
String unit = "km/h";
if (this.settings.units == cgSettings.unitsImperial) {
- kph *= kmInMiles;
+ kph /= miles2km;
unit = "mph";
}