LotComparator implements Comparator<Map<String, Object>> { public int compare(Map<String, Object> o1, Map<String, Object> o2) { int compareValue = 0; String dnulot1 = (String)o1.get(CadastrappConstants.PB_LOT_ID); String dnulot2 = (String)o2.get(CadastrappConstants.PB_LOT_ID); if(dnulot1.matches(numberRegex) && dnulot2.matches(numberRegex)){ compareValue = Integer.valueOf(dnulot1).compareTo(Integer.valueOf(dnulot2)); } else if(dnulot1.matches(numberStartRegex) && dnulot2.matches(numberStartRegex)){ compareValue = Integer.valueOf(dnulot1.replaceAll("[^\\d]", "")).compareTo(Integer.valueOf(dnulot2.replaceAll("[^\\d]", ""))); if(compareValue == 0){ compareValue = dnulot1.replaceAll("[0-9]", "").compareTo(dnulot2.replaceAll("[0-9]", "")); } } else if(dnulot1.matches(numberStartRegex)){ compareValue = -1; } else if(dnulot2.matches(numberStartRegex)){ compareValue = 1; } else{ compareValue = dnulot1.compareTo(dnulot2); } return compareValue; }  int compare(Map<String, Object> o1, Map<String, Object> o2);  }
@Test public void testEqualsNumbersCompare() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "001"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "001"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertEquals(0, result); }
@Test public void testEqualsNumberStringCompare() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "001"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "001A"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertEquals(-1, result); }
@Test public void testEqualsStringCompare() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "A"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "A"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertEquals(0, result); }
@Test public void testBiggerNbOne() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "002"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "001"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertEquals(1, result); }
@Test public void testBiggerNbTwo() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "011"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "101"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertEquals(-1, result); }
@Test public void testBiggerNumberStringCompare() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "01"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "A"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertTrue(result < 0); }
@Test public void testBiggerStringNumberCompare() { Map<String, Object> lotsInformation1 = new HashMap<String, Object>(); lotsInformation1.put(CadastrappConstants.PB_LOT_ID, "A"); Map<String, Object> lotsInformation2 = new HashMap<String, Object>(); lotsInformation2.put(CadastrappConstants.PB_LOT_ID, "01"); int result = lotComparator.compare(lotsInformation1, lotsInformation2); assertTrue(result > 0); }