net.yudichev.jiotty.appliance.Command Maven / Gradle / Ivy
package net.yudichev.jiotty.appliance;
import java.util.Optional;
import java.util.stream.Stream;
@SuppressWarnings("OverloadedVarargsMethod") // no confusion possible here
public interface Command> {
Optional accept(Visitor visitor);
default U 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 U acceptOrFail(Visitor... visitors) {
return accept(visitors).orElseThrow(() -> new IllegalArgumentException(String.format(
"Command %s is not supported", this)));
}
interface Visitor {
}
}