ru.yandex.bolts.function.Function1B Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bolts Show documentation
Show all versions of bolts Show documentation
Collections utilities used in various Yandex projects
The newest version!
package ru.yandex.bolts.function;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import ru.yandex.bolts.collection.Cf;
import ru.yandex.bolts.internal.ReflectionUtils;
import ru.yandex.bolts.internal.Validate;
@FunctionalInterface
public interface Function1B {
boolean apply(A a);
default Function asFunction() {
return this::apply;
}
static Function1B asFunction1B(final Function f) {
return f::apply;
}
@SuppressWarnings("unchecked")
default Function1B uncheckedCast() {
return (Function1B) this;
}
default Function1B nullIsFalseF() {
return Function1B.notNullF().andF(this);
}
default Function1B compose(Function g) {
return g.andThen(this);
}
default Function1B notF() {
return a -> !apply(a);
}
default Function1B orF(final Function1B p) {
return a -> apply(a) || p.apply(a);
}
default Function1B andF(final Function1B p) {
return a -> apply(a) && p.apply(a);
}
static Function1B equalsF(final B b) {
return Function2B.equalsF().bind2(b);
}
static Function1B sameF(final B b) {
return Function2B.sameF().bind2(b);
}
static Function1B notNullF() {
return o -> o != null;
}
static Function1B trueF() {
return b -> true;
}
static Function1B falseF() {
return b -> false;
}
static Function1B allOfF(final Collection extends Function1B> functions) {
if (functions.size() == 0) return trueF();
else if (functions.size() == 1) return functions.iterator().next();
else return b -> {
for (Function1B super B> Function1B : functions) {
if (!Function1B.apply(b)) return false;
}
return true;
};
}
@SuppressWarnings("unchecked")
static Function1B allOfF(Function1B... functions) {
return allOfF(Cf.list(functions));
}
static Function1B anyOfF(final Collection extends Function1B> functions) {
if (functions.size() == 0) return b -> false;
else if (functions.size() == 1) return functions.iterator().next();
else return b -> {
for (Function1B super B> f : functions) {
if (f.apply(b)) return true;
}
return false;
};
}
@SuppressWarnings("unchecked")
static Function1B anyOfF(Function1B... functions) {
return anyOfF(Cf.list(functions));
}
static Function1B instanceOfF(final Class> cl) {
return cl::isInstance;
}
static Function1B wrap(final Function mapper) {
return mapper::apply;
}
default Function1B memoize() {
return asFunction1B(asFunction().memoize());
}
static Function1B constF(boolean value) {
return b -> value;
}
static Function1B wrap(final Method method) {
Validate.isTrue(method.getReturnType().equals(boolean.class) || method.getReturnType().equals(Boolean.class), "method return type must be boolean or Boolean");
if ((method.getModifiers() & Modifier.STATIC) != 0) {
Validate.isTrue(method.getParameterTypes().length == 1, "static method must have single argument, " + method);
return a -> (Boolean) ReflectionUtils.invoke(method, null, a);
} else {
Validate.isTrue(method.getParameterTypes().length == 0, "instance method must have no arguments, " + method);
return a -> (Boolean) ReflectionUtils.invoke(method, a);
}
}
} //~