re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / utils / TypeMap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.openecomp.sdc.be.dao.utils;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 public class TypeMap {
27         private Map<Class<? extends Object>, Map<String, Object>> cacheMap = new HashMap<>();
28
29         private Map<String, Object> getMap(Class<? extends Object> clazz) {
30                 Map<String, Object> map = cacheMap.get(clazz);
31                 if (map == null) {
32                         cacheMap.put(clazz, new HashMap<>());
33                 }
34                 return cacheMap.get(clazz);
35         }
36
37         /**
38          * put an object (value) in it's type map using the given key.
39          * 
40          * @param key
41          *            The key inside the type map.
42          * @param value
43          *            The object to insert (based on it's type and the given key).
44          */
45         public void put(String key, Object value) {
46                 getMap(value.getClass()).put(key, value);
47         }
48
49         /**
50          * Get the cached object based on it's type and key.
51          * 
52          * @param clazz
53          *            The object's type.
54          * @param key
55          *            The object key.
56          * @return The object that match the given type and key or null if none
57          *         matches.
58          */
59         @SuppressWarnings("unchecked")
60         public <T> T get(Class<T> clazz, String key) {
61                 return (T) (cacheMap.get(clazz) == null ? null : cacheMap.get(clazz).get(key));
62         }
63 }