1   /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: 14/03/2002
5    * Time: 11:55:04
6    * To change template for new class use
7    * Code Style | Class Templates options (Tools | IDE Options).
8    */
9   package com.atlassian.core.user;
10  
11  import com.atlassian.user.User;
12  
13  import java.util.Comparator;
14  
15  /**
16   * This comparator tries to compare two users based on their 'best name'
17   * ie their full name if possible, otherwise their username.
18   *
19   * This comparator completely ignores case.
20   */
21  public class BestNameComparator2 implements Comparator
22  {
23      public int compare(Object o1, Object o2)
24      {
25          User u1 = (User) o1;
26          User u2 = (User) o2;
27  
28          if (u1 == null && u2 == null)
29              return 0;
30          else if (u2 == null) // any value is less than null
31              return -1;
32          else if (u1 == null) // null is greater than any value
33              return 1;
34  
35          String name1 = u1.getFullName();
36          String name2 = u2.getFullName();
37  
38          if (name1 == null)
39              name1 = u1.getName();
40  
41          if (name2 == null)
42              name2 = u2.getName();
43  
44          if (name1 == null || name2 == null)
45  			throw new RuntimeException("Null user name");
46  
47          final int fullNameComparison = name1.toLowerCase().compareTo(name2.toLowerCase());
48          if (fullNameComparison == 0) //if full names are the same, we should check the username (JRA-5847)
49          {
50              return u1.getName().compareTo(u2.getName());
51          }
52          else
53          {
54              return fullNameComparison;
55          }
56      }
57  }