MetricParser implements Parser<Metric> { @Override public Metric parse(String line) { List<String> parts = spaceSplitter.splitToList(line); Preconditions.checkPositionIndex(3, parts.size()); Metric.Builder builder = Metric.newBuilder(); builder.name(parts.get(1)); builder.value(Long.parseLong(parts.get(2)), Double.parseDouble(parts.get(3))); parts.stream().skip(4).forEach(builder::tag); return builder.build(); }  @Override Metric parse(String line);  }
@Test public void testParseWithEscapedCharacters() { MetricParser parser = new MetricParser(); Metric m = parser.parse("put mymetric 12341234 5.0 tag1=value1,value1 tag2=value2=value2"); Assert.assertEquals("mymetric", m.getName()); Assert.assertEquals(12341234, (long) m.getValue().getTimestamp()); Assert.assertEquals(5.0, (double) m.getValue().getMeasure(), 0); List<Tag> expected = new ArrayList<>(); expected.add(new Tag("tag1", "value1,value1")); expected.add(new Tag("tag2", "value2=value2")); Assert.assertEquals(expected, m.getTags()); }
@Test public void testParseMalformatted() { MetricParser parser = new MetricParser(); try { parser.parse("put mymetric 12341234 5.0 tag1 tag2=value2"); Assert.fail(); } catch (IllegalArgumentException e) { } }