This article was impulsively created under the influence of this article. 4-year-old daughter "Daddy, use only const?"
Do you like Java, everyone?
I didn't like it very much until recently, but nowadays I like it a lot.
There are a lot of libraries, modern syntax and syntax sugar.
However, I can't accept that there is no If expression, so I made something like that.
public class Iff<T, R> {
Map<Predicate<T>, Supplier<R>> evaluations = new LinkedHashMap<>();
private Supplier<R> elsef;
public Iff(Predicate<T> condition, Supplier<R> then) {
evaluations.put(condition, then);
}
public Iff<T, R> elseIf(Predicate<T> condition, Supplier<R> then) {
evaluations.put(condition, then);
return this;
}
public Iff<T, R> elsef(Supplier<R> elsef) {
this.elsef = elsef;
return this;
}
public Optional<R> eval(T target) {
for (Map.Entry<Predicate<T>, Supplier<R>> entry : this.evaluations.entrySet()) {
if (entry.getKey().test(target)) {
var value = entry.getValue().get();
return Objects.isNull(value) ? Optional.empty() : Optional.of(value);
}
}
if (this.elsef == null) {
return Optional.empty();
} else {
var result = this.elsef.get();
if (Objects.isNull(result)) {
return Optional.empty();
}
return Optional.of(result);
}
}
}
It is a class that takes a T parameter as an argument, checks it, and returns a value of ʻOptional
I wrote a sample as a test.
class IffTest {
@Test
public void testIf() {
//Evaluate a string and return an Integer
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100).eval("hogeFuga");
Assertions.assertEquals(result.get(), 100);
}
@Test
public void testIfElseFirstMatch() {
//You can add elseIf as many times as you like with the elseIf method.
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100)
.elseIf((str) -> str.startsWith("hogeFuga"), () -> 200)
.elseIf((str) -> str.startsWith("hoho"), () -> 300)
.eval("hogeFuga");
Assertions.assertEquals(result.get(), 100);
}
@Test
public void noMatch() {
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100)
.elseIf((str) -> str.startsWith("hogeFuga"), () -> 200)
.elseIf((str) -> str.startsWith("hoho"), () -> 300)
//You can set the behavior in the case of else with the elsef method
.elsef(() -> 400)
.eval(UUID.randomUUID().toString());
Assertions.assertEquals(result.get(), 400);
}
@Test
public void noMatchNotExistsElse() {
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100)
.elseIf((str) -> str.startsWith("hogeFuga"), () -> 200)
.elseIf((str) -> str.startsWith("hoho"), () -> 300)
.eval(UUID.randomUUID().toString());
//If none of the expressions match, Empty will be returned
result.ifPresentOrElse(v -> {
Assertions.fail();
}, () -> {
//If it is else, the answer is correct
});
}
@Test
public void elseResultIsNull() {
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100)
.elseIf((str) -> str.startsWith("hogeFuga"), () -> 200)
.elseIf((str) -> str.startsWith("hoho"), () -> 300)
.elsef(() -> null)
.eval(UUID.randomUUID().toString());
Assertions.assertTrue(result.isEmpty());
}
@Test
public void elseIfResultIsNull() {
var result = new Iff<String, Integer>((str) -> str.startsWith("hoge"), () -> 100)
.elseIf((str) -> str.startsWith("mogemoge"), () -> null)
.elseIf((str) -> str.startsWith("hoho"), () -> 300)
.elsef(() -> null)
.eval("mogemoge");
Assertions.assertTrue(result.isEmpty());
}
}
Recommended Posts