So I'd like to be able to store a boolean
test without storing the value (true
/false
), I'd like to be able to pass multiple of these boolean
tests through methods. Possibly even switching arguments and operators. Is there anyway to do this?
I've seen some people save them as String
s then use Interpreter
from BeanShell, but is there another way? Or should I try my best at creating a way to do this?
In Java 8, you can use a lambda. java.util.function
has a number of classes that represent functions that return booleans (Predicate
, BiPredicate
, IntPredicate
, DoublePredicate
, etc.), and those types can be used to hold an expression that returns a boolean. Example:
BiPredicate<Integer,Integer> pred = (x, y) -> x > y;
Now pred
contains a function that tests whether the first integer is greater, but that function isn't actually evaluated until you use something like
boolean result = pred.test(arg1, arg2);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments