PriorityServiceComparator implements Comparator<Object>, Serializable { @Override public int compare(Object o1, Object o2) { int prio = getPriority(o1) - getPriority(o2); if (prio < 0) { return 1; } else if (prio > 0) { return -1; } else { return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); } } private  PriorityServiceComparator(); static PriorityServiceComparator getInstance(); @Override int compare(Object o1, Object o2); static int getPriority(Object o); @SuppressWarnings({ "rawtypes", "unchecked" }) static int getPriority(Class type);  }
@Test public void compare() throws Exception { assertThat(PriorityServiceComparator.getInstance().compare("a", "b")==0).isTrue(); assertThat(PriorityServiceComparator.getInstance().compare(getClass(), getClass())==0).isTrue(); assertThat(PriorityServiceComparator.getInstance().compare(new A(), new SystemPropertySource())==-1).isTrue(); assertThat(PriorityServiceComparator.getInstance().compare(new SystemPropertySource(), new A())==1).isTrue(); }