2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.utilities;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
29 import java.util.UUID;
31 import org.apache.commons.collections4.MapUtils;
32 import org.apache.commons.lang3.ArrayUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.core.utilities.exception.NewInstanceRuntimeException;
37 * This class provides auxiliary static methods.
39 public class CommonMethods {
41 private static final char[] CHARS = new char[] {
42 '0', '1', '2', '3', '4', '5', '6', '7',
43 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
47 * Private default constructor to prevent instantiation of the class objects.
49 private CommonMethods() {
53 * Gets an universally unique identifier (UUID).
55 * @return String representation of generated UUID.
57 public static String nextUuId() {
58 UUID uuid = UUID.randomUUID();
60 StringBuilder buff = new StringBuilder(32);
61 long2string(uuid.getMostSignificantBits(), buff);
62 long2string(uuid.getLeastSignificantBits(), buff);
64 return buff.toString();
67 private static void long2string(long lng, StringBuilder buff) {
69 for (int i = 0; i < 16; i++) {
70 long nextByte = value & 0xF000000000000000L;
72 boolean isNegative = nextByte < 0;
73 nextByte = nextByte >>> 60;
79 buff.append(CHARS[(int) nextByte]);
84 * Concatenates two Java arrays. The method allocates a new array and copies
85 * all elements to it or returns one of input arrays if another one is
88 * @param <T> the type parameter
89 * @param left Elements of this array will be copied to positions from 0 to <tt>left.length -
90 * 1</tt> in the target array.
91 * @param right Elements of this array will be copied to positions from <tt>left.length</tt> to
92 * <tt>left.length + right.length</tt>
93 * @return A newly allocate Java array that accommodates elements of source (left/right) arrays
94 * or one of source arrays if another is empty, <tt>null</tt> - otherwise.
96 @SuppressWarnings("unchecked")
97 public static <T> T[] concat(T[] left, T[] right) {
100 if (ArrayUtils.isEmpty(left)) {
102 } else if (ArrayUtils.isEmpty(right)) {
105 res = (T[]) Array.newInstance(left[0].getClass(), left.length + right.length);
106 System.arraycopy(left, 0, res, 0, left.length);
107 System.arraycopy(right, 0, res, left.length, right.length);
114 * New instance object.
116 * @param classname the classname
119 public static Object newInstance(String classname) {
120 return newInstance(classname, Object.class);
126 * @param <T> the type parameter
127 * @param classname the classname
131 @SuppressWarnings("unchecked")
132 public static <T> T newInstance(String classname, Class<T> cls) {
134 if (StringUtils.isEmpty(classname)) {
135 throw new IllegalArgumentException();
139 throw new IllegalArgumentException();
143 Class<?> temp = Class.forName(classname);
145 if (!cls.isAssignableFrom(temp)) {
146 throw new ClassCastException(
147 String.format("Failed to cast from '%s' to '%s'", classname, cls.getName()));
150 Class<? extends T> impl = (Class<? extends T>) temp;
152 return newInstance(impl);
153 } catch (ClassNotFoundException exception) {
154 throw new IllegalArgumentException(exception);
161 * @param <T> the type parameter
165 public static <T> T newInstance(final Class<T> cls) {
167 return cls.getDeclaredConstructor().newInstance();
168 } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
169 throw new NewInstanceRuntimeException(String.format("Could not create instance for '%s'", cls.getName())
175 * Print stack trace string.
179 public static String printStackTrace() {
180 StringBuilder sb = new StringBuilder();
181 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
182 for (StackTraceElement traceElement : trace) {
183 sb.append("\t ").append(traceElement);
184 sb.append(System.lineSeparator());
186 return sb.toString();
190 * Converts array of strings to comma-separated string.
192 * @param arr array of strings
195 public static String arrayToCommaSeparatedString(String[] arr) {
196 return arrayToSeparatedString(arr, ',');
200 * Collection to comma separated string string.
202 * @param elementCollection the element collection
205 public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
206 return String.join(",", elementCollection);
210 * Converts array of strings to string separated with specified character.
212 * @param arr array of strings
213 * @param separator the separator
216 public static String arrayToSeparatedString(String[] arr, char separator) {
217 return String.join(Character.toString(separator), arr);
221 * Converts array of strings to string separated with specified character.
223 * @param list array of strings
224 * @param separator the separator
227 public static String listToSeparatedString(List<String> list, char separator) {
228 return String.join(Character.toString(separator), list);
232 * Duplicate string with delimiter string.
235 * @param separator the separator
236 * @param numberOfDuplications the number of duplications
239 public static String duplicateStringWithDelimiter(String arg, char separator,
240 int numberOfDuplications) {
241 StringBuilder sb = new StringBuilder();
243 for (int i = 0; i < numberOfDuplications; i++) {
245 sb.append(separator);
249 return sb.toString();
253 * To single element set set.
255 * @param <T> the class of the objects in the set
256 * @param element the single element to be contained in the returned Set
257 * @return an immutable set containing only the specified object. The returned set is
260 public static <T> Set<T> toSingleElementSet(T element) {
261 return Collections.singleton(element);
266 * Merge lists of map list.
268 * @param <T> the type parameter
269 * @param <S> the type parameter
270 * @param target the target
271 * @param source the source
274 public static <T, S> List<Map<T, S>> mergeListsOfMap(List<Map<T, S>> target,
275 List<Map<T, S>> source) {
276 List<Map<T, S>> retList = new ArrayList<>();
277 if (Objects.nonNull(target)) {
278 retList.addAll(target);
281 if (Objects.nonNull(source)) {
282 for (Map<T, S> sourceMap : source) {
283 for (Map.Entry<T, S> entry : sourceMap.entrySet()) {
284 mergeEntryInList(entry.getKey(), entry.getValue(), retList);
294 * @param <T> the type parameter
295 * @param target the target
296 * @param source the source
299 public static <T> List<T> mergeLists(List<T> target, List<T> source) {
300 List<T> retList = new ArrayList<>();
302 if (Objects.nonNull(source)) {
303 retList.addAll(source);
305 if (Objects.nonNull(target)) {
306 retList.addAll(target);
313 * Merge entry in list.
315 * @param <T> the type parameter
316 * @param <S> the type parameter
318 * @param value the value
319 * @param target the target
321 public static <T, S> void mergeEntryInList(T key, S value, List<Map<T, S>> target) {
322 boolean found = false;
323 for (Map<T, S> map : target) {
324 if (map.containsKey(key)) {
331 Map<T, S> newMap = new HashMap<>();
332 newMap.put(key, value);
341 * @param <T> the type parameter
342 * @param <S> the type parameter
343 * @param firstMap the firstMap
344 * @param secondMap the secondMap
346 * Second Map is overridden data from the first map
348 public static <T, S> Map<T, S> mergeMaps(Map<T, S> firstMap, Map<T, S> secondMap) {
349 Map<T, S> retMap = new HashMap<>();
350 if (MapUtils.isNotEmpty(firstMap)) {
351 retMap.putAll(firstMap);
353 if (MapUtils.isNotEmpty(secondMap)) {
354 retMap.putAll(secondMap);