### Question:
MetricsCache { public Record update(MetricsRecord mr, boolean includingTags) { String name = mr.name(); RecordCache recordCache = map.get(name); if (recordCache == null) { recordCache = new RecordCache(); map.put(name, recordCache); } Collection<MetricsTag> tags = mr.tags(); Record record = recordCache.get(tags); if (record == null) { record = new Record(); recordCache.put(tags, record); } for (AbstractMetric m : mr.metrics()) { record.metrics.put(m.name(), m); } if (includingTags) { for (MetricsTag t : mr.tags()) { record.tags.put(t.name(), t.value()); } } return record; } MetricsCache();  MetricsCache(int maxRecsPerName); Record update(MetricsRecord mr, boolean includingTags); Record update(MetricsRecord mr); Record get(String name, Collection<MetricsTag> tags);  }### Answer:
@SuppressWarnings("deprecation") @Test public void testUpdate() { MetricsCache cache = new MetricsCache(); MetricsRecord mr = makeRecord("r", Arrays.asList(makeTag("t", "tv")), Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1))); MetricsCache.Record cr = cache.update(mr); verify(mr).name(); verify(mr).tags(); verify(mr).metrics(); assertEquals("same record size", cr.metrics().size(), ((Collection<AbstractMetric>)mr.metrics()).size()); assertEquals("same metric value", 0, cr.getMetric("m")); MetricsRecord mr2 = makeRecord("r", Arrays.asList(makeTag("t", "tv")), Arrays.asList(makeMetric("m", 2), makeMetric("m2", 42))); cr = cache.update(mr2); assertEquals("contains 3 metric", 3, cr.metrics().size()); checkMetricValue("updated metric value", cr, "m", 2); checkMetricValue("old metric value", cr, "m1", 1); checkMetricValue("new metric value", cr, "m2", 42); MetricsRecord mr3 = makeRecord("r", Arrays.asList(makeTag("t", "tv3")), Arrays.asList(makeMetric("m3", 3))); cr = cache.update(mr3); assertEquals("contains 1 metric", 1, cr.metrics().size()); checkMetricValue("updated metric value", cr, "m3", 3); assertEquals("no tags", 0, cr.tags().size()); cr = cache.update(mr3, true); assertEquals("Got 1 tag", 1, cr.tags().size()); assertEquals("Tag value", "tv3", cr.getTag("t")); checkMetricValue("Metric value", cr, "m3", 3); }
@Test public void testNullTag() { MetricsCache cache = new MetricsCache(); MetricsRecord mr = makeRecord("r", Arrays.asList(makeTag("t", null)), Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1))); MetricsCache.Record cr = cache.update(mr); assertTrue("t value should be null", null == cr.getTag("t")); }