com.oath.cyclops.matching.Case Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops Show documentation
Show all versions of cyclops Show documentation
Platform for Functional Reactive Programming with Java 8
The newest version!
package com.oath.cyclops.matching;
import static cyclops.control.Option.none;
import cyclops.control.Option;
import cyclops.data.tuple.Tuple1;
import cyclops.data.tuple.Tuple2;
import cyclops.data.tuple.Tuple3;
import cyclops.data.tuple.Tuple4;
import cyclops.data.tuple.Tuple5;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Pattern matching use case contract.
*
* @param the type that is being tested.
* @param the return that the use case will return, it may or note return null.
*/
@FunctionalInterface
public interface Case {
/**
* Test the use case and return in case the test is true.
*
* @param value the value to be tested.
* @return the return, implementations may or not return null to determine success.
*/
Option test(T value);
/**
* Return the test for this user case or for the other. there is no guarantee
* one successful result will be produced.
*
* @param orCase the case bo be tested in case this one fail.
* @return a composite case to perform a XOR operation.
*/
default Case or(Case orCase) {
return (t) -> {
Option val = test(t);
return val.isPresent() ? val : orCase.test(t);
};
}
@AllArgsConstructor
final class CaseFn implements Case {
final Function fn;
@Override
public Option test(T value) {
return Option.some(fn.apply(value));
}
}
@AllArgsConstructor
final class Case0 implements Case {
final Predicate predicate;
final Function supplier;
@Override
public Option test(T value) {
return predicate.test(value) ? Option.some(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class Case1 implements Case, R> {
final Predicate predicate;
final Function, R> supplier;
@Override
public Option test(Tuple1 value) {
return predicate.test(value._1()) ? Option.of(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class Case2 implements Case, R> {
final Predicate predicate1;
final Predicate predicate2;
final Function, ? extends R> supplier;
@Override
public Option test(Tuple2 value) {
return predicate1.test(value._1()) && predicate2.test(value._2()) ? Option.some(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class Case3 implements Case, R> {
final Predicate predicate1;
final Predicate predicate2;
final Predicate predicate3;
final Function, R> supplier;
@Override
public Option test(Tuple3 value) {
return predicate1.test(value._1()) && predicate2.test(value._2()) && predicate3.test(value._3()) ? Option.some(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class Case4 implements Case, R> {
final Predicate predicate1;
final Predicate predicate2;
final Predicate predicate3;
final Predicate predicate4;
final Function, R> supplier;
@Override
public Option test(Tuple4 value) {
return predicate1.test(value._1()) && predicate2.test(value._2()) && predicate3.test(value._3()) && predicate4.test(value._4()) ? Option.some(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class Case5 implements Case, R> {
final Predicate predicate1;
final Predicate predicate2;
final Predicate predicate3;
final Predicate predicate4;
final Predicate predicate5;
final Function, R> supplier;
@Override
public Option test(Tuple5 value) {
return predicate1.test(value._1()) && predicate2.test(value._2()) && predicate3.test(value._3()) && predicate4.test(value._4()) && predicate5.test(value._5()) ? Option.some(supplier.apply(value)) : none();
}
}
@AllArgsConstructor
final class CaseOptional implements Case, R> {
@NonNull
final Supplier supplier0;
@NonNull
final Supplier supplier1;
@Override
public Option test(Optional optional) {
return Option.some(optional.isPresent() ? supplier0.get() : supplier1.get());
}
}
/**
* Marker interface to build a Default Case when no pattern matches.
*
* @param return type.
* @see cyclops.matching.Api#Any(Function)
* @see cyclops.matching.Api#Any(Supplier)
**/
interface Any extends Function {
}
}