RelationshipComparator implements Comparator<ProjectRelationship<?, ?>> { @Override public int compare( final ProjectRelationship<?, ?> one, final ProjectRelationship<?, ?> two ) { if ( one.getType() == two.getType() ) { if ( one.getPomLocation() .equals( RelationshipConstants.POM_ROOT_URI ) && !two.getPomLocation() .equals( RelationshipConstants.POM_ROOT_URI ) ) { return -1; } else if ( !one.getPomLocation() .equals( RelationshipConstants.POM_ROOT_URI ) && two.getPomLocation() .equals( RelationshipConstants.POM_ROOT_URI ) ) { return 1; } int res = one.getDeclaring().compareTo( two.getDeclaring() ); if ( res == 0 ) { res = one.getIndex() - two.getIndex(); } return res; } else { return one.getType() .ordinal() - two.getType() .ordinal(); } } private  RelationshipComparator(); @Override int compare( final ProjectRelationship<?, ?> one, final ProjectRelationship<?, ?> two ); static final RelationshipComparator INSTANCE; }
@Test public void testCompareTransitivity() { final ProjectVersionRef nodeW = projectVersion( "w", "w", "1.0" ); final ProjectVersionRef nodeX = projectVersion( "x", "x", "1.0" ); final ProjectVersionRef nodeY = projectVersion( "y", "y", "1.0" ); final ProjectVersionRef nodeZ = projectVersion( "z", "z", "1.0" ); DependencyRelationship relA = dependency( POM_ROOT_URI, nodeW, nodeX, 0, false, false ); DependencyRelationship relB = dependency( POM_ROOT_URI, nodeX, nodeY, 0, false, false ); DependencyRelationship relC = dependency( POM_ROOT_URI, nodeY, nodeZ, 0, false, false ); assertThat( "relA < relB must be true to perform the test", RelationshipComparator.INSTANCE.compare( relA, relB ), equalTo( -1 ) ); assertThat( "relB < relC must be true to perform the test", RelationshipComparator.INSTANCE.compare( relB, relC ), equalTo( -1 ) ); int compare = RelationshipComparator.INSTANCE.compare( relA, relC ); compare = ((Float) Math.signum( compare )).intValue(); assertThat( "relA < relC must be true because relA < relB and relB < relC, but it is not", compare, equalTo( -1 ) ); }