org.jooby.funzy.When Maven / Gradle / Ivy
package org.jooby.funzy;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
/**
* Functional idiom for switch/case statement.
*
* Basic example:
* {code@
* import static org.jooby.funzy.When.when;
*
* Object value = ...;
* String result = when(value)
* .is(Number.class, "Got a number")
* .is(String.class, "Got a string")
* .orElse("Unknown");
* System.out.println(result);
* }
*
* Automatic cast example:
*
* {@code
* import static org.jooby.funzy.When.when;
*
* Object value = ...;
* int result = when(value)
* .is(Integer.class, i -> i * 2)
* .orElse(-1);
*
* System.out.println(result);
* }
*
*
* @param Input type.
*/
public class When {
public static class Value {
private final V source;
private final Map predicates = new LinkedHashMap<>();
private Value(final V source) {
this.source = source;
}
public Value is(V value, R result) {
return is(source -> Objects.equals(source, value), v -> result);
}
public Value is(Class predicate, R result) {
return is(predicate::isInstance, v -> result);
}
public Value is(V value, Throwing.Supplier result) {
return is(source -> Objects.equals(source, value), v -> result.get());
}
public Value is(Class predicate, Throwing.Function result) {
return is(predicate::isInstance, result);
}
public Value is(Throwing.Predicate predicate,
Throwing.Supplier result) {
return is(predicate, v -> result.get());
}
public Value is(Throwing.Predicate predicate,
Throwing.Function result) {
predicates.put(predicate, result);
return this;
}
public R get() {
return toOptional().orElseThrow(NoSuchElementException::new);
}
public R orElse(R value) {
return toOptional().orElse(value);
}
public R orElseGet(Throwing.Supplier value) {
return toOptional().orElseGet(value);
}
public R orElseThrow(Throwing.Supplier exception) {
return toOptional().orElseThrow(() -> Throwing.sneakyThrow(exception.get()));
}
public Optional toOptional() {
for (Map.Entry predicate : predicates.entrySet()) {
if (predicate.getKey().test(source)) {
return Optional.ofNullable( (R) predicate.getValue().apply(source));
}
}
return Optional.empty();
}
}
private final V source;
public When(final V source) {
this.source = source;
}
public final static When when(V value) {
return new When<>(value);
}
public Value is(V value, R result) {
Value when = new Value<>(source);
when.is(value, result);
return when;
}
public Value is(Class predicate, R result) {
Value when = new Value<>(source);
when.is(predicate, result);
return when;
}
public Value is(V value, Throwing.Supplier result) {
Value when = new Value<>(source);
when.is(value, result);
return when;
}
public Value is(Class predicate, Throwing.Function result) {
Value when = new Value<>(source);
when.is(predicate, result);
return when;
}
public Value is(Throwing.Predicate predicate,
Throwing.Supplier result) {
Value when = new Value<>(source);
when.is(predicate, result);
return when;
}
public Value is(Throwing.Predicate predicate,
Throwing.Function result) {
Value when = new Value<>(source);
when.is(predicate, result);
return when;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy