2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.common.util;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.LinkedList;
14 import java.util.List;
16 import java.util.Objects;
18 import javax.ws.rs.core.MultivaluedMap;
21 * A hash table based implementation of {@link MultivaluedMap} interface.
23 * @author Thomas Pantelis
25 public class MultivaluedHashMap<K, V> implements MultivaluedMap<K, V> {
26 private final Map<K, List<V>> store = new HashMap<>();
29 public final void putSingle(K key, V value) {
30 List<V> values = getValues(key);
39 public void add(K key, V value) {
40 List<V> values = getValues(key);
49 public final void addAll(K key, V... newValues) {
50 addAll(key, Arrays.asList(newValues));
54 public void addAll(K key, List<V> valueList) {
55 Objects.requireNonNull(valueList, "Supplied list of values must not be null.");
57 if (valueList.isEmpty()) {
61 List<V> values = getValues(key);
62 for (V value : valueList) {
70 public void addFirst(K key, V value) {
71 List<V> values = getValues(key);
79 public V getFirst(K key) {
80 List<V> values = store.get(key);
81 return values == null || values.isEmpty() ? null : values.get(0);
85 public boolean equalsIgnoreValueOrder(MultivaluedMap<K, V> omap) {
89 if (!keySet().equals(omap.keySet())) {
92 for (Entry<K, List<V>> e : entrySet()) {
93 List<V> olist = omap.get(e.getKey());
94 if (e.getValue().size() != olist.size()) {
97 for (V v : e.getValue()) {
98 if (!olist.contains(v)) {
107 public Collection<List<V>> values() {
108 return store.values();
117 public List<V> remove(Object key) {
118 return store.remove(key);
122 public void putAll(Map<? extends K, ? extends List<V>> map) {
127 public List<V> put(K key, List<V> value) {
128 return store.put(key, value);
132 public Set<K> keySet() {
133 return store.keySet();
137 public boolean isEmpty() {
138 return store.isEmpty();
142 public List<V> get(Object key) {
143 return store.get(key);
147 public Set<Entry<K, List<V>>> entrySet() {
148 return store.entrySet();
152 public boolean containsValue(Object value) {
153 return store.containsValue(value);
157 public boolean containsKey(Object key) {
158 return store.containsKey(key);
162 public void clear() {
166 private List<V> getValues(K key) {
167 return store.computeIfAbsent(key, k -> new LinkedList<>());