RyaType implements Comparable<RyaType> { @Override public int compareTo(final RyaType o) { if (o == null) { return 1; } final String dataTypeStr = getDataType() != null ? getDataType().stringValue() : null; final String otherDataTypeStr = o.getDataType() != null ? o.getDataType().stringValue() : null; final CompareToBuilder builder = new CompareToBuilder() .append(getData(), o.getData()) .append(dataTypeStr, otherDataTypeStr) .append(getLanguage(), o.getLanguage()); return builder.toComparison(); } RyaType();  RyaType(final String data);  RyaType(final IRI dataType, final String data);  RyaType(final IRI dataType, final String data, final String language); IRI getDataType(); String getData(); void setDataType(final IRI dataType); void setData(final String data); String getLanguage(); void setLanguage(final String language); @Override String toString(); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(final RyaType o);  }
@Test public void testCompareTo() throws Exception { Assert.assertEquals("compareTo(self) should return zero.", 0, aIri.compareTo(aIri)); Assert.assertFalse("compareTo should return nonzero for different data and type.", aIri.compareTo(b) == 0); Assert.assertFalse("compareTo should return nonzero for same data and different datatypes.", a.compareTo(aIri) == 0); Assert.assertFalse("compareTo should return nonzero for same datatype and different data.", bIri.compareTo(aIri) == 0); Assert.assertEquals("compareTo should return zero for different objects with matching data and datatype.", 0, a.compareTo(same)); Assert.assertEquals("compareTo(self) return zero.", 0, aLang.compareTo(aLang)); Assert.assertFalse("compareTo should return nonzero for different languages.", aLang.compareTo(aDiffLang) == 0); Assert.assertFalse("compareTo should return nonzero for same datatype and language and different data.", aLang.compareTo(bLang) == 0); }
@Test public void testCompareToNullFields() throws Exception { Assert.assertEquals("[has no nulls].compareTo([has null data]) should return 1", 1, a.compareTo(nullData)); Assert.assertEquals("[has no nulls].compareTo([has null type]) should return 1 if data is equal", 1, a.compareTo(nullType)); Assert.assertEquals("[has null data].compareTo([has no nulls]) should return -1", -1, nullData.compareTo(a)); Assert.assertEquals("[has null type].compareTo([has no nulls]) should return -1 if data is equal", -1, nullType.compareTo(a)); Assert.assertEquals("[has null type].compareTo([has null data]) should return 1", 1, nullType.compareTo(nullData)); Assert.assertEquals("[has no nulls].compareTo([has null lang]) should return 1", 1, aLang.compareTo(nullLang)); }
@Test public void testCompareToSymmetry() throws Exception { int forward = Integer.signum(a.compareTo(b)); int backward = Integer.signum(b.compareTo(a)); Assert.assertEquals("Comparison of different values with same type should yield opposite signs.", forward, backward * -1); forward = Integer.signum(bIri.compareTo(b)); backward = Integer.signum(b.compareTo(bIri)); Assert.assertEquals("Comparison of same values with different types should yield opposite signs.", forward, backward*-1); forward = Integer.signum(aIri.compareTo(b)); backward = Integer.signum(b.compareTo(aIri)); Assert.assertEquals("Comparison of different values with different types should yield opposite signs.", forward, backward * -1); }
@Test public void testCompareToTransitive() throws Exception { final int sign = Integer.signum(a.compareTo(b)); Assert.assertEquals("compareTo(a,b) and compareTo(b,c) should have the same sign.", sign, Integer.signum(b.compareTo(c))); Assert.assertEquals("if a > b > c, compareTo(a,c) should be consistent.", sign, Integer.signum(a.compareTo(c))); }