Item implements Comparable<Item> { @Override public int compareTo( Item item ) { int result = Long.compare( getTimeStamp(), item.getTimeStamp() ); if( result == 0 ) { result = getId().compareTo( item.getId() ); } return result; } Item( String id , long timeStamp   ); long getTimeStamp(); String getId(); @Override int compareTo( Item item ); @Override int hashCode(); @Override boolean equals( Object obj );  }
@Test public void compareToWithDifferentItems( CompareItemData data ) { assertThat( data.getX() ).isPrecedentTo( data.getY() ); assertThat( data.getY() ).isSubsequentTo( data.getX() ); assertThat( signum( data.getX().compareTo( data.getY() ) ) == -signum( data.getY().compareTo( data.getX() ) ) ); }
@Test public void compareToWithCoincidentItems() { FakeItem first = new FakeItem( "1", 10L ); FakeItem second = new FakeItem( "1", 10L ); FakeItem subSequent = new FakeItem( "1", 20L ); assertThat( first ).isCoincidentTo( second ); assertThat( second ).isCoincidentTo( first ); assertThat( signum( first.compareTo( second ) ) == -signum( second.compareTo( first ) ) ); assertThat( signum( first.compareTo( subSequent ) ) == signum( second.compareTo( subSequent ) ) ); }