blob: f3c4b86aa2176bd34bea078c4dc4e29b654c5668 [file] [log] [blame]
tom27ae0e62014-10-01 20:35:01 -07001package org.onlab.onos.net;
2
3import com.google.common.collect.ImmutableSet;
4
5import java.util.HashMap;
6import java.util.Map;
7import java.util.Set;
8
9import static com.google.common.base.Preconditions.checkArgument;
10
11/**
12 * Base abstraction of an annotated entity.
13 */
14public class AbstractAnnotated implements Annotated {
15
16 private static final Map<String, String> EMPTY = new HashMap<>();
17
18 private final Map<String, String> annotations;
19
20 // For serialization
21 protected AbstractAnnotated() {
22 this.annotations = EMPTY;
23 }
24
25 /**
26 * Creates a new entity, annotated with the specified annotations.
27 *
28 * @param annotations optional key/value annotations map
29 */
30 protected AbstractAnnotated(Map<String, String>[] annotations) {
31 checkArgument(annotations.length <= 1, "Only one set of annotations is expected");
32 this.annotations = annotations.length == 1 ? annotations[0] : EMPTY;
33 }
34
35 @Override
36 public Set<String> annotationKeys() {
37 return ImmutableSet.copyOf(annotations.keySet());
38 }
39
40 @Override
41 public String annotation(String key) {
42 return annotations.get(key);
43 }
44
45}