2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.utils;
\r
22 import java.lang.reflect.Field;
\r
23 import java.lang.reflect.Method;
\r
24 import java.util.HashMap;
\r
25 import java.util.List;
\r
27 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
31 * @author Vladimir Turovets This class is used to create HashMap<String, T>
\r
32 * from the list of T objects. We suppose that
\r
33 * 1) T object contains field 'parmName' or getter 'getParmName()'.
\r
34 * The value of object.parmName or object.getParmName() is used as
\r
35 * a key in created hashMap.
\r
36 * 2) for all objects in the list 'parmName' or getter
\r
37 * 'getParmName().toString' has to be unique and not null.
\r
38 * This class has one function only:
\r
39 * HashMap<String, T> hashMap(List<T> list, String name) and returns
\r
40 * hash map created from list.
\r
44 public class HashMapFromList<T> {
\r
45 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HashMapFromList.class);
\r
47 public HashMap<String, T> hashMap(List<T> list, String name) {
\r
48 HashMap<String, T> result = new HashMap<String, T>();
\r
49 if (list == null || list.size() == 0 || name == null) {
\r
53 if (name.length() > 0) {
\r
54 T object = list.get(0);
\r
56 String parmName = name;
\r
57 Field field = object.getClass().getField(parmName);
\r
58 for (T obj : list) {
\r
60 Object o = field.get(obj);
\r
62 result.put(o.toString(), obj);
\r
63 } catch (Exception e1) {
\r
64 String stackTrace = EcompPortalUtils.getStackTrace(e1);
\r
65 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", field " + parmName + ". Details: ", stackTrace);
\r
66 return new HashMap<String, T>();
\r
69 } catch (Exception e) {
\r
70 String getterName = "get" + (name.length() == 1 ? name.toUpperCase() : (name.substring(0, 1).toUpperCase() + name.substring(1)));
\r
72 Class<?>[] parmClasses = null;
\r
73 Method method = object.getClass().getMethod(getterName, parmClasses);
\r
74 Object[] parmValues = new Object[0];
\r
75 for (T obj : list) {
\r
77 Object o = method.invoke(obj, parmValues);
\r
79 result.put(o.toString(), obj);
\r
80 } catch (Exception e2) {
\r
81 String stackTrace = EcompPortalUtils.getStackTrace(e2);
\r
82 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", method " + getterName + ". Details: ", stackTrace);
\r
83 return new HashMap<String, T>();
\r
86 } catch (Exception e3) {
\r
87 String stackTrace = EcompPortalUtils.getStackTrace(e3);
\r
88 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", bad field '" + name + "' or method '" + getterName + "()'. Details: " + stackTrace);
\r
89 return new HashMap<String, T>();
\r
93 if (list.size() != result.size()) {
\r
94 logger.warn(EELFLoggerDelegate.errorLogger, "Duplicated or empty keys were found!!!");
\r