From db19f84e944183c71e9786a7b7a24d02092028b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Wilhelmst=C3=B6tter?= Date: Tue, 7 Nov 2023 22:37:21 +0100 Subject: [PATCH] #177: Refactor to sealed types. --- .../jenetics/jpx/format/CompositeFormat.java | 4 +- .../io/jenetics/jpx/format/Elevation.java | 4 +- .../java/io/jenetics/jpx/format/Field.java | 14 ++- .../java/io/jenetics/jpx/format/Format.java | 13 ++- .../jpx/format/LocationFormatter.java | 103 ++++++++++-------- .../jenetics/jpx/format/LongitudeDegree.java | 6 +- .../jenetics/jpx/format/OptionalFormat.java | 4 +- 7 files changed, 91 insertions(+), 57 deletions(-) diff --git a/jpx/src/main/java/io/jenetics/jpx/format/CompositeFormat.java b/jpx/src/main/java/io/jenetics/jpx/format/CompositeFormat.java index 1160e2fe..e566b438 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/CompositeFormat.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/CompositeFormat.java @@ -26,10 +26,10 @@ /** * @author Franz Wilhelmstötter - * @version 2.2 + * @version !__version__! * @since 1.4 */ -class CompositeFormat implements Format { +final class CompositeFormat implements Format { private final List _formats; 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 b7af8aae..45db9c84 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/Elevation.java @@ -21,9 +21,9 @@ import java.util.Optional; /** - * This field allows to access the elevation (in meter) of a given location. + * This field allows accessing the elevation (in meter) of a given location. * - * @version 2.2 + * @version !__version__! * @since 2.2 */ final class Elevation extends Field { 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 fb4a4b77..7756d92a 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/Field.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/Field.java @@ -34,10 +34,20 @@ * elevation. * * @author Franz Wilhelmstötter - * @version 3.0 + * @version !__version__! * @since 1.4 */ -abstract class Field implements Format { +abstract sealed class Field + implements Format + permits + Elevation, + LatitudeDegree, + LatitudeMinute, + LatitudeSecond, + LongitudeDegree, + LongitudeMinute, + LongitudeSecond +{ private static final DecimalFormatSymbols SYMBOLS = DecimalFormatSymbols.getInstance(Locale.US); diff --git a/jpx/src/main/java/io/jenetics/jpx/format/Format.java b/jpx/src/main/java/io/jenetics/jpx/format/Format.java index 3464b9d7..80d51687 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/Format.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/Format.java @@ -26,10 +26,19 @@ * Base interface for formatting and parsing a location. * * @author Franz Wilhelmstötter - * @version 2.2 + * @version !__version__! * @since 1.4 */ -interface Format { +sealed interface Format + permits + CompositeFormat, + ConstFormat, + Field, + LatitudeNS, + LongitudeEW, + OptionalFormat, + Plus +{ /** * Formats the given {@code value} to its string representation. If it is not diff --git a/jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java b/jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java index bf610652..b6def30a 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java @@ -650,52 +650,67 @@ private void validate(){ Elevation E = null; for (var format : _formats) { - if (format instanceof LatitudeDegree ld) { - if (D == null) { - D = ld; - } else { - throw iae("Only one 'D' pattern allowed."); + switch (format) { + case LatitudeDegree latd -> { + if (D == null) { + D = latd; + } else { + throw iae("Only one 'D' pattern allowed."); + } + } + case LatitudeMinute latm -> { + if (M == null) { + M = latm; + } else { + throw iae("Only one 'M' pattern allowed."); + } } - } else if (format instanceof LatitudeMinute lm) { - if (M == null) { - M = lm; - } else { - throw iae("Only one 'M' pattern allowed."); + case LatitudeSecond lats -> { + if (S == null) { + S = lats; + } else { + throw iae("Only one 'S' pattern allowed."); + } + } + case LatitudeNS latns -> { + if (X == null) { + X = (LatitudeNS)format; + } } - } else if (format instanceof LatitudeSecond ls) { - if (S == null) { - S = ls; - } else { - throw iae("Only one 'S' pattern allowed."); + case LongitudeDegree lond -> { + if (d == null) { + d = lond; + } else { + throw iae("Only one 'd' pattern allowed."); + } } - } else if (format instanceof LatitudeNS && X==null) { - X = (LatitudeNS)format; - } else if (format instanceof LongitudeDegree ld) { - if (d == null) { - d = ld; - } else { - throw iae("Only one 'd' pattern allowed."); + case LongitudeMinute lonm -> { + if (m == null) { + m = lonm; + } else { + throw iae("Only one 'm' pattern allowed."); + } } - } else if (format instanceof LongitudeMinute lm) { - if (m == null) { - m = lm; - } else { - throw iae("Only one 'm' pattern allowed."); + case LongitudeSecond lons -> { + if (s == null) { + s = lons; + } else { + throw iae("Only one 's' pattern allowed."); + } } - } else if (format instanceof LongitudeSecond ls) { - if (s == null) { - s = ls; - } else { - throw iae("Only one 's' pattern allowed."); + case LongitudeEW lonew -> { + if (x == null) { + x = lonew; + } } - } else if (format instanceof LongitudeEW lew && x == null) { - x = lew; - } else if (format instanceof Elevation ele) { - if (E == null) { - E = ele; - } else { - throw iae("Only one 'E' pattern allowed."); + case Elevation ele -> { + if (E == null) { + E = ele; + } else { + throw iae("Only one 'E' pattern allowed."); + } } + default -> { } } } @@ -915,7 +930,7 @@ static List tokenize(final String pattern) { switch (c) { case '\'': quote = !quote; - if (token.length() > 0) { + if (!token.isEmpty()) { tokens.add(token.toString()); token.setLength(0); } @@ -925,7 +940,7 @@ static List tokenize(final String pattern) { if (quote) { token.append(c); } else { - if (token.length() > 0) { + if (!token.isEmpty()) { tokens.add(token.toString()); token.setLength(0); } @@ -939,7 +954,7 @@ static List tokenize(final String pattern) { pc != ',' && !quote) { - if (token.length() > 0) { + if (!token.isEmpty()) { tokens.add(token.toString()); token.setLength(0); } @@ -951,7 +966,7 @@ static List tokenize(final String pattern) { break; default: if (PROTECTED_CHARS.contains(pc) || pc == '\'') { - if (token.length() > 0) { + if (!token.isEmpty()) { tokens.add(token.toString()); token.setLength(0); } @@ -963,7 +978,7 @@ static List tokenize(final String pattern) { pc = c; } - if (token.length() > 0) { + if (!token.isEmpty()) { tokens.add(token.toString()); } 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 17286858..afc01cd9 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java @@ -23,16 +23,16 @@ import io.jenetics.jpx.Longitude; /** - * This field allows to access the longitude degrees of a given location. If the + * This field allows accessing the longitude degrees of a given location. If the * pattern has a fractional part, the longitude is rounded to match the pattern. * If the pattern has no fractional part, the longitude is truncated rather than * rounded, on the assumption that the fractional part will be represented by * minutes and seconds. * - * @version 2.2 + * @version !__version__! * @since 2.2 */ -class LongitudeDegree extends Field { +final class LongitudeDegree extends Field { LongitudeDegree(final String pattern) { super(pattern, 'd'); diff --git a/jpx/src/main/java/io/jenetics/jpx/format/OptionalFormat.java b/jpx/src/main/java/io/jenetics/jpx/format/OptionalFormat.java index c9ed4aa0..9dcb4f5d 100644 --- a/jpx/src/main/java/io/jenetics/jpx/format/OptionalFormat.java +++ b/jpx/src/main/java/io/jenetics/jpx/format/OptionalFormat.java @@ -27,10 +27,10 @@ /** * @author Franz Wilhelmstötter - * @version 2.2 + * @version !__version__! * @since 1.4 */ -class OptionalFormat implements Format { +final class OptionalFormat implements Format { private final Format _format;