AttributeComparator implements Comparator<Object> { public int compare( Object o1, Object o2 ) { IAttribute attribute1 = null; IValue value1 = null; if ( o1 instanceof IAttribute ) { attribute1 = ( IAttribute ) o1; } else if ( o1 instanceof IValue ) { value1 = ( IValue ) o1; attribute1 = value1.getAttribute(); } IAttribute attribute2 = null; IValue value2 = null; if ( o2 instanceof IAttribute ) { attribute2 = ( IAttribute ) o2; } else if ( o2 instanceof IValue ) { value2 = ( IValue ) o2; attribute2 = value2.getAttribute(); } if ( value1 != null && value2 != null ) { if ( getSortByOrDefault() == BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION ) { if ( value1.getAttribute() != value2.getAttribute() ) { return this.compareAttributes( value1.getAttribute(), value2.getAttribute() ); } else { return this.compareValues( value1, value2 ); } } else if ( getSortByOrDefault() == BrowserCoreConstants.SORT_BY_VALUE ) { return this.compareValues( value1, value2 ); } else { return this.equal(); } } else if ( attribute1 != null && attribute2 != null ) { return this.compareAttributes( attribute1, attribute2 ); } else { throw new ClassCastException( "Can only compare two values or two attributes" ); } } AttributeComparator();  AttributeComparator( int sortBy, int defaultSortBy, int sortOrder, int defaultSortOrder, boolean objectClassAndMustAttributesFirst,
        boolean operationalAttributesLast ); int compare( Object o1, Object o2 ); static List<IValue> toSortedValues( IEntry entry );  }
@Test public void testAttributesEqual() { assertEquals( 0, comparator.compare( objectClass, objectClass ) ); assertEquals( 0, comparator.compare( cn, cn ) ); assertEquals( 0, comparator.compare( cn, new Attribute( entry, "cn" ) ) ); }
@Test public void testValuesEqual() { assertEquals( 0, comparator.compare( cn_foo_1, cn_foo_1 ) ); assertEquals( 0, comparator.compare( cn_foo_1, cn_foo_2 ) ); assertEquals( 0, comparator.compare( cn_foo_2, cn_foo_1 ) ); }
@Test public void testMustAttributresDiffer() { int less = comparator.compare( cn, sn ); assertTrue( less < 0 ); int greater = comparator.compare( sn, cn ); assertTrue( greater > 0 ); assertEquals( 0, less + greater ); }
@Test public void testObjectClassMustAttributesDiffer() { int less = comparator.compare( objectClass, cn ); assertTrue( less < 0 ); int greater = comparator.compare( cn, objectClass ); assertTrue( greater > 0 ); assertEquals( 0, less + greater ); }
@Test public void testValuesDiffer() { int less = comparator.compare( cn_bar, cn_foo_1 ); assertTrue( less < 0 ); int greater = comparator.compare( cn_foo_1, cn_bar ); assertTrue( greater > 0 ); assertEquals( 0, less + greater ); }
@Test public void testEmptyValuesEqual() { assertEquals( 0, comparator.compare( cn_empty_1, cn_empty_1 ) ); assertEquals( 0, comparator.compare( cn_empty_1, cn_empty_2 ) ); assertEquals( 0, comparator.compare( cn_empty_2, cn_empty_1 ) ); }
@Test public void testEmptyValuesDiffer() { int less = comparator.compare( cn_empty_1, cn_bar ); assertTrue( less < 0 ); int greater = comparator.compare( cn_bar, cn_empty_1 ); assertTrue( greater > 0 ); assertEquals( 0, less + greater ); }