diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2011-11-09 16:48:59 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2011-11-09 17:58:49 +0100 |
| commit | ae1fd0ffc874998e3e2bf45e4e345d5702c0faf4 (patch) | |
| tree | 7ed45babd467c50258b7a46fde287c16b0aefa7f /main/src | |
| parent | 56564023546e75b2c9f04fae8967d29de738a354 (diff) | |
| download | cgeo-ae1fd0ffc874998e3e2bf45e4e345d5702c0faf4.zip cgeo-ae1fd0ffc874998e3e2bf45e4e345d5702c0faf4.tar.gz cgeo-ae1fd0ffc874998e3e2bf45e4e345d5702c0faf4.tar.bz2 | |
Use a less general formatter
The number of formats we want to offer is limited. Since we are not
developing a library, we can restrict ourselves to the ones we need
internally. This simplifies the code a lot, and ensures maximum
performance.
Part of issue #769.
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/Geopoint.java | 28 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/GeopointFormatter.java | 210 |
2 files changed, 29 insertions, 209 deletions
diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java index 07e3d38..6b00331 100644 --- a/main/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java @@ -233,32 +233,6 @@ public final class Geopoint * @see GeopointFormatter * @return formatted coordinates */ - public String format(GeopointFormatter format) - { - return format.format(this); - } - - /** - * Returns formatted coordinates. - * - * @param format - * the desired format - * @see GeopointFormatter - * @return formatted coordinates - */ - public String format(String format) - { - return GeopointFormatter.format(format, this); - } - - /** - * Returns formatted coordinates. - * - * @param format - * the desired format - * @see GeopointFormatter - * @return formatted coordinates - */ public String format(GeopointFormatter.Format format) { return GeopointFormatter.format(format, this); @@ -267,7 +241,7 @@ public final class Geopoint /** * Returns formatted coordinates with default format. * Default format is decimalminutes, e.g. N 52° 36.123 E 010° 03.456 - * + * * @return formatted coordinates */ public String toString() diff --git a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java index 16951ad..2c0dd3b 100644 --- a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java +++ b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java @@ -1,177 +1,45 @@ package cgeo.geocaching.geopoint; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.Locale; /** * Formatting of Geopoint. */ public class GeopointFormatter { - private static final Pattern pattern = Pattern.compile("%([yx])(\\d)?([ndms])"); - private final String format; - private final Format enumFormat; - /** - * A few default formats. They can be parsed hardcoded, so the use of them - * can improve the performance. + * Predefined formats. */ - public enum Format - { - LAT_LON_DECDEGREE("%y6d %x6d"), - LAT_LON_DECMINUTE("%yn %yd° %y3m %xn %xd° %x3m"), - LAT_LON_DECMINUTE_PIPE("%yn %yd° %y3m | %xn %xd° %x3m"), - LAT_LON_DECSECOND("%yn %yd° %ym' %ys\" %xn %xd° %xm' %xs\""), - LAT_DECDEGREE("%y6d"), - LAT_DECMINUTE("%yn %yd° %y3m"), - LAT_DECMINUTE_RAW("%yn %yd %y3m"), - LAT_DECSECOND("%yn %yd° %ym' %ys\""), - LON_DECDEGREE("%x6d"), - LON_DECMINUTE("%xn %xd° %x3m"), - LON_DECMINUTE_RAW("%xn %xd %x3m"), - LON_DECSECOND("%xn %xd° %xm' %xs\""); - - private final String format; - - Format(String format) - { - this.format = format; - } - - public String toString() - { - return format; - } - } - - /** - * Creates a new formatter with given format-string. - * - * @param format - * the format-string - * @see format() - */ - public GeopointFormatter(final String format) - { - enumFormat = null; - this.format = format; - } - - /** - * Creates a new formatter with given default-format. - * - * @param format - * one of the default formats - * @see GeopointFormatter.Format - */ - public GeopointFormatter(final Format format) - { - enumFormat = format; - this.format = format.toString(); + public enum Format { + /** Example: "-0,123456 10,123456" */ + LAT_LON_DECDEGREE, + /** Example: "-0.123456,10.123456" (unlocalized) */ + LAT_LON_DECDEGREE_COMMA, + /** Example: "W 5° 12,345 N 10° 12,345" */ + LAT_LON_DECMINUTE, + /** Example: "W 5° 12,345 | N 10° 12,345" */ + LAT_LON_DECMINUTE_PIPE, + /** Example: "W 5° 12,345" */ + LAT_DECMINUTE, + /** Example: "W 5 12,345" */ + LAT_DECMINUTE_RAW, + /** Example: "N 10° 12,345" */ + LON_DECMINUTE, + /** Example: "N 10 12,345" */ + LON_DECMINUTE_RAW; } /** * Formats a Geopoint. * - * Syntax: - * %[dir][precision][value] - * - * [dir] - * y = latitude - * x = longitude - * - * [precision] (optional) - * 0..9, number of digits after the decimal point - * - * [value] - * n = direction - * d = degree - * m = minute - * s = second - * - * Example: - * "%yn %yd° %y3m" = "N 52° 36.123" - * - * All other characters are not interpreted and can be used. - * * @param gp * the Geopoint to format * @param format - * the format-string with syntax from above - * @return the formatted coordinates - */ - public static String format(final String format, final Geopoint gp) - { - final Matcher matcher = pattern.matcher(format); - final StringBuffer formattedResult = new StringBuffer(); - - while (matcher.find()) - { - StringBuilder replacement = new StringBuilder(); - - final double coord = (matcher.group(1).equals("y")) ? gp.getLatitude() : gp.getLongitude(); - - if (matcher.group(3).equals("n")) - { - if (matcher.group(1).equals("y")) - { - replacement.append((coord < 0) ? "S" : "N"); - } - else - { - replacement.append((coord < 0) ? "W" : "E"); - } - } - else if (matcher.group(3).equals("d")) - { - if (null == matcher.group(2)) - { - replacement.append(String.format("%0" + ((matcher.group(1).equals("y")) ? "2." : "3.") + "0f", Math.floor(Math.abs(coord)))); - } - else - { - replacement.append(String.format("%0" + ((matcher.group(1).equals("y")) ? "2." : "3.") + Integer.parseInt(matcher.group(2)) + "f", coord)); - } - } - else if (matcher.group(3).equals("m")) - { - final double value = Math.abs(coord); - final double minutes = (value - Math.floor(value)) * 60; - replacement.append(String.format("%02." + ((null == matcher.group(2)) ? 0 : Integer.parseInt(matcher.group(2))) + "f", (null == matcher.group(2)) ? Math.floor(minutes) : minutes)); - } - else if (matcher.group(3).equals("s")) - { - final double value = Math.abs(coord); - final double minutes = (value - Math.floor(value)) * 60; - replacement.append(String.format("%02." + ((null == matcher.group(2)) ? 0 : Integer.parseInt(matcher.group(2))) + "f", (minutes - Math.floor(minutes)) * 60)); - } - - matcher.appendReplacement(formattedResult, replacement.toString()); - } - - matcher.appendTail(formattedResult); - - return formattedResult.toString(); - } - - /** - * Formats a Geopoint. - * - * @param gp - * the Geopoint to format - * @param format - * one of the default formats - * @see cgeo.geocaching.GeopointFormatter.Format + * one of the predefined formats * @return the formatted coordinates */ public static String format(final Format format, final Geopoint gp) { - // Don't parse often used formats - - if (format == Format.LAT_LON_DECDEGREE) { - return String.format("%.6f %.6f", gp.getLatitude(), gp.getLongitude()); - } - final double latSigned = gp.getLatitude(); final double lonSigned = gp.getLongitude(); final double lat = Math.abs(latSigned); @@ -184,6 +52,12 @@ public class GeopointFormatter final char lonDir = lonSigned < 0 ? 'W' : 'E'; switch (format) { + case LAT_LON_DECDEGREE: + return String.format("%.6f %.6f", latSigned, lonSigned); + + case LAT_LON_DECDEGREE_COMMA: + return String.format((Locale) null, "%.6f,%.6f", latSigned, lonSigned); + case LAT_LON_DECMINUTE: return String.format("%c %02.0f° %.3f %c %03.0f° %.3f", latDir, latFloor, latMin, lonDir, lonFloor, lonMin); @@ -203,38 +77,10 @@ public class GeopointFormatter case LON_DECMINUTE_RAW: return String.format("%c %03.0f %.3f", lonDir, lonFloor, lonMin); - - default: - return format(format.toString(), gp); } - } - /** - * Formats a Geopoint with the format of this instance. - * - * @param gp - * the Geopoint to format - * @return the formatted coordinates of the Geopoint - */ - public String format(final Geopoint gp) - { - if (null == enumFormat) - { - return format(format, gp); - } - else - { - return format(enumFormat, gp); - } + // Keep the compiler happy even though it cannot happen + return null; } - /** - * Returns the format of this instance. - * - * @return the format of this instance. - */ - public String toString() - { - return format; - } } |
