|
| 1 | +package org.jfrog.build.api.multiMap; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 4 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 5 | + |
| 6 | +import java.io.Serializable; |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") |
| 10 | +@JsonSubTypes({ |
| 11 | + @JsonSubTypes.Type(value = ListMultimap.class, name = "list"), |
| 12 | + @JsonSubTypes.Type(value = SetMultimap.class, name = "set"), |
| 13 | +}) |
| 14 | +public abstract class Multimap<Key, Value> implements Serializable { |
| 15 | + private static final long serialVersionUID = 1L; |
| 16 | + Map<Key, Collection<Value>> multiMap = new HashMap<>(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Default constructor. |
| 20 | + */ |
| 21 | + Multimap() { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructor that accepts a map. |
| 26 | + * |
| 27 | + * @param map the map |
| 28 | + */ |
| 29 | + Multimap(Map<Key, Value> map) { |
| 30 | + map.forEach(this::put); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Put a key-value pair into the multimap. |
| 35 | + * |
| 36 | + * @param key the key |
| 37 | + * @param value the value |
| 38 | + */ |
| 39 | + public abstract void put(Key key, Value value); |
| 40 | + |
| 41 | + /** |
| 42 | + * Get all values for a key. |
| 43 | + * |
| 44 | + * @param key the key |
| 45 | + * @return a collection of values for the key |
| 46 | + */ |
| 47 | + public Collection<Value> get(Key key) { |
| 48 | + return multiMap.get(key); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Put all key-value pairs from a map into the multimap. |
| 53 | + * |
| 54 | + * @param map the map |
| 55 | + */ |
| 56 | + public void putAll(Map<Key, Value> map) { |
| 57 | + for (Map.Entry<Key, Value> entry : map.entrySet()) { |
| 58 | + put(entry.getKey(), entry.getValue()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Put all key-value pairs from a multimap into the multimap. |
| 64 | + * |
| 65 | + * @param multimap the multimap |
| 66 | + */ |
| 67 | + public void putAll(Multimap<Key, Value> multimap) { |
| 68 | + for (Map.Entry<Key, Collection<Value>> entry : multimap.multiMap.entrySet()) { |
| 69 | + for (Value value : entry.getValue()) { |
| 70 | + put(entry.getKey(), value); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Put all values for a key into the multimap. |
| 77 | + * |
| 78 | + * @param key the key |
| 79 | + * @param values the values |
| 80 | + */ |
| 81 | + public void putAll(Key key, Collection<Value> values) { |
| 82 | + for (Value value : values) { |
| 83 | + put(key, value); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get all key-value pairs in the multimap. |
| 89 | + * |
| 90 | + * @return a set of key-value pairs |
| 91 | + */ |
| 92 | + public Set<Map.Entry<Key, Value>> entries() { |
| 93 | + Set<Map.Entry<Key, Value>> entries = new HashSet<>(); |
| 94 | + for (Map.Entry<Key, Collection<Value>> entry : multiMap.entrySet()) { |
| 95 | + for (Value value : entry.getValue()) { |
| 96 | + entries.add(new AbstractMap.SimpleEntry<>(entry.getKey(), value)); |
| 97 | + } |
| 98 | + } |
| 99 | + return entries; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get the underlying map. |
| 104 | + * |
| 105 | + * @return the map |
| 106 | + */ |
| 107 | + public Map<Key, Collection<Value>> asMap() { |
| 108 | + return multiMap; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get all keys in the multimap. |
| 113 | + * |
| 114 | + * @return a set of keys |
| 115 | + */ |
| 116 | + public Set<Key> keySet() { |
| 117 | + return multiMap.keySet(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Check if the multimap contains a value. |
| 122 | + * |
| 123 | + * @param value the value |
| 124 | + * @return true if the multimap contains the value |
| 125 | + */ |
| 126 | + public boolean containsValue(Value value) { |
| 127 | + for (Collection<Value> values : multiMap.values()) { |
| 128 | + if (values.contains(value)) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the number of key-value pairs in the multimap. |
| 137 | + * |
| 138 | + * @return the number of key-value pairs |
| 139 | + */ |
| 140 | + public int size() { |
| 141 | + int size = 0; |
| 142 | + for (Collection<Value> values : multiMap.values()) { |
| 143 | + size += values.size(); |
| 144 | + } |
| 145 | + return size; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Check if the multimap is empty. |
| 150 | + * |
| 151 | + * @return true if the multimap is empty |
| 152 | + */ |
| 153 | + public boolean isEmpty() { |
| 154 | + return multiMap.isEmpty(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Clear the multimap. |
| 159 | + */ |
| 160 | + public void clear() { |
| 161 | + multiMap.clear(); |
| 162 | + } |
| 163 | +} |
0 commit comments