tom | 3ea1125 | 2014-10-02 04:32:26 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableMap; |
| 4 | |
| 5 | import java.util.Map; |
| 6 | import java.util.Objects; |
| 7 | import java.util.Set; |
| 8 | |
| 9 | /** |
| 10 | * Represents a set of simple annotations that can be used to add arbitrary |
| 11 | * attributes to various parts of the data model. |
| 12 | */ |
| 13 | public final class Annotations { |
| 14 | |
| 15 | private final Map<String, String> map; |
| 16 | |
| 17 | /** |
| 18 | * Creates a new set of annotations using the specified immutable map. |
| 19 | * |
| 20 | * @param map immutable map of key/value pairs |
| 21 | */ |
| 22 | private Annotations(ImmutableMap<String, String> map) { |
| 23 | this.map = map; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Creates a new annotations builder. |
| 28 | * |
| 29 | * @return new annotations builder |
| 30 | */ |
| 31 | public static Builder builder() { |
| 32 | return new Builder(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns the set of keys for available annotations. Note that this set |
| 37 | * includes keys for any attributes tagged for removal. |
| 38 | * |
| 39 | * @return annotation keys |
| 40 | */ |
| 41 | public Set<String> keys() { |
| 42 | return map.keySet(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the value of the specified annotation. |
| 47 | * |
| 48 | * @param key annotation key |
| 49 | * @return annotation value |
| 50 | */ |
| 51 | public String value(String key) { |
| 52 | String value = map.get(key); |
| 53 | return Objects.equals(Builder.REMOVED, value) ? null : value; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Indicates whether the specified key has been tagged as removed. This is |
| 58 | * used to for merging sparse annotation sets. |
| 59 | * |
| 60 | * @param key annotation key |
| 61 | * @return true if the previous annotation has been tagged for removal |
| 62 | */ |
| 63 | public boolean isRemoved(String key) { |
| 64 | return Objects.equals(Builder.REMOVED, map.get(key)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Facility for gradually building model annotations. |
| 69 | */ |
| 70 | public static final class Builder { |
| 71 | |
| 72 | private static final String REMOVED = "~rEmOvEd~"; |
| 73 | private final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); |
| 74 | |
| 75 | // Private construction is forbidden. |
| 76 | private Builder() { |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds the specified annotation. Any previous value associated with |
| 81 | * the given annotation key will be overwritten. |
| 82 | * |
| 83 | * @param key annotation key |
| 84 | * @param value annotation value |
| 85 | * @return self |
| 86 | */ |
| 87 | public Builder set(String key, String value) { |
| 88 | builder.put(key, value); |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Adds the specified annotation. Any previous value associated with |
| 94 | * the given annotation key will be tagged for removal. |
| 95 | * |
| 96 | * @param key annotation key |
| 97 | * @return self |
| 98 | */ |
| 99 | public Builder remove(String key) { |
| 100 | builder.put(key, REMOVED); |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns immutable annotations built from the accrued key/values pairs. |
| 106 | * |
| 107 | * @return annotations |
| 108 | */ |
| 109 | public Annotations build() { |
| 110 | return new Annotations(builder.build()); |
| 111 | } |
| 112 | } |
| 113 | } |