ServiceInstance extends DiscoveryRecord { @Override public int compareTo ( DiscoveryRecord other ) { if ( other instanceof ServiceInstance ) { ServiceInstance real = (ServiceInstance) other; if ( this.serviceRecord != null && real.serviceRecord != null ) { return this.serviceRecord.compareTo( real.serviceRecord ); } } else { return super.compareTo( other ); } return 0; } ServiceInstance( String serviceType, ServiceRecord serviceRecord, TextRecord textRecord ); ServiceRecord getServiceRecord(); TextRecord getTextRecord(); @Override int hashCode(); @Override boolean equals( Object obj ); @Override int compareTo( DiscoveryRecord other ); @Override String toString(); @Override String getServiceType(); @Override String getServiceZone( String dnsLabel ); @Override String getServiceName( String dnsLabel ); @Override String toDisplay();  }
@Test public void testCompareToDifferentPriority () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 1, 10, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeCoap, svc2, txt2 ); Assert.assertTrue( "Incorrect priority comparison", svcInst1.compareTo( svcInst2 ) < 1 ); Assert.assertTrue( "Incorrect priority comparison", svcInst2.compareTo( svcInst1 ) > 0 ); }
@Test public void testCompareToSamePriorityDifferentWeight () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 20, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeCoap, svc2, txt2 ); Assert.assertTrue( "Incorrect priority comparison", svcInst1.compareTo( svcInst2 ) > 0 ); Assert.assertTrue( "Incorrect priority comparison", svcInst2.compareTo( svcInst1 ) < 0 ); }