I am working with Java 8 mainly, but meanwhile also creating a library that uses Java 6, such that other people can use it as well in the future, as it is quite interesting.
The problem I have now is that I could very easily solve some issue by using Java 8's Predicate<T>
, however I am unsure how to backport it.
I see the following options available, but they either have issues or I'm unsure how to use them:
Predicate<T>
, this however introduces a relatively big dependency where I do not really need it, also when a Java 8 user wants to use Predicate
, then Google Guava's import for the Predicate
class shows up.Predicate<T>
, no big dependency, still the same issues as mentioned above.TessPredicate<T>
, as Tess will be relevant name in my project, does not feel that nice either.RegexVerficationPredicate
, as it is a predicate in addition to using a regular expression, such that you can also do calculations on the elements. Bank codes, etc. usually have some checksum that you need to compute. Implemented as functional interface, this might be most feasible?java.util.function
from Java 8 to Java 6, is this even possible?How can I solve this?
You can’t backport the java.util.function
package due to the heavy use of default
and static
methods within these interfaces. Such a backport would look quite different.
I recommend creating your own Predicate<T>
interface being as minimal as possible, i.e. having that single abstract method with the same signature as the Java 8 Predicate<T>
. Having the same interface name and method signature like the well-known acts like a self-documentation.
This implies that programmers using Java 8 can still implement your predicate using a lambda expression or method reference (without even importing your interface
). And using a Java 8 predicate is as easy as passing predicate::test
to your method.
Adding a dependency to an entire 3rd party library just for one interface looks nasty to me.