CompoundLabel { public String prefixString() { if(type.isEmpty()) throw new IllegalStateException("Type is empty: unable to build a prefix string"); if(proto.isEmpty()) throw new IllegalStateException("Protocol is empty: unable to build a prefix string"); StringBuilder prefix = new StringBuilder(); prefix.append("_"); if(subType != null && !subType.isEmpty()) { prefix.append(subType); prefix.append(Constants.DNS_LABEL_DELIMITER); prefix.append(Constants.SUBTYPE); prefix.append(Constants.DNS_LABEL_DELIMITER); prefix.append("_"); } prefix.append(type); prefix.append(Constants.DNS_LABEL_DELIMITER); prefix.append("_"); prefix.append((proto.contains("_")?proto.subSequence(1, proto.length()):proto)); prefix.append(Constants.DNS_LABEL_DELIMITER); return prefix.toString().toLowerCase(); } CompoundLabel(String type);  CompoundLabel(String type, String subType);  CompoundLabel(String type, String subType, String proto); final static boolean isCompound(String label); final static boolean isValidCompound(String label); final static String[] labelComponents(String label); CompoundLabel buildByLabel(String label); String getType(); String getSubType(); String getProto(); boolean hasSubType(); boolean hasProtocol(); String prefixString(); String prefixString(String protocol); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString();  }
@Test public void testPrefixString() { type = new CompoundLabel("coap"); try { Assert.assertEquals("_coap._tcp.", type.prefixString()); } catch(Exception e) { } type = new CompoundLabel("coap", "subcoap", "udp"); Assert.assertEquals("_subcoap._sub._coap._udp.", type.prefixString()); type = new CompoundLabel("coap", "subcoap"); try { Assert.assertEquals("_subcoap._sub._coap._tcp.", type.prefixString()); } catch(Exception e) { } }
@Test public void testPrefixStringSpecifyingProto() { type = new CompoundLabel("coap"); Assert.assertEquals("_coap._tcp.", type.prefixString("tcp")); type = new CompoundLabel("coap", "subcoap"); Assert.assertEquals("_subcoap._sub._coap._udp.", type.prefixString("udp")); type = new CompoundLabel("coap", "subcoap"); Assert.assertEquals("_subcoap._sub._coap._tcp.", type.prefixString("tcp")); }