Add local MultivaluedHashMap implementation 40/68440/3
authorSai MarapaReddy <sai.marapareddy@gmail.com>
Tue, 20 Feb 2018 19:09:39 +0000 (11:09 -0800)
committerSai MarapaReddy <sai.marapareddy@gmail.com>
Sat, 24 Feb 2018 00:37:01 +0000 (00:37 +0000)
Corresponding Carbon patch - https://git.opendaylight.org/gerrit/#/c/63054/

The JSONRestconfService implementation contains a local UriInfo
implementation, SimpleUriInfo, that uses a MultivaluedHashMap
implementation from javax.ws.rs.core. This class is provided by
the dependency used at compile time however, the runtime bundle
that provides the javax.ws.rs.core interfaces doesn't provide
MultivaluedHashMap. Therefore I added our own local MultivaluedHashMap
class based on the external one.

Change-Id: I4f260f27684659fd734581617f6fa2002a6cd78c
Author:     Sai MarapaReddy <sai.marapareddy@gmail.com>
Signed-off-by: Sai MarapaReddy <sai.marapareddy@gmail.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceDraft18.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/MultivaluedHashMap.java [new file with mode: 0644]

index b2940e622bde4912e07e83bd834fe0998205b978..e3a9ae6bbcb1057e4318506ee3dd54e2d8a8f147 100644 (file)
@@ -20,7 +20,6 @@ import java.util.Collections;
 import java.util.List;
 import javax.annotation.Nullable;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedHashMap;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.Response;
diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/MultivaluedHashMap.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/MultivaluedHashMap.java
new file mode 100644 (file)
index 0000000..ea2ec5e
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.sal.restconf.impl;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import javax.ws.rs.core.MultivaluedMap;
+
+/**
+ * A hash table based implementation of {@link MultivaluedMap} interface.
+ *
+ * @author Thomas Pantelis
+ */
+class MultivaluedHashMap<K, V> implements MultivaluedMap<K, V> {
+    private final Map<K, List<V>> store = new HashMap<>();
+
+    @Override
+    public final void putSingle(K key, V value) {
+        List<V> values = getValues(key);
+
+        values.clear();
+        if (value != null) {
+            values.add(value);
+        }
+    }
+
+    @Override
+    public void add(K key, V value) {
+        List<V> values = getValues(key);
+
+        if (value != null) {
+            values.add(value);
+        }
+    }
+
+    @Override
+    public void addAll(K key, V... newValues) {
+        Objects.requireNonNull(newValues, "Supplied array of values must not be null.");
+
+        if (newValues.length == 0) {
+            return;
+        }
+
+        List<V> values = getValues(key);
+        for (V value : newValues) {
+            if (value != null) {
+                values.add(value);
+            }
+        }
+    }
+
+    @Override
+    public void addAll(K key, List<V> valueList) {
+        Objects.requireNonNull(valueList, "Supplied list of values must not be null.");
+
+        if (valueList.isEmpty()) {
+            return;
+        }
+
+        List<V> values = getValues(key);
+        for (V value : valueList) {
+            if (value != null) {
+                values.add(value);
+            }
+        }
+    }
+
+    @Override
+    public void addFirst(K key, V value) {
+        List<V> values = getValues(key);
+
+        if (value != null) {
+            values.add(0, value);
+        }
+    }
+
+    @Override
+    public V getFirst(K key) {
+        List<V> values = store.get(key);
+        if (values != null && values.size() > 0) {
+            return values.get(0);
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public boolean equalsIgnoreValueOrder(MultivaluedMap<K, V> omap) {
+        if (this == omap) {
+            return true;
+        }
+        if (!keySet().equals(omap.keySet())) {
+            return false;
+        }
+        for (Entry<K, List<V>> e : entrySet()) {
+            List<V> olist = omap.get(e.getKey());
+            if (e.getValue().size() != olist.size()) {
+                return false;
+            }
+            for (V v : e.getValue()) {
+                if (!olist.contains(v)) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public Collection<List<V>> values() {
+        return store.values();
+    }
+
+    @Override
+    public int size() {
+        return store.size();
+    }
+
+    @Override
+    public List<V> remove(Object key) {
+        return store.remove(key);
+    }
+
+    @Override
+    public void putAll(Map<? extends K, ? extends List<V>> map) {
+        store.putAll(map);
+    }
+
+    @Override
+    public List<V> put(K key, List<V> value) {
+        return store.put(key, value);
+    }
+
+    @Override
+    public Set<K> keySet() {
+        return store.keySet();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return store.isEmpty();
+    }
+
+    @Override
+    public List<V> get(Object key) {
+        return store.get(key);
+    }
+
+    @Override
+    public Set<Entry<K, List<V>>> entrySet() {
+        return store.entrySet();
+    }
+
+    @Override
+    public boolean containsValue(Object value) {
+        return store.containsValue(value);
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        return store.containsKey(key);
+    }
+
+    @Override
+    public void clear() {
+        store.clear();
+    }
+
+    private List<V> getValues(K key) {
+        List<V> list = store.get(key);
+        if (list == null) {
+            list = new LinkedList<>();
+            store.put(key, list);
+        }
+
+        return list;
+    }
+}
+