-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/org/pharmgkb/common/util/ComparisonChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.pharmgkb.common.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
|
||
/** | ||
* This is a helper class to simplify implementing {@link Comparable}. | ||
* | ||
* @author Mark Woon | ||
*/ | ||
public class ComparisonChain { | ||
private int m_comparison = 0; | ||
|
||
|
||
@SuppressWarnings("rawtypes") | ||
public ComparisonChain compare(Comparable a, Comparable b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compare(a, b); | ||
return this; | ||
} | ||
|
||
|
||
public ComparisonChain compareIgnoreCase(@Nullable String a, @Nullable String b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compareIgnoreCase(a, b); | ||
return this; | ||
} | ||
|
||
|
||
public ComparisonChain compareNumbers(@Nullable String a, @Nullable String b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compareNumbers(a, b); | ||
return this; | ||
} | ||
|
||
|
||
@SuppressWarnings("rawtypes") | ||
public ComparisonChain compare(@Nullable Collection<? extends Comparable> a, | ||
@Nullable Collection<? extends Comparable> b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compareCollection(a, b); | ||
return this; | ||
} | ||
|
||
|
||
@SuppressWarnings("rawtypes") | ||
public ComparisonChain compare(@Nullable Map a, @Nullable Map b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compareMap(a, b); | ||
return this; | ||
} | ||
|
||
|
||
public ComparisonChain compareCollectionOfMaps(@Nullable Collection a, @Nullable Collection b) { | ||
if (m_comparison != 0) { | ||
return this; | ||
} | ||
m_comparison = ComparatorUtils.compareCollectionOfMaps(a, b); | ||
return this; | ||
} | ||
|
||
public int result() { | ||
return m_comparison; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/org/pharmgkb/common/util/ComparisonChainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.pharmgkb.common.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
/** | ||
* @author Mark Woon | ||
*/ | ||
class ComparisonChainTest { | ||
|
||
@Test | ||
void testChain() { | ||
assertTrue(new ComparisonChain() | ||
.compare("a", "A") | ||
.result() > 0); | ||
|
||
assertEquals(0, new ComparisonChain() | ||
.compareIgnoreCase("a", "A") | ||
.result()); | ||
|
||
assertTrue(new ComparisonChain() | ||
.compare("a", "b") | ||
.result() < 0); | ||
|
||
assertTrue(new ComparisonChain() | ||
.compare("1", "2") | ||
.result() < 0); | ||
|
||
assertTrue(new ComparisonChain() | ||
.compare("12", "2") | ||
.result() < 0); | ||
|
||
assertTrue(new ComparisonChain() | ||
.compareNumbers("12", "2") | ||
.result() > 0); | ||
|
||
|
||
List<String> listA = new ArrayList<>(); | ||
listA.add("a"); | ||
List<String> listB = new ArrayList<>(); | ||
listB.add("b"); | ||
|
||
assertEquals(1, new ComparisonChain() | ||
.compare("a", "a") | ||
.compare(listB, listA) | ||
.result()); | ||
|
||
assertEquals(1, new ComparisonChain() | ||
.compare(listB, listA) | ||
.compare("a", "b") | ||
.result()); | ||
|
||
|
||
Map<String, String> mapA = new HashMap<>(); | ||
mapA.put("a", "A"); | ||
Map<String, String> mapB = new HashMap<>(); | ||
mapB.put("b", "B"); | ||
|
||
assertEquals(-1, new ComparisonChain() | ||
.compare(listA, listA) | ||
.compare(mapA, mapB) | ||
.result()); | ||
|
||
|
||
List<Map<String, String>> listMapA = new ArrayList<>(); | ||
listMapA.add(mapA); | ||
List<Map<String, String>> listMapB = new ArrayList<>(); | ||
listMapB.add(mapB); | ||
assertEquals(-1, new ComparisonChain() | ||
.compareIgnoreCase("a", "A") | ||
.compare(listB, listB) | ||
.compareCollectionOfMaps(listMapA, listMapB) | ||
.result()); | ||
} | ||
} |