tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableSet; |
| 4 | |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Map; |
| 7 | import java.util.Set; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkArgument; |
| 10 | |
| 11 | /** |
| 12 | * Base abstraction of an annotated entity. |
| 13 | */ |
| 14 | public 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 | } |