Skip to content

Commit

Permalink
Simplifies SpinOption(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Mar 7, 2024
1 parent f9cffba commit 26e6f5d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@ public IntegerSpinOption(String name, Consumer<Integer> trigger, int defaultValu
protected Integer parse(String value) {
return Integer.parseInt(value);
}

@Override
protected int compare(Integer first, Integer other) {
return first.compareTo(other);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@ public LongSpinOption(String name, Consumer<Long> trigger, long defaultValue, lo
protected Long parse(String value) {
return Long.parseLong(value);
}

@Override
protected int compare(Long first, Long other) {
return first.compareTo(other);
}

}
7 changes: 3 additions & 4 deletions src/main/java/com/fathzer/jchess/uci/option/SpinOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import java.util.function.Consumer;

abstract class SpinOption<N extends Number> extends Option<N> {
abstract class SpinOption<N extends Number & Comparable<N>> extends Option<N> {
private final N defaultValue;
private final N min;
private final N max;

protected SpinOption(String name, Consumer<N> trigger, N defaultValue, N min, N max) {
super(name, trigger);
if (compare(defaultValue, min)<0 || compare(defaultValue, max)>0) {
if (defaultValue.compareTo(min)<0 || defaultValue.compareTo(max)>0) {
throw new IllegalArgumentException("default ("+defaultValue+") is not between min ("+min+") and max("+max+")");
}
this.defaultValue = defaultValue;
Expand All @@ -19,7 +19,6 @@ protected SpinOption(String name, Consumer<N> trigger, N defaultValue, N min, N
}

protected abstract N parse(String value);
protected abstract int compare(N first, N other);

@Override
Type getType() {
Expand All @@ -32,7 +31,7 @@ public void setValue(String value) {
throw new IllegalArgumentException();
}
final N val = parse(value);
if (compare(val, max)>0 || compare(val, min)<0) {
if (val.compareTo(max)>0 || val.compareTo(min)<0) {
throw new IllegalArgumentException();
}
setCastedValue(val);
Expand Down

0 comments on commit 26e6f5d

Please sign in to comment.