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.codec.binary.Base64;
20 import org.apache.commons.collections4.MapUtils;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.PrintWriter;
28 import java.io.Serializable;
29 import java.io.StringWriter;
30 import java.lang.reflect.Array;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Objects;
41 import java.util.UUID;
44 * This class provides auxiliary static methods.
46 public class CommonMethods {
48 private static final char[] CHARS = new char[]{
49 '0', '1', '2', '3', '4', '5', '6', '7',
50 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
52 private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
55 * Private default constructor to prevent instantiation of the class objects.
57 private CommonMethods() {
61 * Serializes an object instance into byte array.
63 * @param object An instance to be serialized.
64 * @return Java array of bytes.
65 * @see #deserializeObject(byte[]) #deserializeObject(byte[])
67 public static byte[] serializeObject(Serializable object) {
68 ByteArrayOutputStream byteArray = new ByteArrayOutputStream(2048);
70 ObjectOutputStream ds = new ObjectOutputStream(byteArray);
71 ds.writeObject(object);
73 } catch (IOException exception) {
74 throw new RuntimeException(exception);
77 return byteArray.toByteArray();
81 * Deserializes an object instance.
83 * @param bytes Java array of bytes.
84 * @return Deserialized instance of an object.
85 * @see #serializeObject(Serializable) #serializeObject(Serializable)
87 public static Serializable deserializeObject(byte[] bytes) {
88 Serializable obj = null;
90 ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(bytes));
91 obj = (Serializable) stream.readObject();
93 } catch (IOException | ClassNotFoundException exception) {
94 throw new RuntimeException(exception);
98 } // deserializeObject
101 * Encodes binary byte stream to ASCII format.
103 * @param binary An Java array of bytes in binary format.
104 * @return An Java array of bytes encoded in ASCII format.
105 * @see #decode(byte[]) #decode(byte[])
107 public static byte[] encode(byte[] binary) {
108 return Base64.encodeBase64(binary);
112 * Decodes ASCII byte stream into binary format.
114 * @param ascii An Java array of bytes in ASCII format.
115 * @return An Java array of bytes encoded in binary format.
116 * @see #encode(byte[]) #encode(byte[])
118 public static byte[] decode(byte[] ascii) {
119 return Base64.decodeBase64(ascii);
123 * Checks whether the given <tt>Object</tt> is empty.
125 * @param obj Object to be checked.
126 * @return <tt>true</tt> - if the Object is null, <tt>false</tt> otherwise.
128 public static boolean isEmpty(Object obj) {
133 * Checks whether the given <tt>Object</tt> is empty.
135 * @param byteArray Object to be checked.
136 * @return <tt>true</tt> - if the Object is null, <tt>false</tt> otherwise.
138 public static boolean isEmpty(byte[] byteArray) {
139 return byteArray == null || byteArray.length == 0;
143 * Checks whether the given <tt>String</tt> is empty.
145 * @param str String object to be checked.
146 * @return <tt>true</tt> - if the String is null or empty, <tt>false</tt> - otherwise.
148 public static boolean isEmpty(String str) {
149 return str == null || str.length() == 0;
153 * Checks whether the given Java array is empty.
155 * @param array Java array to be checked.
156 * @return <tt>true</tt> - if the array is null or empty, <tt>false</tt> - otherwise.
158 public static boolean isEmpty(Object[] array) {
159 return array == null || array.length == 0;
163 * Checks whether the given collection is empty.
165 * @param collection A collection to be checked.
166 * @return <tt>true</tt> - if the collection is null or empty, <tt>false</tt> - otherwise.
168 public static boolean isEmpty(Collection<?> collection) {
169 return collection == null || collection.isEmpty();
173 * Checks whether the given map is empty.
175 * @param map A map to be checked.
176 * @return <tt>true</tt> - if the map is null or empty, <tt>false</tt> - otherwise.
178 public static boolean isEmpty(Map<?, ?> map) {
179 return map == null || map.isEmpty();
183 * Converts the array with Long elements to the array with long (primitive type).
185 * @param array input array with Long elements
186 * @return array with the same elements converted to the long type (primitive)
188 public static long[] toPrimitive(Long[] array) {
193 long[] result = new long[array.length];
194 for (int i = 0; i < array.length; i++) {
195 result[i] = array[i] != null ? array[i] : 0L;
201 * Converts a collection to Java array.
203 * @param <T> Java type of the collection element.
204 * @param col Collection to be converted to array
205 * @param type Java type of collection/array element
206 * @return An Java array of collection elements, or empty array if collection is null or empty.
208 @SuppressWarnings("unchecked")
209 public static <T> T[] toArray(Collection<? extends T> col, Class<T> type) {
210 int length = isEmpty(col) ? 0 : col.size();
211 T[] array = (T[]) Array.newInstance(type, length);
212 return col != null ? col.toArray(array) : array;
216 * Gets an universally unique identifier (UUID).
218 * @return String representation of generated UUID.
220 public static String nextUuId() {
221 UUID uuid = UUID.randomUUID();
223 StringBuilder buff = new StringBuilder(32);
224 long2string(uuid.getMostSignificantBits(), buff);
225 long2string(uuid.getLeastSignificantBits(), buff);
227 return buff.toString();
230 private static void long2string(long lng, StringBuilder buff) {
232 for (int i = 0; i < 16; i++) {
233 long nextByte = value & 0xF000000000000000L;
235 boolean isNegative = nextByte < 0;
236 nextByte = rightShift(nextByte, 60);
242 buff.append(CHARS[(int) nextByte]);
246 private static long rightShift(long lng, int num) {
251 * Concatenates two Java arrays. The method allocates a new array and copies
252 * all elements to it or returns one of input arrays if another one is
255 * @param <T> the type parameter
256 * @param left Elements of this array will be copied to positions from 0 to <tt>left.length -
257 * 1</tt> in the target array.
258 * @param right Elements of this array will be copied to positions from <tt>left.length</tt> to
259 * <tt>left.length + right.length</tt>
260 * @return A newly allocate Java array that accommodates elements of source (left/right) arrays
261 orone of source arrays if another is empty, <tt>null</tt> - otherwise.
263 @SuppressWarnings("unchecked")
264 public static <T> T[] concat(T[] left, T[] right) {
269 } else if (isEmpty(right)) {
272 res = (T[]) Array.newInstance(left[0].getClass(), left.length + right.length);
273 System.arraycopy(left, 0, res, 0, left.length);
274 System.arraycopy(right, 0, res, left.length, right.length);
281 * Casts an object to the class or interface represented by the specified
282 * <tt>Class</tt> object. The method logic is similar to Java method
283 * <tt>Class.cast(Object)</tt> with the only difference that unlike Java's
284 * version the type name of the current object instance is specified in the
285 * error message if casting fails to simplify error tracking.
287 * @param <B> the type parameter
288 * @param <D> the type parameter
289 * @param b1 An object instance to be casted to the specified Java type.
290 * @param cls Target Java type.
291 * @return Object instance safely casted to the requested Java type.
292 * @throws ClassCastException In case which is the given object is not instance of the specified
295 @SuppressWarnings("unchecked")
296 public static <B, D> D cast(B b1, Class<D> cls) {
299 if (!cls.isInstance(b1)) {
300 throw new ClassCastException(String
301 .format("Failed to cast from '%s' to '%s'", b1.getClass().getName(), cls.getName()));
311 * New instance object.
313 * @param classname the classname
316 public static Object newInstance(String classname) {
317 return newInstance(classname, Object.class);
323 * @param <T> the type parameter
324 * @param classname the classname
328 @SuppressWarnings("unchecked")
329 public static <T> T newInstance(String classname, Class<T> cls) {
331 if (isEmpty(classname)) {
332 throw new IllegalArgumentException();
336 throw new IllegalArgumentException();
340 Class<?> temp = Class.forName(classname);
342 if (!cls.isAssignableFrom(temp)) {
343 throw new ClassCastException(
344 String.format("Failed to cast from '%s' to '%s'", classname, cls.getName()));
347 Class<? extends T> impl = (Class<? extends T>) temp;
349 return newInstance(impl);
350 } catch (ClassNotFoundException exception) {
351 throw new IllegalArgumentException(exception);
358 * @param <T> the type parameter
362 public static <T> T newInstance(Class<T> cls) {
364 return cls.newInstance();
365 } catch (InstantiationException | IllegalAccessException exception) {
366 throw new RuntimeException(exception);
371 * Gets resources path.
373 * @param resourceName the resource name
374 * @return the resources path
376 public static String getResourcesPath(String resourceName) {
377 URL resourceUrl = CommonMethods.class.getClassLoader().getResource(resourceName);
378 return resourceUrl != null ? resourceUrl.getPath()
379 .substring(0, resourceUrl.getPath().lastIndexOf("/") + 1) : null;
385 * @param throwable the throwable
386 * @return the stack trace
388 public static String getStackTrace(Throwable throwable) {
389 if (null == throwable) {
392 StringWriter sw = new StringWriter();
393 throwable.printStackTrace(new PrintWriter(sw));
394 return sw.toString();
398 * Print stack trace string.
402 public static String printStackTrace() {
403 StringBuilder sb = new StringBuilder();
404 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
405 for (StackTraceElement traceElement : trace) {
406 sb.append("\t ").append(traceElement);
407 sb.append(System.lineSeparator());
409 return sb.toString();
413 * Is equal object boolean.
415 * @param obj1 the obj 1
416 * @param obj2 the obj 2
417 * @return the boolean
419 public static boolean isEqualObject(Object obj1, Object obj2) {
420 boolean isEqualValue = false;
421 if (obj1 == null && obj2 == null) {
425 if (!isEqualValue && obj1 != null && obj2 != null && obj1.equals(obj2)) {
432 * Converts array of strings to comma-separated string.
434 * @param arr array of strings
437 public static String arrayToCommaSeparatedString(String[] arr) {
438 return arrayToSeparatedString(arr, ',');
442 * Collection to comma separated string string.
444 * @param elementCollection the element collection
447 public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
448 List<String> list = new ArrayList<>();
449 list.addAll(elementCollection);
450 return listToSeparatedString(list, ',');
454 * Converts array of strings to string separated with specified character.
456 * @param arr array of strings
457 * @param separator the separator
460 public static String arrayToSeparatedString(String[] arr, char separator) {
461 return listToSeparatedString(Arrays.asList(arr), separator);
465 * Converts array of strings to string separated with specified character.
467 * @param list array of strings
468 * @param separator the separator
471 public static String listToSeparatedString(List<String> list, char separator) {
474 StringBuilder sb = new StringBuilder();
475 int sz = list.size();
476 for (int i = 0; i < sz; i++) {
478 sb.append(separator);
480 sb.append(list.get(i));
488 * Duplicate string with delimiter string.
491 * @param separator the separator
492 * @param numberOfDuplications the number of duplications
495 public static String duplicateStringWithDelimiter(String arg, char separator,
496 int numberOfDuplications) {
497 StringBuilder sb = new StringBuilder();
499 for (int i = 0; i < numberOfDuplications; i++) {
501 sb.append(separator);
505 return sb.toString();
509 * Bytes to hex string.
511 * @param bytes the bytes
514 public static String bytesToHex(byte[] bytes) {
515 char[] hexChars = new char[bytes.length * 2];
516 for (int j = 0; j < bytes.length; j++) {
517 int var = bytes[j] & 0xFF;
519 hexChars[x1] = hexArray[var >>> 4];
520 hexChars[x1 + 1] = hexArray[var & 0x0F];
522 return new String(hexChars);
526 * To single element set set.
528 * @param <T> the class of the objects in the set
529 * @param element the single element to be contained in the returned Set
530 * @return an immutable set containing only the specified object. The returned set is
533 public static <T> Set<T> toSingleElementSet(T element) {
534 return Collections.singleton(element);
539 * Merge lists of map list.
541 * @param <T> the type parameter
542 * @param <S> the type parameter
543 * @param target the target
544 * @param source the source
547 public static <T, S> List<Map<T, S>> mergeListsOfMap(List<Map<T, S>> target,
548 List<Map<T, S>> source) {
549 List<Map<T, S>> retList = new ArrayList<>();
550 if (Objects.nonNull(target)) {
551 retList.addAll(target);
554 if (Objects.nonNull(source)) {
555 for (Map<T, S> sourceMap : source) {
556 for (Map.Entry<T, S> entry : sourceMap.entrySet()) {
557 mergeEntryInList(entry.getKey(), entry.getValue(), retList);
567 * @param <T> the type parameter
568 * @param target the target
569 * @param source the source
572 public static <T> List<T> mergeLists(List<T> target, List<T> source) {
573 List<T> retList = new ArrayList<>();
575 if (Objects.nonNull(source)) {
576 retList.addAll(source);
578 if (Objects.nonNull(target)) {
579 retList.addAll(target);
586 * Merge entry in list.
588 * @param <T> the type parameter
589 * @param <S> the type parameter
591 * @param value the value
592 * @param target the target
594 public static <T, S> void mergeEntryInList(T key, S value, List<Map<T, S>> target) {
595 boolean found = false;
596 for (Map<T, S> map : target) {
597 if (map.containsKey(key)) {
604 Map<T, S> newMap = new HashMap<>();
605 newMap.put(key, value);
614 * @param <T> the type parameter
615 * @param <S> the type parameter
616 * @param target the target
617 * @param source the source
620 public static <T, S> Map<T, S> mergeMaps(Map<T, S> target, Map<T, S> source) {
621 Map<T, S> retMap = new HashMap<>();
622 if (MapUtils.isNotEmpty(source)) {
623 retMap.putAll(source);
625 if (MapUtils.isNotEmpty(target)) {
626 retMap.putAll(target);