2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.utils;
40 import java.lang.reflect.Field;
41 import java.lang.reflect.Method;
42 import java.util.HashMap;
43 import java.util.List;
45 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 * @author Vladimir Turovets This class is used to create HashMap<String, T>
50 * from the list of T objects. We suppose that
51 * 1) T object contains field 'parmName' or getter 'getParmName()'.
52 * The value of object.parmName or object.getParmName() is used as
53 * a key in created hashMap.
54 * 2) for all objects in the list 'parmName' or getter
55 * 'getParmName().toString' has to be unique and not null.
56 * This class has one function only:
57 * HashMap<String, T> hashMap(List<T> list, String name) and returns
58 * hash map created from list.
63 public class HashMapFromList<T> {
64 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HashMapFromList.class);
66 public HashMap<String, T> hashMap(List<T> list, String name) {
67 HashMap<String, T> result = new HashMap<String, T>();
68 if (list == null || list.size() == 0 || name == null) {
72 if (name.length() > 0) {
73 T object = list.get(0);
75 String parmName = name;
76 Field field = object.getClass().getField(parmName);
79 Object o = field.get(obj);
81 result.put(o.toString(), obj);
82 } catch (Exception e1) {
83 logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 1: object of class " + object.getClass().getName() + ", field " + parmName, e1);
84 return new HashMap<String, T>();
87 } catch (Exception e) {
88 String getterName = "get" + (name.length() == 1 ? name.toUpperCase() : (name.substring(0, 1).toUpperCase() + name.substring(1)));
90 Class<?>[] parmClasses = null;
91 Method method = object.getClass().getMethod(getterName, parmClasses);
92 Object[] parmValues = new Object[0];
95 Object o = method.invoke(obj, parmValues);
97 result.put(o.toString(), obj);
98 } catch (Exception e2) {
99 logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 2: object of class " + object.getClass().getName() + ", method " + getterName, e2);
100 return new HashMap<String, T>();
103 } catch (Exception e3) {
104 logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 3: object of class " + object.getClass().getName() + ", bad field '" + name + "' or method '" + getterName + "()", e3);
105 return new HashMap<String, T>();
109 if (list.size() != result.size()) {
110 logger.warn(EELFLoggerDelegate.errorLogger, "Duplicated or empty keys were found!!!");