2 * Copyright © 2016-2017 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 org.apache.commons.collections4.MapUtils;
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.apache.commons.lang3.StringUtils;
23 import java.lang.reflect.Array;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Objects;
32 import java.util.UUID;
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 * Checks whether the given collection is empty.
53 * @param collection A collection to be checked.
54 * @return <tt>true</tt> - if the collection is null or empty, <tt>false</tt> - otherwise.
56 public static boolean isEmpty(Collection<?> collection) {
57 return collection == null || collection.isEmpty();
61 * Gets an universally unique identifier (UUID).
63 * @return String representation of generated UUID.
65 public static String nextUuId() {
66 UUID uuid = UUID.randomUUID();
68 StringBuilder buff = new StringBuilder(32);
69 long2string(uuid.getMostSignificantBits(), buff);
70 long2string(uuid.getLeastSignificantBits(), buff);
72 return buff.toString();
75 private static void long2string(long lng, StringBuilder buff) {
77 for (int i = 0; i < 16; i++) {
78 long nextByte = value & 0xF000000000000000L;
80 boolean isNegative = nextByte < 0;
81 nextByte = nextByte >>> 60;
87 buff.append(CHARS[(int) nextByte]);
92 * Concatenates two Java arrays. The method allocates a new array and copies
93 * all elements to it or returns one of input arrays if another one is
96 * @param <T> the type parameter
97 * @param left Elements of this array will be copied to positions from 0 to <tt>left.length -
98 * 1</tt> in the target array.
99 * @param right Elements of this array will be copied to positions from <tt>left.length</tt> to
100 * <tt>left.length + right.length</tt>
101 * @return A newly allocate Java array that accommodates elements of source (left/right) arrays
102 orone of source arrays if another is empty, <tt>null</tt> - otherwise.
104 @SuppressWarnings("unchecked")
105 public static <T> T[] concat(T[] left, T[] right) {
108 if (ArrayUtils.isEmpty(left)) {
110 } else if (ArrayUtils.isEmpty(right)) {
113 res = (T[]) Array.newInstance(left[0].getClass(), left.length + right.length);
114 System.arraycopy(left, 0, res, 0, left.length);
115 System.arraycopy(right, 0, res, left.length, right.length);
122 * New instance object.
124 * @param classname the classname
127 public static Object newInstance(String classname) {
128 return newInstance(classname, Object.class);
134 * @param <T> the type parameter
135 * @param classname the classname
139 @SuppressWarnings("unchecked")
140 public static <T> T newInstance(String classname, Class<T> cls) {
142 if (StringUtils.isEmpty(classname)) {
143 throw new IllegalArgumentException();
147 throw new IllegalArgumentException();
151 Class<?> temp = Class.forName(classname);
153 if (!cls.isAssignableFrom(temp)) {
154 throw new ClassCastException(
155 String.format("Failed to cast from '%s' to '%s'", classname, cls.getName()));
158 Class<? extends T> impl = (Class<? extends T>) temp;
160 return newInstance(impl);
161 } catch (ClassNotFoundException exception) {
162 throw new IllegalArgumentException(exception);
169 * @param <T> the type parameter
173 public static <T> T newInstance(Class<T> cls) {
175 return cls.newInstance();
176 } catch (InstantiationException | IllegalAccessException exception) {
177 throw new RuntimeException(exception);
182 * Print stack trace string.
186 public static String printStackTrace() {
187 StringBuilder sb = new StringBuilder();
188 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
189 for (StackTraceElement traceElement : trace) {
190 sb.append("\t ").append(traceElement);
191 sb.append(System.lineSeparator());
193 return sb.toString();
197 * Converts array of strings to comma-separated string.
199 * @param arr array of strings
202 public static String arrayToCommaSeparatedString(String[] arr) {
203 return arrayToSeparatedString(arr, ',');
207 * Collection to comma separated string string.
209 * @param elementCollection the element collection
212 public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
213 return String.join(",", elementCollection);
217 * Converts array of strings to string separated with specified character.
219 * @param arr array of strings
220 * @param separator the separator
223 public static String arrayToSeparatedString(String[] arr, char separator) {
224 return String.join(Character.toString(separator), arr);
228 * Converts array of strings to string separated with specified character.
230 * @param list array of strings
231 * @param separator the separator
234 public static String listToSeparatedString(List<String> list, char separator) {
235 return String.join(Character.toString(separator), list);
239 * Duplicate string with delimiter string.
242 * @param separator the separator
243 * @param numberOfDuplications the number of duplications
246 public static String duplicateStringWithDelimiter(String arg, char separator,
247 int numberOfDuplications) {
248 StringBuilder sb = new StringBuilder();
250 for (int i = 0; i < numberOfDuplications; i++) {
252 sb.append(separator);
256 return sb.toString();
260 * To single element set set.
262 * @param <T> the class of the objects in the set
263 * @param element the single element to be contained in the returned Set
264 * @return an immutable set containing only the specified object. The returned set is
267 public static <T> Set<T> toSingleElementSet(T element) {
268 return Collections.singleton(element);
273 * Merge lists of map list.
275 * @param <T> the type parameter
276 * @param <S> the type parameter
277 * @param target the target
278 * @param source the source
281 public static <T, S> List<Map<T, S>> mergeListsOfMap(List<Map<T, S>> target,
282 List<Map<T, S>> source) {
283 List<Map<T, S>> retList = new ArrayList<>();
284 if (Objects.nonNull(target)) {
285 retList.addAll(target);
288 if (Objects.nonNull(source)) {
289 for (Map<T, S> sourceMap : source) {
290 for (Map.Entry<T, S> entry : sourceMap.entrySet()) {
291 mergeEntryInList(entry.getKey(), entry.getValue(), retList);
301 * @param <T> the type parameter
302 * @param target the target
303 * @param source the source
306 public static <T> List<T> mergeLists(List<T> target, List<T> source) {
307 List<T> retList = new ArrayList<>();
309 if (Objects.nonNull(source)) {
310 retList.addAll(source);
312 if (Objects.nonNull(target)) {
313 retList.addAll(target);
320 * Merge entry in list.
322 * @param <T> the type parameter
323 * @param <S> the type parameter
325 * @param value the value
326 * @param target the target
328 public static <T, S> void mergeEntryInList(T key, S value, List<Map<T, S>> target) {
329 boolean found = false;
330 for (Map<T, S> map : target) {
331 if (map.containsKey(key)) {
338 Map<T, S> newMap = new HashMap<>();
339 newMap.put(key, value);
348 * @param <T> the type parameter
349 * @param <S> the type parameter
350 * @param target the target
351 * @param source the source
354 public static <T, S> Map<T, S> mergeMaps(Map<T, S> target, Map<T, S> source) {
355 Map<T, S> retMap = new HashMap<>();
356 if (MapUtils.isNotEmpty(source)) {
357 retMap.putAll(source);
359 if (MapUtils.isNotEmpty(target)) {
360 retMap.putAll(target);