io.ultreia.java4all.lang.TwoSide Maven / Gradle / Ivy
package io.ultreia.java4all.lang;
/*-
* #%L
* Java Lang extends by Ultreia.io
* %%
* Copyright (C) 2017 - 2021 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* To be able to program with natural API from java.lang in a two side context.
*
* Created on 12/11/2021.
*
* @author Tony Chemit - [email protected]
* @since 2.0.3
*/
@FunctionalInterface
public interface TwoSide {
/**
* @return {@code} true if this object represents the left side, {@code false} otherwise.
*/
boolean onLeft();
/**
* Choose the correct object for this side.
*
* @param leftCandidate the left side candidate
* @param rightCandidate the right side candidate
* @param type of candidate
* @return the candidate for this side
*/
default O get(O leftCandidate, O rightCandidate) {
return (onLeft() ? leftCandidate : rightCandidate);
}
default void consume(O o, Consumer leftCandidate, Consumer rightCandidate) {
get(leftCandidate, rightCandidate).accept(o);
}
default V apply(O o, Function leftCandidate, Function rightCandidate) {
return get(leftCandidate, rightCandidate).apply(o);
}
default boolean test(O o, Predicate leftCandidate, Predicate rightCandidate) {
return get(leftCandidate, rightCandidate).test(o);
}
default void run(Runnable leftCandidate, Runnable rightCandidate) {
get(leftCandidate, rightCandidate).run();
}
default O call(Callable leftCandidate, Callable rightCandidate) throws Exception {
return get(leftCandidate, rightCandidate).call();
}
}