DeliveryBucketImpl implements DeliveryBucket { @Override public int compareTo(final DeliveryBucket bucket) { int compare = supplier.compareTo(bucket.getSupplier()); if (compare == 0) { compare = group.compareTo(bucket.getGroup()); if (compare == 0) { compare = qualifier.compareTo(bucket.getQualifier()); } } return compare; } DeliveryBucketImpl(final String group, final String supplier);  DeliveryBucketImpl(final String group, final String supplier, final String qualifier); @Override String getGroup(); @Override String getSupplier(); @Override String getQualifier(); @Override int compareTo(final DeliveryBucket bucket); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testCompareTo() throws Exception { assertEquals(0, new DeliveryBucketImpl("D1", "S001").compareTo(new DeliveryBucketImpl("D1", "S001"))); assertTrue(0 > new DeliveryBucketImpl("D1", "S001").compareTo(new DeliveryBucketImpl("D2", "S001"))); assertTrue(0 < new DeliveryBucketImpl("D2", "S001").compareTo(new DeliveryBucketImpl("D1", "S001"))); assertFalse(0 == new DeliveryBucketImpl("D1", "S002").compareTo(new DeliveryBucketImpl("D1", "S001"))); assertFalse(0 == new DeliveryBucketImpl("D1", "S001").compareTo(new DeliveryBucketImpl("D1", "S002"))); assertTrue(0 > new DeliveryBucketImpl("D1", "S001").compareTo(new DeliveryBucketImpl("D2", "S002"))); assertTrue(0 < new DeliveryBucketImpl("D1", "S002").compareTo(new DeliveryBucketImpl("D2", "S001"))); assertTrue(0 > new DeliveryBucketImpl("D2", "S001").compareTo(new DeliveryBucketImpl("D1", "S002"))); assertTrue(0 < new DeliveryBucketImpl("D2", "S002").compareTo(new DeliveryBucketImpl("D1", "S001"))); assertTrue(0 == new DeliveryBucketImpl("D1", "S001", "Q1").compareTo(new DeliveryBucketImpl("D1", "S001", "Q1"))); assertTrue(0 > new DeliveryBucketImpl("D1", "S001").compareTo(new DeliveryBucketImpl("D1", "S001", "Q1"))); assertTrue(0 < new DeliveryBucketImpl("D1", "S001", "Q1").compareTo(new DeliveryBucketImpl("D1", "S001"))); final SortedMap<DeliveryBucket, Object> buckets = new TreeMap<>(); buckets.put(new DeliveryBucketImpl("D1", "S001"), new Object()); buckets.put(new DeliveryBucketImpl("D2", "S001"), new Object()); buckets.put(new DeliveryBucketImpl("D3", "S001"), new Object()); buckets.put(new DeliveryBucketImpl("D3", "S001", "Q1"), new Object()); buckets.put(new DeliveryBucketImpl("D4", "S001"), new Object()); buckets.put(new DeliveryBucketImpl("D5", "S001"), new Object()); buckets.put(new DeliveryBucketImpl("D1", "S002"), new Object()); buckets.put(new DeliveryBucketImpl("D2", "S002"), new Object()); buckets.put(new DeliveryBucketImpl("D3", "S002", "Q1"), new Object()); buckets.put(new DeliveryBucketImpl("D3", "S002", "Q2"), new Object()); buckets.put(new DeliveryBucketImpl("D4", "S002"), new Object()); buckets.put(new DeliveryBucketImpl("D5", "S002"), new Object()); final List<String> order = new ArrayList<>(Arrays.asList( "S001_D1_", "S001_D2_", "S001_D3_", "S001_D3_Q1", "S001_D4_", "S001_D5_", "S002_D1_", "S002_D2_", "S002_D3_Q1", "S002_D3_Q2", "S002_D4_", "S002_D5_" )); for (final DeliveryBucket bucket : buckets.keySet()) { assertEquals(bucket.toString(), order.remove(0)); } assertTrue(order.isEmpty()); }