com.github.tonivade.purefun.Pattern3 Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
import static com.github.tonivade.purefun.Function3.cons;
import static com.github.tonivade.purefun.Precondition.checkNonNull;
public class Pattern3 implements PartialFunction3 {
private final Pattern1, R> pattern;
private Pattern3() {
this(Pattern1.build());
}
private Pattern3(Pattern1, R> pattern) {
this.pattern = checkNonNull(pattern);
}
@Override
public boolean isDefinedAt(A a, B b, C c) {
return pattern.isDefinedAt(Tuple.of(a, b, c));
}
@Override
public R apply(A a, B b, C c) {
return pattern.apply(Tuple.of(a, b, c));
}
public static Pattern3 build() {
return new Pattern3<>();
}
public ThenStep, A, B, C, R> when(Matcher3 matcher) {
return handler -> add(matcher, handler);
}
public ThenStep, A, B, C, R> otherwise() {
return handler -> add(Matcher3.otherwise(), handler);
}
private Pattern3 add(Matcher3 matcher, Function3 handler) {
return new Pattern3<>(pattern.add(matcher.tupled(), handler.tupled()));
}
@FunctionalInterface
public interface ThenStep {
P then(Function3 handler);
default P returns(R value) {
return then(cons(value));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy