PropertyServiceReferenceComparator implements Comparator<ServiceReference<T>>, Serializable { @Override public int compare( ServiceReference<T> serviceReference1, ServiceReference<T> serviceReference2) { if (serviceReference1 == null) { if (serviceReference2 == null) { return 0; } return 1; } else if (serviceReference2 == null) { return -1; } Object propertyValue1 = serviceReference1.getProperty(_propertyKey); Object propertyValue2 = serviceReference2.getProperty(_propertyKey); if (propertyValue1 == null) { if (propertyValue2 == null) { return 0; } return 1; } else if (propertyValue2 == null) { return -1; } if (!(propertyValue2 instanceof Comparable)) { return serviceReference2.compareTo(serviceReference1); } Comparable<Object> propertyValueComparable2 = (Comparable<Object>)propertyValue2; return propertyValueComparable2.compareTo(propertyValue1); } PropertyServiceReferenceComparator(String propertyKey); @Override int compare(
		ServiceReference<T> serviceReference1,
		ServiceReference<T> serviceReference2);  }
@Test public void testCompare() { PropertyServiceReferenceComparator<Object> propertyServiceReferenceComparator = new PropertyServiceReferenceComparator<>("ranking"); ServiceReference<Object> serviceReference1 = new TestServiceReference<>( "ranking", 1); ServiceReference<Object> serviceReference2 = new TestServiceReference<>( "ranking", 2); int compare = propertyServiceReferenceComparator.compare( serviceReference2, serviceReference1); Assert.assertTrue(compare < 0); Assert.assertEquals( propertyServiceReferenceComparator.compare( serviceReference1, serviceReference2), -propertyServiceReferenceComparator.compare( serviceReference2, serviceReference1)); ServiceReference<Object> serviceReference3 = new TestServiceReference<>( "ranking", 1); Assert.assertEquals( 0, propertyServiceReferenceComparator.compare( serviceReference1, serviceReference3)); }
@Test public void testCompareIsTransitiveWhenServiceReferencePropertiesAreNull() { PropertyServiceReferenceComparator<Object> propertyServiceReferenceComparator = new PropertyServiceReferenceComparator<>("ranking"); ServiceReference<Object> serviceReference1 = new TestServiceReference<>( "ranking", -1); ServiceReference<Object> serviceReference2 = new TestServiceReference<>( "ranking", 0); ServiceReference<Object> serviceReference3 = new TestServiceReference<>( "ranking", 1); ServiceReference<Object> serviceReference4 = new TestServiceReference<>(); int compare1 = propertyServiceReferenceComparator.compare( serviceReference4, serviceReference1); Assert.assertTrue(compare1 > 0); int compare2 = propertyServiceReferenceComparator.compare( serviceReference1, serviceReference2); Assert.assertTrue(compare2 > 0); int compare3 = propertyServiceReferenceComparator.compare( serviceReference4, serviceReference2); Assert.assertTrue(compare3 > 0); int compare4 = propertyServiceReferenceComparator.compare( serviceReference4, serviceReference2); Assert.assertTrue(compare4 > 0); int compare5 = propertyServiceReferenceComparator.compare( serviceReference2, serviceReference3); Assert.assertTrue(compare5 > 0); int compare6 = propertyServiceReferenceComparator.compare( serviceReference4, serviceReference3); Assert.assertTrue(compare6 > 0); }
@Test public void testCompareIsTransitiveWhenServiceReferencesAreNull() { PropertyServiceReferenceComparator<Object> propertyServiceReferenceComparator = new PropertyServiceReferenceComparator<>("ranking"); ServiceReference<Object> serviceReference1 = new TestServiceReference<>( "ranking", -1); ServiceReference<Object> serviceReference2 = new TestServiceReference<>( "ranking", 0); ServiceReference<Object> serviceReference3 = new TestServiceReference<>( "ranking", 1); int compare1 = propertyServiceReferenceComparator.compare( null, serviceReference1); Assert.assertTrue(compare1 > 0); int compare2 = propertyServiceReferenceComparator.compare( serviceReference1, serviceReference2); Assert.assertTrue(compare2 > 0); int compare3 = propertyServiceReferenceComparator.compare( null, serviceReference2); Assert.assertTrue(compare3 > 0); int compare4 = propertyServiceReferenceComparator.compare( null, serviceReference2); Assert.assertTrue(compare4 > 0); int compare5 = propertyServiceReferenceComparator.compare( serviceReference2, serviceReference3); Assert.assertTrue(compare5 > 0); int compare6 = propertyServiceReferenceComparator.compare( null, serviceReference3); Assert.assertTrue(compare6 > 0); }
@Test public void testCompareWhenServiceReferencePropertiesAreNull() { PropertyServiceReferenceComparator<Object> propertyServiceReferenceComparator = new PropertyServiceReferenceComparator<>("ranking"); ServiceReference<Object> serviceReference1 = new TestServiceReference<>( "ranking", 1); ServiceReference<Object> serviceReference2 = new TestServiceReference<>(); int compare = propertyServiceReferenceComparator.compare( serviceReference1, serviceReference2); Assert.assertTrue(compare < 0); Assert.assertEquals( propertyServiceReferenceComparator.compare( serviceReference1, serviceReference2), -propertyServiceReferenceComparator.compare( serviceReference2, serviceReference1)); ServiceReference<Object> serviceReference3 = new TestServiceReference<>(); Assert.assertEquals( 0, propertyServiceReferenceComparator.compare( serviceReference2, serviceReference3)); Assert.assertEquals( propertyServiceReferenceComparator.compare( serviceReference2, serviceReference3), -propertyServiceReferenceComparator.compare( serviceReference3, serviceReference2)); }
@Test public void testCompareWhenServiceReferencesAreNull() { PropertyServiceReferenceComparator<Object> propertyServiceReferenceComparator = new PropertyServiceReferenceComparator<>("ranking"); ServiceReference<Object> serviceReference1 = new TestServiceReference<>( "ranking", 1); Assert.assertEquals( 0, propertyServiceReferenceComparator.compare(null, null)); int compare = propertyServiceReferenceComparator.compare( serviceReference1, null); Assert.assertTrue(compare < 0); Assert.assertEquals( propertyServiceReferenceComparator.compare(serviceReference1, null), -propertyServiceReferenceComparator.compare( null, serviceReference1)); }