diff --git a/src/main/java/com/fathzer/jchess/bot/Option.java b/src/main/java/com/fathzer/jchess/bot/Option.java index 2af65d1..bd04130 100644 --- a/src/main/java/com/fathzer/jchess/bot/Option.java +++ b/src/main/java/com/fathzer/jchess/bot/Option.java @@ -11,7 +11,7 @@ public enum Type { protected Option(String name, T defaultValue) { super(defaultValue); - if (name==null || defaultValue==null) { + if (name==null) { throw new IllegalArgumentException(); } this.name = name; diff --git a/src/main/java/com/fathzer/jchess/bot/options/ButtonOption.java b/src/main/java/com/fathzer/jchess/bot/options/ButtonOption.java index 89cc151..a2bf23b 100644 --- a/src/main/java/com/fathzer/jchess/bot/options/ButtonOption.java +++ b/src/main/java/com/fathzer/jchess/bot/options/ButtonOption.java @@ -3,7 +3,6 @@ import com.fathzer.jchess.bot.Option; //TODO Is it usefull? -//FIXME it will never inform listener as value of Void type can't change public class ButtonOption extends Option { public ButtonOption(String name) { @@ -19,4 +18,9 @@ public Type getType() { public boolean isValid(Void value) { return true; } + + @Override + public void setValue(Void value) { + super.fireChange(value, value); + } } diff --git a/src/main/java/com/fathzer/jchess/bot/options/ComboOption.java b/src/main/java/com/fathzer/jchess/bot/options/ComboOption.java index 43b5535..3154ccd 100644 --- a/src/main/java/com/fathzer/jchess/bot/options/ComboOption.java +++ b/src/main/java/com/fathzer/jchess/bot/options/ComboOption.java @@ -9,7 +9,7 @@ public class ComboOption extends Option { public ComboOption(String name, String defaultValue, Set values) { super(name, defaultValue); - if (!values.contains(defaultValue) || values.isEmpty()) { + if (defaultValue==null || !values.contains(defaultValue)) { throw new IllegalArgumentException(); } this.values = values; diff --git a/src/main/java/com/fathzer/util/Observable.java b/src/main/java/com/fathzer/util/Observable.java new file mode 100644 index 0000000..1e8ebcb --- /dev/null +++ b/src/main/java/com/fathzer/util/Observable.java @@ -0,0 +1,35 @@ +package com.fathzer.util; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.function.BiConsumer; + +public class Observable { + private Collection> listeners; + private T value; + + public Observable(T value) { + this.value = value; + this.listeners = new LinkedList<>(); + } + + public void addListener(BiConsumer listener) { + this.listeners.add(listener); + } + + public void setValue(T value) { + if (!this.value.equals(value)) { + final T previous = this.value; + this.value = value; + fireChange(previous, value); + } + } + + protected void fireChange(final T previous, T value) { + listeners.forEach(c -> c.accept(previous, value)); + } + + public T getValue() { + return this.value; + } +} diff --git a/src/test/java/com/fathzer/jchess/bot/options/OptionsTest.java b/src/test/java/com/fathzer/jchess/bot/options/OptionsTest.java index b0d49d9..aa31065 100644 --- a/src/test/java/com/fathzer/jchess/bot/options/OptionsTest.java +++ b/src/test/java/com/fathzer/jchess/bot/options/OptionsTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; import org.junit.jupiter.api.Test; @@ -25,6 +26,7 @@ public void accept(T t, T u) { void testCombo() { final Set abSet = Set.of("a","b"); assertThrows(IllegalArgumentException.class, () -> new ComboOption("combo", "c", abSet)); + assertThrows(IllegalArgumentException.class, () -> new ComboOption("combo", null, abSet)); assertThrows(IllegalArgumentException.class, () -> new ComboOption(null, "a", abSet)); final Option combo = new ComboOption("combo", "a", Set.of("a","b")); assertEquals("a", combo.getValue()); @@ -118,6 +120,17 @@ void testCheck() { @Test void testButton() { - fail("Not Yet Implemented"); + assertThrows(IllegalArgumentException.class, () -> new ButtonOption(null)); + final ButtonOption button = new ButtonOption("button"); + final AtomicBoolean called = new AtomicBoolean(); + OptionListener listener = new OptionListener<>() { + @Override + public void accept(Void a, Void b) { + called.set(true); + } + }; + button.addListener(listener); + button.setValue(null); + assertTrue(called.get()); } }