@Test public void merge() { annotations = builder().set("foo", "1").set("bar", "2").build(); assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); SparseAnnotations updates = builder().remove("foo").set("bar", "3").set("goo", "4").build(); annotations = DefaultAnnotations.merge(annotations, updates); assertEquals("incorrect keys", of("goo", "bar"), annotations.keys()); assertNull("incorrect value", annotations.value("foo")); assertEquals("incorrect value", "3", annotations.value("bar")); }
public static DefaultAnnotations merge(DefaultAnnotations annotations, SparseAnnotations sparseAnnotations) { checkNotNull(annotations, "Annotations cannot be null"); if (sparseAnnotations == null || sparseAnnotations.keys().isEmpty()) { return annotations; } Map<String, String> merged = copy(annotations.map); for (String key : sparseAnnotations.keys()) { if (sparseAnnotations.isRemoved(key)) { merged.remove(key); } else { merged.put(key, sparseAnnotations.value(key)); } } return new DefaultAnnotations(merged); }
DefaultAnnotations implements SparseAnnotations { public static DefaultAnnotations merge(DefaultAnnotations annotations, SparseAnnotations sparseAnnotations) { checkNotNull(annotations, "Annotations cannot be null"); if (sparseAnnotations == null || sparseAnnotations.keys().isEmpty()) { return annotations; } Map<String, String> merged = copy(annotations.map); for (String key : sparseAnnotations.keys()) { if (sparseAnnotations.isRemoved(key)) { merged.remove(key); } else { merged.put(key, sparseAnnotations.value(key)); } } return new DefaultAnnotations(merged); } }
DefaultAnnotations implements SparseAnnotations { public static DefaultAnnotations merge(DefaultAnnotations annotations, SparseAnnotations sparseAnnotations) { checkNotNull(annotations, "Annotations cannot be null"); if (sparseAnnotations == null || sparseAnnotations.keys().isEmpty()) { return annotations; } Map<String, String> merged = copy(annotations.map); for (String key : sparseAnnotations.keys()) { if (sparseAnnotations.isRemoved(key)) { merged.remove(key); } else { merged.put(key, sparseAnnotations.value(key)); } } return new DefaultAnnotations(merged); } private  DefaultAnnotations(); private  DefaultAnnotations(Map<String, String> map); }
DefaultAnnotations implements SparseAnnotations { public static DefaultAnnotations merge(DefaultAnnotations annotations, SparseAnnotations sparseAnnotations) { checkNotNull(annotations, "Annotations cannot be null"); if (sparseAnnotations == null || sparseAnnotations.keys().isEmpty()) { return annotations; } Map<String, String> merged = copy(annotations.map); for (String key : sparseAnnotations.keys()) { if (sparseAnnotations.isRemoved(key)) { merged.remove(key); } else { merged.put(key, sparseAnnotations.value(key)); } } return new DefaultAnnotations(merged); } private  DefaultAnnotations(); private  DefaultAnnotations(Map<String, String> map); @Override boolean equals(Object o); @Override int hashCode(); HashMap<String, String> asMap(); static Builder builder(); static DefaultAnnotations merge(DefaultAnnotations annotations,
                                           SparseAnnotations sparseAnnotations); static SparseAnnotations union(SparseAnnotations annotations,
                                          SparseAnnotations sparseAnnotations); @Override Set<String> keys(); @Override String value(String key); @Override boolean isRemoved(String key); @Override String toString(); }
DefaultAnnotations implements SparseAnnotations { public static DefaultAnnotations merge(DefaultAnnotations annotations, SparseAnnotations sparseAnnotations) { checkNotNull(annotations, "Annotations cannot be null"); if (sparseAnnotations == null || sparseAnnotations.keys().isEmpty()) { return annotations; } Map<String, String> merged = copy(annotations.map); for (String key : sparseAnnotations.keys()) { if (sparseAnnotations.isRemoved(key)) { merged.remove(key); } else { merged.put(key, sparseAnnotations.value(key)); } } return new DefaultAnnotations(merged); } private  DefaultAnnotations(); private  DefaultAnnotations(Map<String, String> map); @Override boolean equals(Object o); @Override int hashCode(); HashMap<String, String> asMap(); static Builder builder(); static DefaultAnnotations merge(DefaultAnnotations annotations,
                                           SparseAnnotations sparseAnnotations); static SparseAnnotations union(SparseAnnotations annotations,
                                          SparseAnnotations sparseAnnotations); @Override Set<String> keys(); @Override String value(String key); @Override boolean isRemoved(String key); @Override String toString(); static final SparseAnnotations EMPTY; }