Metric { public Metric addDimensionsFrom(Metric other) { Map<String, String> combined = new LinkedHashMap<>(dimensions); other.dimensions.forEach( (k, v) -> { if (!combined.containsKey(k)) combined.put(k, v); }); return new Metric(name, outputName, description, combined); } Metric(String name, String outputName, String description, Map<String, String> dimensions);  Metric(String name, String outputName, String description);  Metric(String name, String outputName);  Metric(String name); Metric addDimensionsFrom(Metric other); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); final String name; final String outputName; final String description; final Map<String, String> dimensions; }
@Test public void this_metric_takes_precedence_when_combined_with_another_metric() { String COMMON_DIMENSION_KEY = "commonKey"; Map<String, String> thisDimensions = ImmutableMap.<String, String>builder() .put(COMMON_DIMENSION_KEY, "thisCommonVal") .put("thisKey", "thisVal") .build(); Metric thisMetric = new Metric("thisMetric", "this-output-name", "this-description", thisDimensions); Map<String, String> thatDimensions = ImmutableMap.<String, String>builder() .put(COMMON_DIMENSION_KEY, "thatCommonVal") .put("thatKey", "thatVal") .build(); Metric thatMetric = new Metric("thatMetric", "that-output-name", "that-description", thatDimensions); Metric combinedMetric = thisMetric.addDimensionsFrom(thatMetric); assertEquals("this-output-name", combinedMetric.outputName); assertEquals("this-description", combinedMetric.description); assertEquals(3, combinedMetric.dimensions.size()); assertEquals("thisCommonVal", combinedMetric.dimensions.get(COMMON_DIMENSION_KEY)); }