Merge "Remove A1 Policy Management Service"
[nonrtric.git] / information-coordinator-service / src / main / java / org / oransc / ics / repository / MultiMap.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.ics.repository;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Vector;
28
29 /**
30  * A map, where each key can be bound to may values (where each value has an own
31  * ID)
32  */
33 public class MultiMap<T> {
34
35     private final Map<String, Map<String, T>> map = new HashMap<>();
36
37     public void put(String key, String id, T value) {
38         this.map.computeIfAbsent(key, k -> new HashMap<>()).put(id, value);
39     }
40
41     public T remove(String key, String id) {
42         Map<String, T> innerMap = this.map.get(key);
43         if (innerMap != null) {
44             T removedElement = innerMap.remove(id);
45             if (innerMap.isEmpty()) {
46                 this.map.remove(key);
47             }
48             return removedElement;
49         }
50         return null;
51     }
52
53     public Collection<T> get(String key) {
54         Map<String, T> innerMap = this.map.get(key);
55         if (innerMap == null) {
56             return Collections.emptyList();
57         }
58         return new Vector<>(innerMap.values());
59     }
60
61     public void clear() {
62         this.map.clear();
63     }
64
65 }