| BestNameComparator | Line # 21 | 20 | 10 | 65.7% |
0.6571429
|
| (1) | |||
| Result | |||
|
0.6571429
|
test.atlassian.core.user.TestUserUtils.testGetUsersFromGroups
test.atlassian.core.user.TestUserUtils.testGetUsersFromGroups
|
1 PASS | |
| 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.opensymphony.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 BestNameComparator implements Comparator | |
| 22 | { | |
| 23 | 3 |
public int compare(Object o1, Object o2) |
| 24 | { | |
| 25 | 3 | User u1 = (User) o1; |
| 26 | 3 | User u2 = (User) o2; |
| 27 | ||
| 28 | 3 | if (u1 == null && u2 == null) |
| 29 | 0 | return 0; |
| 30 | 3 | else if (u2 == null) // any value is less than null |
| 31 | 0 | return -1; |
| 32 | 3 | else if (u1 == null) // null is greater than any value |
| 33 | 0 | return 1; |
| 34 | ||
| 35 | 3 | String name1 = u1.getFullName(); |
| 36 | 3 | String name2 = u2.getFullName(); |
| 37 | ||
| 38 | 3 | if (name1 == null) |
| 39 | 3 | name1 = u1.getName(); |
| 40 | ||
| 41 | 3 | if (name2 == null) |
| 42 | 3 | name2 = u2.getName(); |
| 43 | ||
| 44 | 3 | if (name1 == null || name2 == null) |
| 45 | 0 | throw new RuntimeException("Null user name"); |
| 46 | ||
| 47 | 3 | final int fullNameComparison = name1.toLowerCase().compareTo(name2.toLowerCase()); |
| 48 | 3 | if (fullNameComparison == 0) //if full names are the same, we should check the username (JRA-5847) |
| 49 | { | |
| 50 | 0 | return u1.getName().compareTo(u2.getName()); |
| 51 | } | |
| 52 | else | |
| 53 | { | |
| 54 | 3 | return fullNameComparison; |
| 55 | } | |
| 56 | } | |
| 57 | } | |
|
||||||||||