2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019-2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oran.dmaapadapter.repository;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
28 import java.util.Vector;
31 * A map, where each key can be bound to may values (where each value has an own
34 public class MultiMap<T> {
36 private final Map<String, Map<String, T>> map = new HashMap<>();
38 public void put(String key, String id, T value) {
39 this.map.computeIfAbsent(key, k -> new HashMap<>()).put(id, value);
42 public T remove(String key, String id) {
43 Map<String, T> innerMap = this.map.get(key);
44 if (innerMap != null) {
45 T removedElement = innerMap.remove(id);
46 if (innerMap.isEmpty()) {
49 return removedElement;
54 public T get(String key1, String key2) {
55 Map<String, T> innerMap = this.map.get(key1);
56 if (innerMap == null) {
59 return innerMap.get(key2);
62 public Collection<T> get(String key) {
63 Map<String, T> innerMap = this.map.get(key);
64 if (innerMap == null) {
65 return Collections.emptyList();
67 return new Vector<>(innerMap.values());
70 public Set<String> keySet() {
71 return this.map.keySet();