EObjectNameComparator implements Comparator<T>, Serializable { @Override public int compare(T arg0, T arg1) { if(arg0==null) return 1; EStructuralFeature feature1 = arg0.eClass().getEStructuralFeature(featureName); String n1 = (String) (feature1 != null ? arg0.eGet(feature1) : arg0.toString()); EStructuralFeature feature2 = arg0.eClass().getEStructuralFeature(featureName); String n2 = (String) (feature2 != null ? arg1.eGet(feature2) : arg1.toString()); if(n1==null && n2==null) return 0; if(n1==null) return 1; if(n2==null) return -1; return COLLATOR.compare(n1, n2); } EObjectNameComparator();  EObjectNameComparator(String featureName); @Override int compare(T arg0, T arg1);  }
@Test public void testCompare() { folder1.setName("a"); folder2.setName("b"); assertEquals(-1, fixture.compare(folder1, folder2)); folder1.setName("a"); folder2.setName("B"); assertEquals(-1, fixture.compare(folder1, folder2)); folder1.setName("A"); folder2.setName("b"); assertEquals(-1, fixture.compare(folder1, folder2)); folder1.setName("A"); folder2.setName("B"); assertEquals(-1, fixture.compare(folder1, folder2)); }
@Test public void testCompareOtherFeature() { Review r1 = PropertiesFactory.eINSTANCE.createReview(); Review r2 = PropertiesFactory.eINSTANCE.createReview(); EObjectNameComparator<Review> c = new EObjectNameComparator<Review>(PropertiesPackage.Literals.REVIEW__KEY.getName()); r1.setKey("a"); r2.setKey("b"); assertEquals(-1, c.compare(r1, r2)); r1.setKey("a"); r2.setKey("B"); assertEquals(-1, c.compare(r1, r2)); r1.setKey("A"); r2.setKey("b"); assertEquals(-1, c.compare(r1, r2)); r1.setKey("A"); r2.setKey("B"); assertEquals(-1, c.compare(r1, r2)); }