diff --git a/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java b/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java index f14c292c..b7af8aae 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java @@ -29,12 +29,7 @@ final class Elevation extends Field { Elevation(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'E'; + super(pattern, 'E'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/Field.java b/jpx/src/main/java/io/jenetics/jpx/format/Field.java index 10c53ade..3fe2041f 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/Field.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/Field.java @@ -43,31 +43,27 @@ abstract class Field implements Format { DecimalFormatSymbols.getInstance(Locale.US); private final String _pattern; + private final char _type; + private boolean _prefixSign = false; private boolean _absolute = false; private final AtomicReference _format = new AtomicReference<>(); - Field(final String pattern) { + Field(final String pattern, final char type) { _pattern = requireNonNull(pattern); - _format.set(new DecimalFormat(toDecimalPattern(pattern), SYMBOLS)); + _type = type; + _format.set(new DecimalFormat(toDecimalPattern(pattern, type), SYMBOLS)); } - private String toDecimalPattern(final String pattern) { - return pattern.replace(type(), '0'); + private static String toDecimalPattern(final String pattern, final char type) { + return pattern.replace(type, '0'); } - /** - * Return the type character of this field. - * - * @return the type character of this field - */ - abstract char type(); - final void setPrefixSign(final boolean b) { _prefixSign = b; - final String decimalPattern = toDecimalPattern(_pattern); + final String decimalPattern = toDecimalPattern(_pattern, _type); final String pattern = _prefixSign ? "+%1$s;-%1$s".formatted(decimalPattern) : decimalPattern; @@ -111,7 +107,9 @@ public String toPattern() { * @return the formatted double value */ final String format(final double value) { - return _format.get().format(value); + synchronized (_format) { + return _format.get().format(value); + } } /** @@ -130,18 +128,22 @@ final double parse(final CharSequence in, final ParsePosition pos) { s = in.subSequence(0, end).toString(); } - final Number n; synchronized (_format) { - n = _format.get().parse(s, pos); - } + final Number n = parse0(s, pos); if (i == pos.getIndex()) { pos.setErrorIndex(i); - throw new ParseException("Not found " + _pattern, in, i); + throw new ParseException("Not found " + toPattern(), in, i); } return n.doubleValue(); } + private Number parse0(final String value, final ParsePosition pos) { + synchronized (_format) { + return _format.get().parse(value, pos); + } + } + static double toMinutes(final double degrees) { final double dd = abs(degrees); diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeDegree.java b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeDegree.java index 6be1b0ea..5566d675 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeDegree.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeDegree.java @@ -35,12 +35,7 @@ final class LatitudeDegree extends Field { LatitudeDegree(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'D'; + super(pattern, 'D'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeMinute.java b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeMinute.java index 4e53e3b5..7195c702 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeMinute.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeMinute.java @@ -30,12 +30,7 @@ final class LatitudeMinute extends Field { LatitudeMinute(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'M'; + super(pattern, 'M'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeSecond.java b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeSecond.java index f789d363..82d999f2 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LatitudeSecond.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LatitudeSecond.java @@ -30,12 +30,7 @@ final class LatitudeSecond extends Field { LatitudeSecond(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'S'; + super(pattern, 'S'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java index e6a05714..17286858 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java @@ -35,12 +35,7 @@ class LongitudeDegree extends Field { LongitudeDegree(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'd'; + super(pattern, 'd'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeMinute.java b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeMinute.java index a08c6bac..b2392dec 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeMinute.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeMinute.java @@ -30,12 +30,7 @@ final class LongitudeMinute extends Field { LongitudeMinute(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 'm'; + super(pattern, 'm'); } @Override diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeSecond.java b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeSecond.java index a837cf33..ea9dca39 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeSecond.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeSecond.java @@ -30,12 +30,7 @@ final class LongitudeSecond extends Field { LongitudeSecond(final String pattern) { - super(pattern); - } - - @Override - char type() { - return 's'; + super(pattern, 's'); } @Override public void parse(