Skip to content

Commit

Permalink
#152: Extract additional methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Nov 13, 2021
1 parent 173e9b2 commit c2d4ac7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 59 deletions.
7 changes: 1 addition & 6 deletions jpx/src/main/java/io/jenetics/jpx/format/Elevation.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
final class Elevation extends Field {

Elevation(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'E';
super(pattern, 'E');
}

@Override
Expand Down
36 changes: 19 additions & 17 deletions jpx/src/main/java/io/jenetics/jpx/format/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<NumberFormat> _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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions jpx/src/main/java/io/jenetics/jpx/format/LatitudeDegree.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@
final class LatitudeDegree extends Field {

LatitudeDegree(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'D';
super(pattern, 'D');
}

@Override
Expand Down
7 changes: 1 addition & 6 deletions jpx/src/main/java/io/jenetics/jpx/format/LatitudeMinute.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
final class LatitudeMinute extends Field {

LatitudeMinute(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'M';
super(pattern, 'M');
}

@Override
Expand Down
7 changes: 1 addition & 6 deletions jpx/src/main/java/io/jenetics/jpx/format/LatitudeSecond.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
final class LatitudeSecond extends Field {

LatitudeSecond(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'S';
super(pattern, 'S');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@
class LongitudeDegree extends Field {

LongitudeDegree(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'd';
super(pattern, 'd');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
final class LongitudeMinute extends Field {

LongitudeMinute(final String pattern) {
super(pattern);
}

@Override
char type() {
return 'm';
super(pattern, 'm');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c2d4ac7

Please sign in to comment.