Skip to content

Commit

Permalink
#177: Refactor to sealed types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Nov 7, 2023
1 parent 407040d commit db19f84
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 57 deletions.
4 changes: 2 additions & 2 deletions jpx/src/main/java/io/jenetics/jpx/format/CompositeFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @version 2.2
* @version !__version__!
* @since 1.4
*/
class CompositeFormat implements Format {
final class CompositeFormat implements Format {

private final List<Format> _formats;

Expand Down
4 changes: 2 additions & 2 deletions jpx/src/main/java/io/jenetics/jpx/format/Elevation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions jpx/src/main/java/io/jenetics/jpx/format/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@
* elevation.
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @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);
Expand Down
13 changes: 11 additions & 2 deletions jpx/src/main/java/io/jenetics/jpx/format/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@
* Base interface for formatting and parsing a location.
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @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
Expand Down
103 changes: 59 additions & 44 deletions jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> { }
}
}

Expand Down Expand Up @@ -915,7 +930,7 @@ static List<String> tokenize(final String pattern) {
switch (c) {
case '\'':
quote = !quote;
if (token.length() > 0) {
if (!token.isEmpty()) {
tokens.add(token.toString());
token.setLength(0);
}
Expand All @@ -925,7 +940,7 @@ static List<String> tokenize(final String pattern) {
if (quote) {
token.append(c);
} else {
if (token.length() > 0) {
if (!token.isEmpty()) {
tokens.add(token.toString());
token.setLength(0);
}
Expand All @@ -939,7 +954,7 @@ static List<String> tokenize(final String pattern) {
pc != ',' &&
!quote)
{
if (token.length() > 0) {
if (!token.isEmpty()) {
tokens.add(token.toString());
token.setLength(0);
}
Expand All @@ -951,7 +966,7 @@ static List<String> 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);
}
Expand All @@ -963,7 +978,7 @@ static List<String> tokenize(final String pattern) {
pc = c;
}

if (token.length() > 0) {
if (!token.isEmpty()) {
tokens.add(token.toString());
}

Expand Down
6 changes: 3 additions & 3 deletions jpx/src/main/java/io/jenetics/jpx/format/LongitudeDegree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions jpx/src/main/java/io/jenetics/jpx/format/OptionalFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @version 2.2
* @version !__version__!
* @since 1.4
*/
class OptionalFormat implements Format {
final class OptionalFormat implements Format {

private final Format _format;

Expand Down

0 comments on commit db19f84

Please sign in to comment.