01: package org.andromda.timetracker.vo;
02:
03: import java.util.Comparator;
04:
05: public class UserVOComparator implements Comparator<UserVO> {
06:
07: /**
08: * Compares two UserVO objects based on their usernames. If the usernames are
09: * not available (or null), it compares on id. <p>
10: *
11: * The comparison is null safe and places null objects less than non-null objects.<p>
12: */
13: public int compare(UserVO o1, UserVO o2) {
14: int result = 0; // assume equal
15:
16: if (o1 == null) {
17: result = (o2 == null) ? 0 : -1;
18: } else if (o2 == null) {
19: result = 1;
20: } else if ((o1.getUsername() != null)
21: && (o2.getUsername() != null)) {
22: // Both not-null, compare usernames
23: result = o1.getUsername().compareTo(o2.getUsername());
24: } else if ((o1.getId() != null) && (o2.getId() != null)) {
25: // Both not-null but no names, check id's
26: result = o1.getId().compareTo(o2.getId());
27: }
28:
29: return result;
30: }
31: }
|