CourseSectionCodeComparator implements Comparator<CourseSection> { @Override public int compare(CourseSection o1, CourseSection o2) { if (o1 == o2) { return 0; } if (o1 == null) { return -1; } if (o2 == null) { return 1; } 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(CourseSection o1, CourseSection o2); static final CourseSectionCodeComparator INSTANCE; }
@Test public void testCompare() { CourseSection courseSection1 = null; CourseSection courseSection2 = null; CourseSection courseSection3 = new CourseSection(); CourseSection courseSection4 = new CourseSection(); courseSection3.setCode("010"); courseSection4.setCode("010"); assertEquals("They are not equal",0,new CourseSectionCodeComparator().compare(courseSection1,courseSection2)); assertEquals("They are not equal",-1,new CourseSectionCodeComparator().compare(courseSection1,courseSection3)); assertEquals("They are not equal",+1,new CourseSectionCodeComparator().compare(courseSection3,courseSection1)); assertEquals("They are not equal",0,new CourseSectionCodeComparator().compare(courseSection3,courseSection4)); courseSection4.setCode("011"); assertEquals("They are not equal",-1,new CourseSectionCodeComparator().compare(courseSection3,courseSection4)); assertEquals("They are not equal",+1,new CourseSectionCodeComparator().compare(courseSection4,courseSection3)); courseSection1 = new CourseSection(); courseSection2 = new CourseSection(); assertEquals("They are not equal",0,new CourseSectionCodeComparator().compare(courseSection1,courseSection2)); courseSection1.setCode("311"); assertEquals("They are not equal",+1,new CourseSectionCodeComparator().compare(courseSection1,courseSection2)); courseSection1.setCode(null); courseSection2.setCode("311"); assertEquals("They are not equal",-1,new CourseSectionCodeComparator().compare(courseSection1,courseSection2)); }