2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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
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 * ================================================================================
20 package org.openecomp.portalapp.portal.utils;
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 * @author Vladimir Turovets This class is used to create HashMap<String, T>
32 * from the list of T objects. We suppose that
33 * 1) T object contains field 'parmName' or getter 'getParmName()'.
34 * The value of object.parmName or object.getParmName() is used as
35 * a key in created hashMap.
36 * 2) for all objects in the list 'parmName' or getter
37 * 'getParmName().toString' has to be unique and not null.
38 * This class has one function only:
39 * HashMap<String, T> hashMap(List<T> list, String name) and returns
40 * hash map created from list.
44 public class HashMapFromList<T> {
45 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HashMapFromList.class);
47 public HashMap<String, T> hashMap(List<T> list, String name) {
48 HashMap<String, T> result = new HashMap<String, T>();
49 if (list == null || list.size() == 0 || name == null) {
53 if (name.length() > 0) {
54 T object = list.get(0);
56 String parmName = name;
57 Field field = object.getClass().getField(parmName);
60 Object o = field.get(obj);
62 result.put(o.toString(), obj);
63 } catch (Exception e1) {
64 String stackTrace = EcompPortalUtils.getStackTrace(e1);
65 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", field " + parmName + ". Details: ", stackTrace);
66 return new HashMap<String, T>();
69 } catch (Exception e) {
70 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
71 String getterName = "get" + (name.length() == 1 ? name.toUpperCase() : (name.substring(0, 1).toUpperCase() + name.substring(1)));
73 Class<?>[] parmClasses = null;
74 Method method = object.getClass().getMethod(getterName, parmClasses);
75 Object[] parmValues = new Object[0];
78 Object o = method.invoke(obj, parmValues);
80 result.put(o.toString(), obj);
81 } catch (Exception e2) {
82 String stackTrace = EcompPortalUtils.getStackTrace(e2);
83 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", method " + getterName + ". Details: ", stackTrace);
84 return new HashMap<String, T>();
87 } catch (Exception e3) {
88 String stackTrace = EcompPortalUtils.getStackTrace(e3);
89 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", bad field '" + name + "' or method '" + getterName + "()'. Details: " + stackTrace);
90 return new HashMap<String, T>();
94 if (list.size() != result.size()) {
95 logger.warn(EELFLoggerDelegate.errorLogger, "Duplicated or empty keys were found!!!");