558fc465fef729d86f16bf73b3e4897d53f37f74
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / repository / InfoTypes.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.oran.dmaapadapter.repository;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.oran.dmaapadapter.exceptions.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class InfoTypes {
33     private static final Logger logger = LoggerFactory.getLogger(InfoTypes.class);
34
35     private Map<String, InfoType> allTypes = new HashMap<>();
36
37     public InfoTypes(Collection<InfoType> types) {
38         for (InfoType type : types) {
39             put(type);
40         }
41     }
42
43     public synchronized InfoType get(String id) {
44         return allTypes.get(id);
45     }
46
47     public synchronized InfoType getType(String id) throws ServiceException {
48         InfoType type = allTypes.get(id);
49         if (type == null) {
50             throw new ServiceException("Could not find type: " + id);
51         }
52         return type;
53     }
54
55     public static class ConfigFile {
56         Collection<InfoType> types;
57     }
58
59     private synchronized void put(InfoType type) {
60         logger.debug("Put type: {}", type.getId());
61         allTypes.put(type.getId(), type);
62     }
63
64     public synchronized Iterable<InfoType> getAll() {
65         return new Vector<>(allTypes.values());
66     }
67
68     public synchronized Collection<String> typeIds() {
69         return allTypes.keySet();
70     }
71
72     public synchronized int size() {
73         return allTypes.size();
74     }
75
76     public synchronized void clear() {
77         allTypes.clear();
78     }
79 }