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 if (values != null && values.size() > 0) {
89 public boolean equalsIgnoreValueOrder(MultivaluedMap<K, V> omap) {
93 if (!keySet().equals(omap.keySet())) {
96 for (Entry<K, List<V>> e : entrySet()) {
97 List<V> olist = omap.get(e.getKey());
98 if (e.getValue().size() != olist.size()) {
101 for (V v : e.getValue()) {
102 if (!olist.contains(v)) {
111 public Collection<List<V>> values() {
112 return store.values();
121 public List<V> remove(Object key) {
122 return store.remove(key);
126 public void putAll(Map<? extends K, ? extends List<V>> map) {
131 public List<V> put(K key, List<V> value) {
132 return store.put(key, value);
136 public Set<K> keySet() {
137 return store.keySet();
141 public boolean isEmpty() {
142 return store.isEmpty();
146 public List<V> get(Object key) {
147 return store.get(key);
151 public Set<Entry<K, List<V>>> entrySet() {
152 return store.entrySet();
156 public boolean containsValue(Object value) {
157 return store.containsValue(value);
161 public boolean containsKey(Object key) {
162 return store.containsKey(key);
166 public void clear() {
170 private List<V> getValues(K key) {
171 return store.computeIfAbsent(key, k -> new LinkedList<>());