Skip to content

Commit

Permalink
fix: add ComparisonChain
Browse files Browse the repository at this point in the history
  • Loading branch information
markwoon committed Jan 16, 2023
1 parent 814a33a commit ee25ad8
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/java/org/pharmgkb/common/util/ComparisonChain.java
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 src/test/java/org/pharmgkb/common/util/ComparisonChainTest.java
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());
}
}

0 comments on commit ee25ad8

Please sign in to comment.