### Question:
Query { public Query(final String metricName, final List<TagMatcher> tagMatchers) { if (metricName == null || metricName.isEmpty() || tagMatchers == null) { throw new IllegalArgumentException("metric name or tag matcher can't be null."); } final Map<String, List<TagMatcher>> tagNameMap = tagMatchers.stream() .map(t -> new SimpleEntry<>(t.tag.key, t)) .collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList()))); tagNameMap.entrySet().forEach(tagKeyEntry -> { if (tagKeyEntry.getValue().size() != 1) { throw new IllegalArgumentException("Only one tagFilter is allowed per tagKey: " + tagKeyEntry.getKey() + " .But we found " + tagKeyEntry.getValue().toString()); } }); this.metricName = metricName; this.tagMatchers = tagMatchers; } Query(final String metricName, final List<TagMatcher> tagMatchers); static Query parse(String s); @Override String toString(); final String metricName; final List<TagMatcher> tagMatchers; }### Answer:
@Test public void testQuery() { Query q = new Query(metric, Arrays.asList(tagMatcher1)); assertEquals(metric, q.metricName); assertEquals(1, q.tagMatchers.size()); assertEquals(tagMatcher1, q.tagMatchers.get(0)); Query q1 = Query.parse(metric); assertEquals(metric, q1.metricName); assertTrue(q1.tagMatchers.isEmpty()); Query q2 = Query.parse("metric k1=v1"); assertEquals(metric, q2.metricName); assertEquals(1, q2.tagMatchers.size()); assertEquals(new TagMatcher(MatchType.EXACT, new Tag("k1", "v1", "k1=v1")), q2.tagMatchers.get(0)); Query q3 = Query.parse("metric k1=v1 k2=v2"); assertEquals(metric, q3.metricName); assertEquals(2, q3.tagMatchers.size()); assertEquals(new TagMatcher(MatchType.EXACT, new Tag("k1", "v1", "k1=v1")), q3.tagMatchers.get(0)); assertEquals(new TagMatcher(MatchType.EXACT, new Tag("k2", "v2", "k2=v2")), q3.tagMatchers.get(1)); Query q4 = Query.parse("metric k1=*"); assertEquals(metric, q4.metricName); assertEquals(1, q4.tagMatchers.size()); assertEquals(new TagMatcher(MatchType.WILDCARD, new Tag("k1", "*")), q4.tagMatchers.get(0)); Query q5 = Query.parse("metric k1=* k2=v1"); assertEquals(metric, q5.metricName); assertEquals(2, q5.tagMatchers.size()); assertEquals(new TagMatcher(MatchType.WILDCARD, new Tag("k1", "*")), q5.tagMatchers.get(0)); assertEquals(new TagMatcher(MatchType.EXACT, new Tag("k2", "v1", "k2=v1")), q5.tagMatchers.get(1)); }