TermComparator implements Comparator<Term> { @Override public int compare(Term o1, Term o2) { if (o1 == o2) { return 0; } if (o1 == null) { return -1; } if (o2 == null) { return 1; } final DateTime s1 = o1.getStart(); final DateTime s2 = o2.getStart(); if (s1 != null && s2 != null) { return s1.compareTo(s2); } final BigInteger y1 = o1.getYear(); final BigInteger y2 = o2.getYear(); if (y1 != null && y2 != null) { return y1.compareTo(y2); } if(o1.getCode() == o2.getCode()) return 0; if(null == o1.getCode()) return -1; if(null == o2.getCode()) return +1; return o1.getCode().compareTo(o2.getCode()); }  @Override int compare(Term o1, Term o2); static final TermComparator INSTANCE; }
@Test public void testCompare() { Term term1 = null; Term term2 = null; Term term3 = new Term(); Term term4 = new Term(); term3.setCode("1142"); term4.setCode("1142"); DateTime start = new DateTime(); term3.setStart(start); term4.setStart(start); assertEquals("They are not equal",0,new TermComparator().compare(term1,term2)); assertEquals("They are not equal",-1,new TermComparator().compare(term1,term3)); assertEquals("They are not equal",+1,new TermComparator().compare(term3,term1)); assertEquals("They are not equal",0,new TermComparator().compare(term3,term4)); term4.setStart(start.plusMinutes(5)); assertEquals("They are not equal",-1,new TermComparator().compare(term3,term4)); assertEquals("They are not equal",+1,new TermComparator().compare(term4,term3)); term1 = new Term(); term2 = new Term(); assertEquals("They are not equal",0,new TermComparator().compare(term1,term2)); term1.setCode("1142"); assertEquals("They are not equal",+1,new TermComparator().compare(term1,term2)); term1.setCode(null); term2.setCode("1142"); assertEquals("They are not equal",-1,new TermComparator().compare(term1,term2)); }