I've got this Predicate
, which filters my Task
objects based in a date:
Predicate<Task> startDateFiltering = new Predicate<Task>() {
@Override
public boolean apply(Task input) {
return input.getStartDate() != null
&& input.getStartDate().after(date);
}
};
There's no problem to use it as long as date
variable is accessible in the context. However, I'll like to make it reusable and embed it in the Task
class itself, doing something like this:
public static final Predicate<Task> startDateFiltering = new Predicate<Task>() {
@Override
public boolean apply(Task input) {
return input.getStartDate() != null
&& input.getStartDate().after(date);
}
};
In order to access it as Task.startDateFiltering
each time I need it. But how to pass the date
argument to it?
I'd create a static
factory method (or just directly a new instance every time)
public static Predicate<Task> startDateFilteringWithDate(final Date date) {
return new Predicate<Task>() {
@Override
public boolean apply(Task input) {
return input.getStartDate() != null
&& input.getStartDate().after(date);
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With