#741: MyBeanComparator.java

projectforge-business/src/main/java/org/projectforge/framework/utils/MyBeanComparator.java Lines: 139 · Type: Java generic Comparator<T> 139 lines · 103 code · 23 comments · 13 blank

Purpose

Generic reflection-based bean comparator with natural and alphanumeric sort support. 139 lines. Sorts any list of Java beans by one or two properties using runtime reflection — no compile-time type safety on the property name. This is the comparator used by Wicket's SortableDataProvider in every list page — when a user clicks a column header to sort, this comparator is instantiated with the column's property name and sort direction.

Sorting capabilities

Annotation-based sort type detection

The checkAnnotation() method checks if the property's getter/field has the @StringAlphanumericSort annotation. This annotation on a DO field means "sort this column alphanumerically, not lexicographically." Example: a field displaying document names like "Report1", "Report2", "Report10" — lexicographic sort gives "Report1", "Report10", "Report2"; alphanumeric gives "Report1", "Report2", "Report10". Both objects must have the annotation for alphanumeric sort to be used — otherwise falls back to standard String.compareTo().

Error resilience

If property access throws any exception (e.g. NoSuchMethodException for a misspelled property name), the error is logged and 0 is returned — the two objects are considered equal for sorting purposes. This prevents a single bad property name from breaking an entire list page. This is a fail-safe design — the user sees unsorted data rather than an error page.