Package 

Object ComparableContract


  • 
    public class ComparableContract
    
                        

    Tests the various properties the Comparable contract is supposed to uphold. If you think this is confusing, realize that like equals(), it is often not possible to implement a one-sided compareTo() correctly with inheritance - it's a broken concept, but it's still used so often that you have to do your best with it.

    I got the idea of contract-based testing from watching Bill Venners: https://www.youtube.com/watch?v=bCTZQi2dpl8

    • Method Summary

      Modifier and Type Method Description
      final static <S extends Comparable<S>> Unit testCompareTo(S least1, S least2, S middle1, S middle2, S greatest1, S greatest2) Tests the various properties the Comparable contract is supposed to uphold.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • testCompareTo

         final static <S extends Comparable<S>> Unit testCompareTo(S least1, S least2, S middle1, S middle2, S greatest1, S greatest2)

        Tests the various properties the Comparable contract is supposed to uphold. Also tests that the behavior of compareTo() is compatible with equals() and hashCode() which is strongly suggested, but not actually required. Write your own test if you don't want that. Expects three pair of unique objects. Within a pair, the two objects should be equal. Both objects in the first pair are less than the ones in the second pair, which in turn are less than the objects in the third pair.

        See note in class documentation.