OrderedObjectNameMapper implements ObjectNameMapper { @Override public ObjectName createObjectName(String domain, Monitor<?> monitor) { ObjectNameBuilder objBuilder = ObjectNameBuilder.forDomain(domain); Map<String, String> tags = new TreeMap<>( monitor.getConfig().getTags().asMap()); for (String knownKey : keyOrder) { if ("name".equals(knownKey)) { addName(objBuilder, monitor); } else { String value = tags.remove(knownKey); if (value != null) { objBuilder.addProperty(knownKey, value); } } } if (appendRemaining) { if (!orderIncludesName) { addName(objBuilder, monitor); } for (Map.Entry<String, String> additionalTag : tags.entrySet()) { objBuilder.addProperty(additionalTag.getKey(), additionalTag.getValue()); } } return objBuilder.build(); } OrderedObjectNameMapper(boolean appendRemaining, String... orderedKeys);  OrderedObjectNameMapper(boolean appendRemaining, List<String> orderedKeys); @Override ObjectName createObjectName(String domain, Monitor<?> monitor);  }
@Test public void testOrderedTagsWithAppend() { ObjectNameMapper mapper = new OrderedObjectNameMapper(true, "name", DataSourceType.KEY, "foo", "notPresentKey"); ObjectName name = mapper.createObjectName(TEST_DOMAIN, TEST_COUNTER); assertEquals(name.getDomain(), TEST_DOMAIN); assertEquals(name.getKeyPropertyListString(), String.format("name=testName,%s=COUNTER,foo=bar,aaa=aaaVal,zzz=zzzVal", DataSourceType.KEY)); }
@Test public void testOrderedTagsWithoutAppend() { ObjectNameMapper mapper = new OrderedObjectNameMapper(false, Arrays.asList("name", DataSourceType.KEY, "foo", "notPresentKey")); ObjectName name = mapper.createObjectName(TEST_DOMAIN, TEST_COUNTER); assertEquals(name.getDomain(), TEST_DOMAIN); assertEquals(name.getKeyPropertyListString(), String.format("name=testName,%s=COUNTER,foo=bar", DataSourceType.KEY)); }
@Test public void testOrderedTagsWithoutNameExplicitlyOrdered() { ObjectNameMapper mapper = new OrderedObjectNameMapper(true, "foo", DataSourceType.KEY); ObjectName name = mapper.createObjectName(TEST_DOMAIN, TEST_COUNTER); assertEquals(name.getDomain(), TEST_DOMAIN); assertEquals(name.getKeyPropertyListString(), String.format("foo=bar,%s=COUNTER,name=testName,aaa=aaaVal,zzz=zzzVal", DataSourceType.KEY)); }