Skip to content

Commit

Permalink
変更検知リスナーの仕様をシンプルに
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Jan 4, 2023
1 parent e739bee commit f04d6d4
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 197 deletions.
81 changes: 12 additions & 69 deletions common/src/main/java/net/kunmc/lab/configlib/CollectionValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class CollectionValue<T extends Collection<E>, E, U extends CollectionValue<T, E, U>> extends Value<T, U> {
private final transient List<BiFunction<T, CommandContext, Boolean>> addListeners = new ArrayList<>();
private final transient List<BiFunction<T, CommandContext, Boolean>> removeListeners = new ArrayList<>();
private final transient List<Function<CommandContext, Boolean>> clearListeners = new ArrayList<>();
private final transient List<Consumer<T>> addListeners = new ArrayList<>();
private final transient List<Consumer<T>> removeListeners = new ArrayList<>();
private final transient List<Runnable> clearListeners = new ArrayList<>();
private transient boolean addable = true;
private transient boolean removable = true;
private transient boolean clearable = true;
Expand Down Expand Up @@ -48,31 +45,13 @@ protected abstract String incorrectArgumentMessageForAdd(String entryName,

protected abstract String invalidValueMessageForAdd(String entryName, T value, CommandContext ctx);

public U onAdd(Consumer<T> listener) {
return onAdd((v, ctx) -> {
listener.accept(v);
});
}

public U onAdd(BiConsumer<T, CommandContext> listener) {
return onAdd((v, ctx) -> {
listener.accept(v, ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public U onAdd(BiFunction<T, CommandContext, Boolean> listener) {
public final U onAdd(Consumer<T> listener) {
addListeners.add(listener);
return ((U) this);
}

protected boolean onAddValue(T newValue, CommandContext ctx) {
return addListeners.stream()
.map(x -> x.apply(newValue, ctx))
.reduce(false, (a, b) -> a || b);
protected final void onAddValue(T newValue) {
addListeners.forEach(x -> x.accept(newValue));
}

protected String succeedMessageForAdd(String entryName, T value) {
Expand Down Expand Up @@ -102,31 +81,13 @@ protected abstract String incorrectArgumentMessageForRemove(String entryName,

protected abstract String invalidValueMessageForRemove(String entryName, T value, CommandContext ctx);

public U onRemove(Consumer<T> listener) {
return onRemove((v, ctx) -> {
listener.accept(v);
});
}

public U onRemove(BiConsumer<T, CommandContext> listener) {
return onRemove((v, ctx) -> {
listener.accept(v, ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public U onRemove(BiFunction<T, CommandContext, Boolean> listener) {
public final U onRemove(Consumer<T> listener) {
removeListeners.add(listener);
return ((U) this);
}

protected boolean onRemoveValue(T newValue, CommandContext ctx) {
return removeListeners.stream()
.map(x -> x.apply(newValue, ctx))
.reduce(false, (a, b) -> a || b);
protected final void onRemoveValue(T newValue) {
removeListeners.forEach(x -> x.accept(newValue));
}

protected String succeedMessageForRemove(String entryName, T value) {
Expand All @@ -142,31 +103,13 @@ public U clearableByCommand(boolean clearable) {
return ((U) this);
}

public U onClear(Runnable listener) {
return onClear(ctx -> {
listener.run();
});
}

public U onClear(Consumer<CommandContext> listener) {
return onClear(ctx -> {
listener.accept(ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public U onClear(Function<CommandContext, Boolean> listener) {
public final U onClear(Runnable listener) {
clearListeners.add(listener);
return ((U) this);
}

protected boolean onClearValue(CommandContext ctx) {
return clearListeners.stream()
.map(x -> x.apply(ctx))
.reduce(false, (a, b) -> a || b);
protected final void onClearValue() {
clearListeners.forEach(Runnable::run);
}

protected final String clearMessage(String entryName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ private static void applySet(Command command, Field field, SingleValue value, Co
ctx.sendFailure(value.invalidValueMessage(entryName, newValue, ctx));
return;
}

if (value.onModifyValue(newValue, ctx)) {
return;
}
value.onModifyValue(newValue);

value.value(newValue);
ctx.sendSuccess(value.succeedModifyMessage(entryName));
Expand Down
83 changes: 13 additions & 70 deletions common/src/main/java/net/kunmc/lab/configlib/MapValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import net.kunmc.lab.commandlib.ArgumentBuilder;
import net.kunmc.lab.commandlib.CommandContext;
import net.kunmc.lab.configlib.function.TriFunction;
import org.apache.logging.log4j.util.TriConsumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class MapValue<K, V, T extends MapValue<K, V, T>> extends Value<Map<K, V>, T> {
private final transient List<TriFunction<K, V, CommandContext, Boolean>> putListeners = new ArrayList<>();
private final transient List<BiFunction<K, CommandContext, Boolean>> removeListeners = new ArrayList<>();
private final transient List<Function<CommandContext, Boolean>> clearListeners = new ArrayList<>();
private final transient List<BiConsumer<K, V>> putListeners = new ArrayList<>();
private final transient List<BiConsumer<K, V>> removeListeners = new ArrayList<>();
private final transient List<Runnable> clearListeners = new ArrayList<>();
private transient boolean puttable = true;
private transient boolean removable = true;
private transient boolean clearable = true;
Expand Down Expand Up @@ -73,31 +70,13 @@ protected String invalidValueMessageForPut(String entryName, V v, CommandContext
return "";
}

public T onPut(BiConsumer<K, V> listener) {
return onPut((k, v, ctx) -> {
listener.accept(k, v);
});
}

public T onPut(TriConsumer<K, V, CommandContext> listener) {
return onPut((k, v, ctx) -> {
listener.accept(k, v, ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public T onPut(TriFunction<K, V, CommandContext, Boolean> listener) {
public final T onPut(BiConsumer<K, V> listener) {
putListeners.add(listener);
return ((T) this);
}

protected boolean onPutValue(K k, V v, CommandContext ctx) {
return putListeners.stream()
.map(x -> x.apply(k, v, ctx))
.reduce(false, (a, b) -> a || b);
protected final void onPutValue(K k, V v) {
putListeners.forEach(x -> x.accept(k, v));
}

protected String succeedMessageForPut(String entryName, K k, V v) {
Expand Down Expand Up @@ -133,31 +112,13 @@ protected String invalidKeyMessageForRemove(String entryName, K k, CommandContex
return String.format("%sは%sに追加されていませんでした.", keyToString(k), entryName);
}

public T onRemove(Consumer<K> listener) {
return onRemove((k, ctx) -> {
listener.accept(k);
});
}

public T onRemove(BiConsumer<K, CommandContext> listener) {
return onRemove((k, ctx) -> {
listener.accept(k, ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public T onRemove(BiFunction<K, CommandContext, Boolean> listener) {
public final T onRemove(BiConsumer<K, V> listener) {
removeListeners.add(listener);
return ((T) this);
}

protected boolean onRemoveKey(K k, CommandContext ctx) {
return removeListeners.stream()
.map(x -> x.apply(k, ctx))
.reduce(false, (a, b) -> a || b);
protected final void onRemoveKey(K k, V v) {
removeListeners.forEach(x -> x.accept(k, v));
}

protected String succeedMessageForRemove(String entryName, K k, V v) {
Expand All @@ -168,36 +129,18 @@ protected boolean clearableByCommand() {
return clearable;
}

public T clearableByCommand(boolean clearable) {
public final T clearableByCommand(boolean clearable) {
this.clearable = clearable;
return ((T) this);
}

public T onClear(Runnable listener) {
return onClear(ctx -> {
listener.run();
});
}

public T onClear(Consumer<CommandContext> listener) {
return onClear(ctx -> {
listener.accept(ctx);
return false;
});
}

/**
* @return true if you want to cancel event, otherwise false
*/
public T onClear(Function<CommandContext, Boolean> listener) {
public final T onClear(Runnable listener) {
clearListeners.add(listener);
return ((T) this);
}

protected boolean onClearMap(CommandContext ctx) {
return clearListeners.stream()
.map(x -> x.apply(ctx))
.reduce(false, (a, b) -> a || b);
protected void onClearMap() {
clearListeners.forEach(Runnable::run);
}

protected String clearMessage(String entryName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public ModifyAddCommand(Field field, CollectionValue value, CommonBaseConfig con
return;
}

if (value.onAddValue(newValue, ctx)) {
return;
}
value.onAddValue(newValue);

((Collection) value.value()).addAll(newValue);
ctx.sendSuccess(value.succeedMessageForAdd(entryName, newValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public ModifyClearCommand(Field field, CollectionValue value, CommonBaseConfig c
super("clear");

execute(ctx -> {
if (value.onClearValue(ctx)) {
return;
}
value.onClearValue();

((Collection) value.value()).clear();
ctx.sendSuccess(value.clearMessage(field.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ private void exec(double amount, CommandContext ctx) {
}

Number newValue = value.copySub(amount);
if (value.onModifyValue(newValue, ctx)) {
return;
}
value.onModifyValue(newValue);

value.value(newValue);
ctx.sendSuccess(value.succeedModifyMessage(entryName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public void exec(double amount, CommandContext ctx) {
}

Number newValue = value.copyAdd(amount);
if (value.onModifyValue(newValue, ctx)) {
return;
}
value.onModifyValue(newValue);

value.value(newValue);
ctx.sendSuccess(value.succeedModifyMessage(entryName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public ModifyMapClearCommand(Field field, MapValue value, CommonBaseConfig confi
super("clear");

execute(ctx -> {
if (value.onClearMap(ctx)) {
return;
}
value.onClearMap();

value.clear();
ctx.sendSuccess(value.clearMessage(field.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public ModifyMapPutCommand(Field field, MapValue value, CommonBaseConfig config)
return;
}

if (value.onPutValue(k, v, ctx)) {
return;
}
value.onPutValue(k, v);

value.put(k, v);
ctx.sendSuccess(value.succeedMessageForPut(entryName, k, v));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ public ModifyMapRemoveCommand(Field field, MapValue value, CommonBaseConfig conf
ctx.sendFailure(value.invalidKeyMessageForRemove(entryName, k, ctx));
return;
}

if (value.onRemoveKey(k, ctx)) {
return;
}

Object v = value.remove(k);
ctx.sendSuccess(value.succeedMessageForRemove(entryName, k, v));
value.onRemoveKey(k, v);

ctx.sendSuccess(value.succeedMessageForRemove(entryName, k, v));
config.saveConfigIfPresent();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public ModifyRemoveCommand(Field field, CollectionValue value, CommonBaseConfig
return;
}

if (value.onRemoveValue(newValue, ctx)) {
return;
}
value.onRemoveValue(newValue);

((Collection) value.value()).removeAll(newValue);
ctx.sendSuccess(value.succeedMessageForRemove(entryName, newValue));
Expand Down
Loading

0 comments on commit f04d6d4

Please sign in to comment.