![JAR search and dependency download from the Maven repository](/logo.png)
org.rapidpm.frp.matcher.Case Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rapidpm-functional-reactive Show documentation
Show all versions of rapidpm-functional-reactive Show documentation
Functional Reactive with Core Java
/**
* Copyright © 2017 Sven Ruppert ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rapidpm.frp.matcher;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.rapidpm.frp.model.Pair;
import org.rapidpm.frp.model.Result;
public class Case extends Pair, Supplier>> {
/**
* Constructor for Case.
*
* @param booleanSupplier a {@link java.util.function.Supplier} object.
* @param resultSupplier a {@link java.util.function.Supplier} object.
*/
public Case(final Supplier booleanSupplier , final Supplier> resultSupplier) {
super(booleanSupplier , resultSupplier);
}
/**
* matchCase.
*
* @param condition a {@link java.util.function.Supplier} object.
* @param value a {@link java.util.function.Supplier} object.
* @param a T object.
* @return a {@link org.rapidpm.frp.matcher.Case} object.
*/
public static Case matchCase(Supplier condition ,
Supplier> value) {
return new Case<>(condition , value);
}
/**
* matchCase.
*
* @param value a {@link java.util.function.Supplier} object.
* @param a T object.
* @return a {@link org.rapidpm.frp.matcher.Case.DefaultCase} object.
*/
public static DefaultCase matchCase(Supplier> value) {
return new DefaultCase<>(() -> true , value);
}
/**
* isMatching.
*
* @return a boolean.
*/
private boolean isMatching() {
return getT1().get();
}
/**
* result.
*
* @return a {@link org.rapidpm.frp.model.Result} object.
*/
public Result result() {
return getT2().get();
}
/**
* match.
*
* @param defaultCase a {@link org.rapidpm.frp.matcher.Case.DefaultCase} object.
* @param matchers a {@link org.rapidpm.frp.matcher.Case} object.
* @param a T object.
* @return a {@link org.rapidpm.frp.model.Result} object.
*/
@SafeVarargs
public static Result match(DefaultCase defaultCase , Case... matchers) {
return Stream
.of(matchers)
.filter(Case::isMatching)
.map(Case::result)
.findFirst()
.orElseGet(defaultCase::result);
}
public static class DefaultCase extends Case {
public DefaultCase(final Supplier booleanSupplier , final Supplier> resultSupplier) {
super(booleanSupplier , resultSupplier);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy