The second decoction of "Try If expression in Java".
For example, you can write FizzBuzz as follows:
public static void main(String... args) {
for (int i = 1; i <= 100; ++i) {
int count = i;
String fizzBuzzed =
If.<String>test(() -> count % 15 == 0).then(() -> "FizzBuzz")
.elif(() -> count % 3 == 0).then(() -> "Fizz")
.elif(() -> count % 5 == 0).then(() -> "Buzz")
.el(() -> Integer.toString(count));
System.out.println(fizzBuzzed);
}
}
Because it handles a nullable value as the value of the if expression Create the following generic class.
import java.util.function.Supplier;
/**
*null can also be taken as a value{@link java.util.Optional}。
*
*Methods not used this time are omitted.
*/
public class MayBe<T> {
/**
*Create an instance where the value exists.
*/
public static <R> MayBe<R> of(R result) {
return new MayBe<>(true, result);
}
/**
*Create an instance that does not have a value.
*/
public static <R> MayBe<R> empty() {
return new MayBe<>(false, null);
}
private final boolean isPresent;
private final T value;
private MayBe(boolean isPresent, T value) {
this.isPresent = isPresent;
this.value = value;
}
/**
*Returns whether the value exists.
*/
public boolean isPresent() {
return this.isPresent;
}
/**
*If the value exists, it returns it, otherwise it returns the value obtained from the argument other.
*/
public T orElseGet(Supplier<T> other) {
return isPresent() ? this.value : other.get();
}
}
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
public class If<R> {
public static <R> If<R> test(BooleanSupplier predicate) {
return new If<>(predicate, null);
}
private final If<R> prev;
private final BooleanSupplier predicate;
private Then<R> then = null;
/**
* @param predicate A predicate for this if.
* @param prev If this if is else if, the previous if.
*Otherwise, null.
*/
private If(BooleanSupplier predicate, If<R> prev) {
this.prev = prev;
this.predicate = predicate;
}
public Then<R> then(Supplier<R> valueSupplier) {
if (this.then != null) {
throw new IllegalStateException("`then`Has already been called.");
}
return this.then = new Then<>(this, valueSupplier);
}
/**
*Evaluate up to this if.
*
* @return The value of the evaluation result.
*/
private MayBe<R> eval() {
if (this.then == null) {
throw new IllegalStateException("`then`Has not been called yet.");
}
if (this.prev != null) {
MayBe<R> prevValue = this.prev.eval();
if (prevValue.isPresent()) {
return prevValue;
}
}
return this.predicate.getAsBoolean()
? MayBe.of(this.then.getThenValue())
: MayBe.empty();
}
/**
* {@link If#then}The class of the object returned by.
*/
public static class Then<R> {
private final If<R> relatedIf;
private final Supplier<R> thenValueSupplier;
/**
* @param relatedIf This then if.
* @param valueSupplier The value to return if this then if is true.
*/
Then(If<R> relatedIf, Supplier<R> valueSupplier) {
this.relatedIf = relatedIf;
this.thenValueSupplier = valueSupplier;
}
public If<R> elif(BooleanSupplier predicate) {
return new If<>(predicate, this.relatedIf);
}
public R el(Supplier<R> valueSupplier) {
return this.relatedIf.eval().orElseGet(valueSupplier);
}
/**
*Returns the value if this then if is true.
*/
R getThenValue() {
return this.thenValueSupplier.get();
}
}
}
--Only the necessary predicates of if are evaluated. --Only those that need to get the value are evaluated. --You can use else if (ʻelif`), so you don't have to nest deeply.
It's easier to read if it's like an immediate function in JavaScript.
public static void main(String... args) {
for (int i = 1; i <= 100; ++i) {
int count = i;
String fizzBuzzed = ((Supplier<String>) () -> {
if (count % 15 == 0) return "FizzBuzz";
else if (count % 3 == 0) return "Fizz";
else if (count % 5 == 0) return "Buzz";
else return Integer.toString(count);
}).get();
System.out.println(fizzBuzzed);
}
}
/that's all
Recommended Posts