Reputation: 31651
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?
Upvotes: 6
Views: 4726
Reputation: 8432
Wrap it into a method (Factory pattern
kind of).
public static Predicate<Task> startDateFiltering(Date date) {
return new Predicate<Task>() {
public boolean apply(Task input) {
return input.getStartDate() != null
&& input.getStartDate().after(date);
}
}
}
Upvotes: 4
Reputation: 279950
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);
}
};
}
Upvotes: 18