@Test public void testCopyOfIterableString() throws Exception { Map<String, String> map = ImmutableMap.of("foo", "bar", "dee", "dum"); List<String> input = ImmutableList.of("foo=bar", "dee=dum"); TagList tags = BasicTagList.copyOf(input); assertEquals(tags.asMap(), map); }
@Test public void testConcatVararg() throws Exception { Map<String, String> map = ImmutableMap.of("foo", "bar", "dee", "dum"); TagList t1 = BasicTagList.of("foo", "bar"); TagList tags = BasicTagList.concat(t1, new BasicTag("dee", "dum")); assertEquals(tags.asMap(), map); }
@Test public void testConcatOverride() throws Exception { Map<String, String> map = ImmutableMap.of("foo", "bar2"); TagList t1 = BasicTagList.of("foo", "bar"); TagList t2 = BasicTagList.of("foo", "bar2"); TagList tags = BasicTagList.concat(t1, t2); assertEquals(tags.asMap(), map); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || !(obj instanceof DefaultId)) return false; DefaultId other = (DefaultId) obj; return name.equals(other.name) && ((tags == null && other.tags == null) || (tags != null && tags.equals(other.tags))); }
@Test public void testAccessors() throws Exception { TagList t1 = BasicTagList.of("foo", "bar"); assertTrue(!t1.isEmpty()); assertEquals(t1.size(), 1); assertEquals(t1.getTag("foo"), new BasicTag("foo", "bar")); assertTrue(t1.getTag("dee") == null, "dee is not a tag"); assertTrue(t1.containsKey("foo")); assertTrue(!t1.containsKey("dee")); }
/** * Create a new id by possibly removing tags from the list. This operation will:<br> * 1) remove keys as specified by the parameters<br> * 2) dedup entries that have the same key, the first value associated with the key will be the * one kept,<br> * 3) sort the list by the tag keys. * * @param keys Set of keys to either keep or remove. * @param keep If true, then the new id can only have tag keys in the provided set. Otherwise the * new id can only have ids not in that set. * @return New identifier after applying the rollup. */ DefaultId rollup(Set<String> keys, boolean keep) { if (tags == TagList.EMPTY) { return this; } else { Map<String, String> ts = new TreeMap<>(); for (Tag t : tags) { if (keys.contains(t.key()) == keep && !ts.containsKey(t.key())) { ts.put(t.key(), t.value()); } } return new DefaultId(name, TagList.create(ts)); } }
@Test public void testEmpty() throws Exception { TagList t1 = BasicTagList.EMPTY; assertTrue(t1.isEmpty()); assertEquals(t1.size(), 0); }
@Override public DefaultId withTags(Map<String, String> ts) { return withTags(TagList.create(ts)); }
@Override public DefaultId withTags(Iterable<Tag> ts) { TagList tmp = (tags == TagList.EMPTY) ? TagList.create(ts) : tags.prepend(ts); return new DefaultId(name, tmp); }
@Override public int hashCode() { return name.hashCode() + (tags == null ? 0 : tags.hashCode()); }