net.yudichev.jiotty.appliance.Command Maven / Gradle / Ivy
package net.yudichev.jiotty.appliance;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
@SuppressWarnings("OverloadedVarargsMethod") // no confusion possible here
public interface Command {
Optional accept(Visitor visitor);
default String name() {
return toString();
}
default T acceptOrFail(Visitor visitor) {
return accept(visitor).orElseThrow(() -> new IllegalArgumentException(String.format(
"Command %s is not supported", this)));
}
@SuppressWarnings("unchecked")
default Optional accept(Visitor... visitors) {
return Stream.of(visitors)
.map(this::accept)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
@SuppressWarnings("unchecked")
default T acceptOrFail(Visitor... visitors) {
return accept(visitors).orElseThrow(() -> new IllegalArgumentException(String.format(
"Command %s is not supported", this)));
}
default T map(Function super Command, T> mapper) {
return mapper.apply(this);
}
interface Visitor {
}
}