I am studying this Functional Interface subject, and I studies how to use the pre-defined functional interfaces: Predicate and Function.
So I created several implementations:
public static Predicate<String> isStringEmpty = String::isEmpty;
public static Predicate<String> isStringNotEmpty = isStringEmpty.negate();
public static Predicate<ArrayList> arrayListIsEmpty = ArrayList::isEmpty;
public static Predicate<ArrayList> arrayListIsNotEmpty = arrayListIsEmpty.negate();
public static Predicate<String> stringStartsWithA = s -> s.startsWith("A");
public static Predicate<Integer> largerThanNine = n -> n > 9;
public static Function<WebElement, String> getWebElementText = WebElement::getText;
//etc.
And I went on to use them in my code. For example:
isStringEmpty.negate().test("asd");
isStringNotEmpty.test("asd");
stringStartsWithA.negate().test("asd");
isStringNotEmpty.and(isStringEmpty).negate().test("aaa");
csvLine = getWebElementText.apply(leaugeRowElement);
I cannot understand what is our gain in using this form of testing a condition or calling a function? (I am sure there is such!)
How is this different than simply calling a regular function to do these tasks?
Is it for allowing lambdas to use them? Is it to allow passing them as methods arguments?
I surly miss out the real reasoning for this technique.
Can you explain me please?
Thanks!