Skip to content

Commit

Permalink
Makes button option work
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 8, 2024
1 parent b5bd9f2 commit 5afe8b8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/fathzer/jchess/bot/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {

public ButtonOption(String name) {
Expand All @@ -19,4 +18,9 @@ public Type getType() {
public boolean isValid(Void value) {
return true;
}

@Override
public void setValue(Void value) {
super.fireChange(value, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ComboOption extends Option<String> {

public ComboOption(String name, String defaultValue, Set<String> values) {
super(name, defaultValue);
if (!values.contains(defaultValue) || values.isEmpty()) {
if (defaultValue==null || !values.contains(defaultValue)) {
throw new IllegalArgumentException();
}
this.values = values;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/fathzer/util/Observable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fathzer.util;

import java.util.Collection;
import java.util.LinkedList;
import java.util.function.BiConsumer;

public class Observable<T> {
private Collection<BiConsumer<T,T>> listeners;
private T value;

public Observable(T value) {
this.value = value;
this.listeners = new LinkedList<>();
}

public void addListener(BiConsumer<T,T> 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;
}
}
15 changes: 14 additions & 1 deletion src/test/java/com/fathzer/jchess/bot/options/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@ public void accept(T t, T u) {
void testCombo() {
final Set<String> 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<String> combo = new ComboOption("combo", "a", Set.of("a","b"));
assertEquals("a", combo.getValue());
Expand Down Expand Up @@ -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<Void> listener = new OptionListener<>() {
@Override
public void accept(Void a, Void b) {
called.set(true);
}
};
button.addListener(listener);
button.setValue(null);
assertTrue(called.get());
}
}

0 comments on commit 5afe8b8

Please sign in to comment.