Skip to content

Commit c5c4876

Browse files
committedMay 23, 2013
TRUNK-3992: PersonByNameComparator should be case-insensitive
1 parent 3d2266e commit c5c4876

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
 

‎api/src/main/java/org/openmrs/util/PersonByNameComparator.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*/
1414
package org.openmrs.util;
1515

16-
import java.util.Comparator;
17-
1816
import org.openmrs.Person;
1917
import org.openmrs.PersonName;
2018

19+
import java.util.Comparator;
20+
2121
/**
2222
* A simple person comparator for sorting persons by name. Sorts names based on the following
2323
* precedence: FamilyName, FamilyName2, GivenName, MiddleName, FamilyNamePrefix, FamilyNameSuffix
@@ -40,6 +40,7 @@ public int compare(Person person1, Person person2) {
4040
* @should return negative if personName for person1 comes before that of person2
4141
* @should return positive if personName for person1 comes after that of person2
4242
* @should return zero if the givenName middleName and familyName match
43+
* @should be case insensitive
4344
* @since 1.8
4445
*/
4546
public static int comparePersonsByName(Person person1, Person person2) {
@@ -55,26 +56,34 @@ public static int comparePersonsByName(Person person1, Person person2) {
5556
PersonName name1 = person1.getPersonName();
5657
PersonName name2 = person2.getPersonName();
5758

58-
int ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName(), name2.getFamilyName());
59+
int ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName() != null ? name1.getFamilyName().toLowerCase()
60+
: null, name2.getFamilyName() != null ? name2.getFamilyName().toLowerCase() : null);
5961

6062
if (ret == 0) {
61-
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName2(), name2.getFamilyName2());
63+
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName2() != null ? name1.getFamilyName().toLowerCase()
64+
: null, name2.getFamilyName2() != null ? name2.getFamilyName2().toLowerCase() : null);
6265
}
6366

6467
if (ret == 0) {
65-
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getGivenName(), name2.getGivenName());
68+
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getGivenName() != null ? name1.getGivenName().toLowerCase()
69+
: null, name2.getGivenName() != null ? name2.getGivenName().toLowerCase() : null);
6670
}
6771

6872
if (ret == 0) {
69-
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getMiddleName(), name2.getMiddleName());
73+
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getMiddleName() != null ? name1.getMiddleName().toLowerCase()
74+
: null, name2.getMiddleName() != null ? name2.getMiddleName().toLowerCase() : null);
7075
}
7176

7277
if (ret == 0) {
73-
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNamePrefix(), name2.getFamilyNamePrefix());
78+
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNamePrefix() != null ? name1.getFamilyNamePrefix()
79+
.toLowerCase() : null, name2.getFamilyNamePrefix() != null ? name2.getFamilyNamePrefix().toLowerCase()
80+
: null);
7481
}
7582

7683
if (ret == 0) {
77-
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNameSuffix(), name2.getFamilyNameSuffix());
84+
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNameSuffix() != null ? name1.getFamilyNameSuffix()
85+
.toLowerCase() : null, name2.getFamilyNameSuffix() != null ? name2.getFamilyNameSuffix().toLowerCase()
86+
: null);
7887
}
7988

8089
return ret;

‎api/src/test/java/org/openmrs/util/PersonByNameComparatorTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ public void comparePersonsByName_shouldReturnZeroIfTheGivenNameMiddleNameAndFami
6565
int actualValue = PersonByNameComparator.comparePersonsByName(person1, person2);
6666
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0);
6767
}
68+
69+
@Test
70+
@Verifies(value = "should not be case-sensitive", method = "comparePersonsByName(Person,Person)")
71+
public void comparePersonsByName_shouldNotBeCaseSensitive() throws Exception {
72+
Person person1 = new Person();
73+
person1.addName(new PersonName("GIVENNAME", "MIDDLENAME", "FAMILYNAME"));
74+
Person person2 = new Person();
75+
person2.addName(new PersonName("givenName", "middleName", "familyName"));
76+
int actualValue = PersonByNameComparator.comparePersonsByName(person1, person2);
77+
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0);
78+
}
6879
}

0 commit comments

Comments
 (0)
Please sign in to comment.