@Retention(value=RUNTIME) @Target(value={METHOD,FIELD}) @Documented public @interface ProvidesPropertySort
Comparable value or a Comparator that
should be used to determine property sort ordering in a Java class whose instances back the items in a
Container.
@ProvidesProperty and @ProvidesPropertySort method annotations
can be used to automatically generate a list of PropertyDefs and a PropertyExtractor using a
ProvidesPropertyScanner. This happens automatically when using the appropriate constructors of the various
Container classes in this package.
This annotation is used in conjunction with the @ProvidesProperty
annotation when the value returned by the @ProvidesProperty-annotated method is not
Comparable or does not naturally sort as desired. By declaring a
@ProvidesPropertySort-annotated method for a property, any arbitrary sorting
function can be supplied.
If the annotated method returns a Comparator, then the return value will be used to sort property values;
otherwise, the annotated method must return a sub-type of Comparable, in which case the returned value determines how
that instance's property value sorts (with null values sorting first). If the method returns neither a
Comparator nor a Comparable, an exception is thrown during scanning.
Here is a typical situation where @ProvidesPropertySort is needed: you have
a String property containing a formatted Date, but the way the Date
strings are formatted does not sort in chronological order. This will usually be the case unless your
Date strings are formatted like 2013-03-12, etc.
To address this problem, define a @ProvidesPropertySort-annotated method that
provides a properly sorting Comparable value corresponding to the associated property. For example:
// Container backing object class
public class User {
private Date birthday;
@ProvidesPropertySort // property name "birthday" is implied by method name
public Date getBirthday() {
return this.birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@ProvidesProperty("birthday") // the actual property value is returned here
private Label birthdayProperty() {
return new Label(new SimpleDateFormat("MM/dd/yyyy").format(this.birthday));
}
Alternately, have the @ProvidesPropertySort-annotated method return
a Comparator, for example, if you wanted to sort null values last instead of first:
@ProvidesPropertySort("birthday")
private static Comparator<User> birthdayComparator() {
return new Comparator<User>(User user1, User user2) {
final Date date1 = user1.getBirthday();
final Date date2 = user2.getBirthday();
if (date1 == null && date2 != null)
return 1;
if (date1 != null && date2 == null)
return -1;
if (date1 == null && date2 == null)
return 0;
return date1.compareTo(date2);
};
}
Note that the returned Comparator compares backing instances, not property values,
and that methods returning Comparator may be declared static. The returned
Comparator may be cached by the implementation.
Some details regarding @ProvidesPropertySort annotations on methods:
@ProvidesPropertySort
annotations on other methods are ignored@ProvidesPropertySort annotations on interface methods are supported@ProvidesPropertySort, then the overridding method's annotation takes precedence.
@ProvidesPropertySort for the same
property name, then the declaration in the class which is a sub-type of the other
wins (if the two methods are delcared in the same class, an exception is thrown). This allows subclasses
to "override" which method supplies the sort value for a given property.ProvidesProperty,
ProvidesPropertyScannerpublic abstract String value
Copyright © 2017. All rights reserved.