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.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Objects;
28 import java.util.UUID;
30 import org.apache.commons.collections4.MapUtils;
31 import org.apache.commons.lang3.ArrayUtils;
32 import org.apache.commons.lang3.StringUtils;
35 * This class provides auxiliary static methods.
37 public class CommonMethods {
39 private static final char[] CHARS = new char[] {
40 '0', '1', '2', '3', '4', '5', '6', '7',
41 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
45 * Private default constructor to prevent instantiation of the class objects.
47 private CommonMethods() {
51 * Gets an universally unique identifier (UUID).
53 * @return String representation of generated UUID.
55 public static String nextUuId() {
56 UUID uuid = UUID.randomUUID();
58 StringBuilder buff = new StringBuilder(32);
59 long2string(uuid.getMostSignificantBits(), buff);
60 long2string(uuid.getLeastSignificantBits(), buff);
62 return buff.toString();
65 private static void long2string(long lng, StringBuilder buff) {
67 for (int i = 0; i < 16; i++) {
68 long nextByte = value & 0xF000000000000000L;
70 boolean isNegative = nextByte < 0;
71 nextByte = nextByte >>> 60;
77 buff.append(CHARS[(int) nextByte]);
82 * Concatenates two Java arrays. The method allocates a new array and copies
83 * all elements to it or returns one of input arrays if another one is
86 * @param <T> the type parameter
87 * @param left Elements of this array will be copied to positions from 0 to <tt>left.length -
88 * 1</tt> in the target array.
89 * @param right Elements of this array will be copied to positions from <tt>left.length</tt> to
90 * <tt>left.length + right.length</tt>
91 * @return A newly allocate Java array that accommodates elements of source (left/right) arrays
92 * or one of source arrays if another is empty, <tt>null</tt> - otherwise.
94 @SuppressWarnings("unchecked")
95 public static <T> T[] concat(T[] left, T[] right) {
98 if (ArrayUtils.isEmpty(left)) {
100 } else if (ArrayUtils.isEmpty(right)) {
103 res = (T[]) Array.newInstance(left[0].getClass(), left.length + right.length);
104 System.arraycopy(left, 0, res, 0, left.length);
105 System.arraycopy(right, 0, res, left.length, right.length);
112 * New instance object.
114 * @param classname the classname
117 public static Object newInstance(String classname) {
118 return newInstance(classname, Object.class);
124 * @param <T> the type parameter
125 * @param classname the classname
129 @SuppressWarnings("unchecked")
130 public static <T> T newInstance(String classname, Class<T> cls) {
132 if (StringUtils.isEmpty(classname)) {
133 throw new IllegalArgumentException();
137 throw new IllegalArgumentException();
141 Class<?> temp = Class.forName(classname);
143 if (!cls.isAssignableFrom(temp)) {
144 throw new ClassCastException(
145 String.format("Failed to cast from '%s' to '%s'", classname, cls.getName()));
148 Class<? extends T> impl = (Class<? extends T>) temp;
150 return newInstance(impl);
151 } catch (ClassNotFoundException exception) {
152 throw new IllegalArgumentException(exception);
159 * @param <T> the type parameter
163 public static <T> T newInstance(Class<T> cls) {
165 return cls.newInstance();
166 } catch (InstantiationException | IllegalAccessException exception) {
167 throw new RuntimeException(exception);
172 * Print stack trace string.
176 public static String printStackTrace() {
177 StringBuilder sb = new StringBuilder();
178 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
179 for (StackTraceElement traceElement : trace) {
180 sb.append("\t ").append(traceElement);
181 sb.append(System.lineSeparator());
183 return sb.toString();
187 * Converts array of strings to comma-separated string.
189 * @param arr array of strings
192 public static String arrayToCommaSeparatedString(String[] arr) {
193 return arrayToSeparatedString(arr, ',');
197 * Collection to comma separated string string.
199 * @param elementCollection the element collection
202 public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
203 return String.join(",", elementCollection);
207 * Converts array of strings to string separated with specified character.
209 * @param arr array of strings
210 * @param separator the separator
213 public static String arrayToSeparatedString(String[] arr, char separator) {
214 return String.join(Character.toString(separator), arr);
218 * Converts array of strings to string separated with specified character.
220 * @param list array of strings
221 * @param separator the separator
224 public static String listToSeparatedString(List<String> list, char separator) {
225 return String.join(Character.toString(separator), list);
229 * Duplicate string with delimiter string.
232 * @param separator the separator
233 * @param numberOfDuplications the number of duplications
236 public static String duplicateStringWithDelimiter(String arg, char separator,
237 int numberOfDuplications) {
238 StringBuilder sb = new StringBuilder();
240 for (int i = 0; i < numberOfDuplications; i++) {
242 sb.append(separator);
246 return sb.toString();
250 * To single element set set.
252 * @param <T> the class of the objects in the set
253 * @param element the single element to be contained in the returned Set
254 * @return an immutable set containing only the specified object. The returned set is
257 public static <T> Set<T> toSingleElementSet(T element) {
258 return Collections.singleton(element);
263 * Merge lists of map list.
265 * @param <T> the type parameter
266 * @param <S> the type parameter
267 * @param target the target
268 * @param source the source
271 public static <T, S> List<Map<T, S>> mergeListsOfMap(List<Map<T, S>> target,
272 List<Map<T, S>> source) {
273 List<Map<T, S>> retList = new ArrayList<>();
274 if (Objects.nonNull(target)) {
275 retList.addAll(target);
278 if (Objects.nonNull(source)) {
279 for (Map<T, S> sourceMap : source) {
280 for (Map.Entry<T, S> entry : sourceMap.entrySet()) {
281 mergeEntryInList(entry.getKey(), entry.getValue(), retList);
291 * @param <T> the type parameter
292 * @param target the target
293 * @param source the source
296 public static <T> List<T> mergeLists(List<T> target, List<T> source) {
297 List<T> retList = new ArrayList<>();
299 if (Objects.nonNull(source)) {
300 retList.addAll(source);
302 if (Objects.nonNull(target)) {
303 retList.addAll(target);
310 * Merge entry in list.
312 * @param <T> the type parameter
313 * @param <S> the type parameter
315 * @param value the value
316 * @param target the target
318 public static <T, S> void mergeEntryInList(T key, S value, List<Map<T, S>> target) {
319 boolean found = false;
320 for (Map<T, S> map : target) {
321 if (map.containsKey(key)) {
328 Map<T, S> newMap = new HashMap<>();
329 newMap.put(key, value);
338 * @param <T> the type parameter
339 * @param <S> the type parameter
340 * @param firstMap the firstMap
341 * @param secondMap the secondMap
343 * Second Map is overridden data from the first map
345 public static <T, S> Map<T, S> mergeMaps(Map<T, S> firstMap, Map<T, S> secondMap) {
346 Map<T, S> retMap = new HashMap<>();
347 if (MapUtils.isNotEmpty(firstMap)) {
348 retMap.putAll(firstMap);
350 if (MapUtils.isNotEmpty(secondMap)) {
351 retMap.putAll(secondMap);