View Javadoc
1   package org.andromda.timetracker.vo;
2   
3   import java.util.Comparator;
4   
5   /**
6    *
7    */
8   public class UserVOComparator implements Comparator<UserVO>
9   {
10      /**
11       * Compares two UserVO objects based on their usernames. If the usernames are
12       * not available (or null), it compares on id. <p>
13       *
14       * The comparison is null safe and places null objects less than non-null objects.<p>
15       * @param o1
16       * @param o2
17       * @return result
18       */
19      @Override
20      public int compare(UserVO o1, UserVO o2) {
21          int result = 0; // assume equal
22  
23          if (o1 == null) {
24              result = (o2 == null) ? 0 : -1;
25          }
26          else if (o2 == null) {
27              result = 1;
28          }
29          else if ((o1.getUsername() != null) && (o2.getUsername() != null)) {
30              // Both not-null, compare usernames
31              result = o1.getUsername().compareTo(o2.getUsername());
32          }
33          else if ((o1.getId() != null) && (o2.getId() != null)) {
34              // Both not-null but no names, check id's
35              result = o1.getId().compareTo(o2.getId());
36          }
37  
38          return result;
39      }
40  }