Package com.evolveum.midpoint.util
Class MiscUtil
- java.lang.Object
-
- com.evolveum.midpoint.util.MiscUtil
-
public class MiscUtil extends Object
- Author:
- semancik
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMiscUtil.ExceptionSupplier<E>
-
Constructor Summary
Constructors Constructor Description MiscUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> voidaddAllIfNotPresent(List<T> receivingList, List<T> supplyingList)static <T> voidaddIfNotPresent(List<T> receivingList, T supplyingElement)static Booleanand(Boolean... operands)n-ary and that ignores null values.static voidargCheck(boolean condition, String template, Object... arguments)static DateasDate(int year, int month, int date, int hrs, int min, int sec)static DateasDate(XMLGregorianCalendar xgc)Converts an XMLGregorianCalendar to an instance of java.util.Datestatic @Nullable InstantasInstant(@Nullable Long millis)static @Nullable InstantasInstant(@Nullable XMLGregorianCalendar xgc)static <T> List<T>asListTreatingNull(T[] values)LikeArrays.asList(Object[])but if there's a single null value at input, creates an empty list.static LongasLong(XMLGregorianCalendar xgc)static voidassertCheck(boolean condition, String template, Object... arguments)static @Nullable XMLGregorianCalendarasXMLGregorianCalendar(@Nullable Long timeInMillis)Returns null for null input, but also for value of 0L.static @Nullable XMLGregorianCalendarasXMLGregorianCalendar(@Nullable Instant instant)static @Nullable XMLGregorianCalendarasXMLGregorianCalendar(@Nullable Date date)Converts a java.util.Date into an instance of XMLGregorianCalendarstatic @NotNull StringbinaryToHex(@org.jetbrains.annotations.NotNull byte[] bytes)static @Nullable StringbinaryToHexPreview(@org.jetbrains.annotations.Nullable byte[] bytes)Returns first 8 bytes from provided byte array as hexadecimal and adds length information.static @Nullable StringbinaryToHexPreview(@org.jetbrains.annotations.Nullable byte[] bytes, int previewLen)Returns couple of bytes from provided byte array as hexadecimal and adds length information.static <T> voidcarthesian(List<? extends Collection<T>> valuesForDimensions, Processor<List<T>> tupleProcessor)We have n dimensions (D1...Dn), each containing a number of values.static <T> TcastSafely(Object value, Class<T> expectedClass)static voidcheckCollectionImmutable(Collection<?> collection)static <K,V>
Map<K,V>cloneMap(Map<K,V> orig)Shallow clonestatic IntegercompareNullLast(Object o1, Object o2)static <T> @NotNull Collection<? extends T>concat(@NotNull Collection<? extends T> collection1, @NotNull Collection<? extends T> collection2)Returns a concatenation of two collections.static Stringconcat(Collection<String> stringCollection)static voidconfigCheck(boolean condition, String template, Object... arguments)static <T> booleancontains(T element, T[] array)static voidcopyDirectory(File src, File dst)Copy a directory and its contents.static voidcopyFile(File sourceFile, File destFile)static <T> Collection<T>createCollection(T... items)static <T extends Throwable>
TcreateSame(T original, String message)static Class<?>determineCommonAncestor(Collection<Class<?>> classes)static @NotNull StringemptyIfNull(String s)static <T> @NotNull Collection<T>emptyIfNull(Collection<T> collection)static <T> @NotNull List<T>emptyIfNull(List<T> list)static <T extends Enum<T>>
TenumFromOrdinal(Class<T> enumType, Integer ordinal)Converts integer ordinal number to enum value of the defined enum type.static @Nullable IntegerenumOrdinal(@Nullable Enum<?> enumValue)Returns ordinal value from nullable enum or returnsnull.static booleanequals(Object a, Object b)static <T> TextractSingleton(Collection<T> collection)static <T,E extends Throwable>
TextractSingleton(Collection<T> collection, Supplier<E> exceptionSupplier)static <T,E extends Throwable>
TextractSingletonRequired(Collection<T> collection, Supplier<E> multiExceptionSupplier, Supplier<E> noneExceptionSupplier)static <T> Collection<T>filter(Collection<T> input, Predicate<? super T> predicate)static <T> Set<T>filter(Set<T> input, Predicate<? super T> predicate)static <V> Vfind(Collection<V> values, V value, @NotNull Comparator<V> comparator)static StringfixFileName(String originalName)Crude attempt at replacing all illegal chars with underscores.static StringformatExceptionMessageWithCause(Throwable t)static StringformatExceptionMessageWithCause(Throwable t, int indent)static StringgetClass(Object object)static StringgetClassWithMessage(Throwable e)static XMLGregorianCalendargetEarliestTimeIgnoringNull(Collection<XMLGregorianCalendar> realValues)static <T> TgetFirstNonNull(T... values)static StringgetFirstNonNullString(Object... values)static StringgetObjectName(Object o)static <T> TgetRandomItem(List<T> collection)static StringgetSimpleClassName(Object o)static <T> TgetSingleValue(Collection<T> values, T defaultValue, String contextDescription)static <T> Collection<T>getValuesFromDisplayableValues(Collection<? extends DisplayableValue<T>> disps)static StringgetValueWithClass(Object object)static <T> booleanhasDuplicates(Collection<T> collection)Returns true if the collection contains at least one pair of equals elements.static booleanhasNoValue(Collection<?> collection)static byte[]hexToBinary(String hex)static StringhexToUtf8String(String hex)static <E> Function<Object,Stream<E>>instancesOf(Class<E> cls)static booleanisAllNull(Collection<?> collection)static booleanisBetween(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end)static booleanisCollectionOf(Object object, @NotNull Class<?> memberClass)static booleanisNoValue(Collection<?> collection)static booleanisSingleNull(Object[] values)static <T> List<T>join(Collection<T> a, Collection<T> b)static <T> Tlast(List<T> list)static Integermax(Integer a, Integer b)static Longmax(Long a, Long b)static Integermin(Integer a, Integer b)static Longmin(Long a, Long b)static <V> @NotNull Collection<V>nonNullValues(@NotNull Collection<V> values)static booleannullableCollectionsEqual(Collection<?> c1, Collection<?> c2)static StringnullIfEmpty(String s)static booleanonWindows()Are we running on Windows? (Rough estimate.)static doubleor0(Double value)static intor0(Integer value)static longor0(Long value)static <K,V>
Map<K,V>paramsToMap(Object[] params)static StringreadFile(File file)static StringreadZipFile(File file, Charset charset)static StringreplaceIllegalCharInFileNameOnWindows(String path)Replacing illegal characters in file name on Windows.static <T,E extends Throwable>
TrequireNonNull(T value, MiscUtil.ExceptionSupplier<E> exceptionSupplier)static <T> TrequireNonNull(T value, Supplier<String> messageSupplier)static <T> TrunChecked(CheckedFunction<Producer<T>,T> function, CheckedProducer<T> checkedProducer)static voidschemaCheck(boolean condition, String template, Object... arguments)static <T> List<T>singletonOrEmptyList(T value)static <T> Set<T>singletonOrEmptySet(T value)static voidsleepCatchingInterruptedException(long delay)Sleeps for specified time.static voidsleepNonInterruptibly(long delay)Sleeps for specified time.static voidsleepWatchfully(long until, long increment, Supplier<Boolean> canRunSupplier)Sleeps watching "can run" flag.static List<String>splitLines(String string)static booleanstartsWith(List<?> wholeList, List<?> subList)static voidstateCheck(boolean condition, String template, Object... arguments)static <T> @NotNull Stream<T>streamOf(Collection<T> collection)static StringstripHtmlMarkup(String htmlString)static StringtakeThreadDump(@Nullable Thread thread)Thanks for this code go to https://crunchify.com/how-to-generate-java-thread-dump-programmatically/static <T extends Throwable>
voidthrowAsSame(Throwable original, String message)Re-throws the original exception wrapped in the same class (e.g.static voidthrowExceptionAsUnchecked(Throwable t)static IntegertoInteger(Long value)static LongtoLong(Integer value)static StringtoString(Object o)static URLtoUrlUnchecked(URI uri)static @Nullable StringtrimString(@Nullable String value, int size)static <T> @NotNull Collection<T>union(Collection<T>... sets)static <T> Collection<? extends T>unionExtends(Collection<? extends T>... sets)static <T> booleanunorderedArrayEquals(T[] a, T[] b)static <T> booleanunorderedArrayEquals(T[] a, T[] b, Comparator<T> comparator)Only zero vs non-zero value of comparator is important.static <T> booleanunorderedCollectionCompare(Collection<T> a, Collection<T> b, Comparator<T> comparator)Only zero vs non-zero value of comparator is important.static <T> booleanunorderedCollectionEquals(Collection<? extends T> a, Collection<? extends T> b)static <A,B>
booleanunorderedCollectionEquals(Collection<A> a, Collection<B> b, HeteroComparator<A,B> comparator)Only zero vs non-zero value of comparator is important.static <T> intunorderedCollectionHashcode(Collection<T> collection, Predicate<T> filter)static <T> TunwrapTunnelledException(TunnelException te)static <T> TunwrapTunnelledExceptionToRuntime(TunnelException te)static voidwriteZipFile(File file, String entryName, String content, Charset charset)
-
-
-
Method Detail
-
union
@SafeVarargs @NotNull public static <T> @NotNull Collection<T> union(Collection<T>... sets)
-
unionExtends
@SafeVarargs public static <T> Collection<? extends T> unionExtends(Collection<? extends T>... sets)
-
unorderedCollectionEquals
public static <T> boolean unorderedCollectionEquals(Collection<? extends T> a, Collection<? extends T> b)
-
unorderedCollectionCompare
public static <T> boolean unorderedCollectionCompare(Collection<T> a, Collection<T> b, Comparator<T> comparator)
Only zero vs non-zero value of comparator is important.
-
unorderedCollectionEquals
public static <A,B> boolean unorderedCollectionEquals(Collection<A> a, Collection<B> b, HeteroComparator<A,B> comparator)
Only zero vs non-zero value of comparator is important.
-
unorderedArrayEquals
public static <T> boolean unorderedArrayEquals(T[] a, T[] b)
-
unorderedArrayEquals
public static <T> boolean unorderedArrayEquals(T[] a, T[] b, Comparator<T> comparator)Only zero vs non-zero value of comparator is important.
-
unorderedCollectionHashcode
public static <T> int unorderedCollectionHashcode(Collection<T> collection, Predicate<T> filter)
-
readFile
public static String readFile(File file) throws IOException
- Throws:
IOException
-
copyFile
public static void copyFile(File sourceFile, File destFile) throws IOException
- Throws:
IOException
-
copyDirectory
public static void copyDirectory(File src, File dst) throws IOException
Copy a directory and its contents.- Parameters:
src- The name of the directory to copy.dst- The name of the destination directory.- Throws:
IOException- If the directory could not be copied.
-
createCollection
@SafeVarargs public static <T> Collection<T> createCollection(T... items)
-
asXMLGregorianCalendar
@Nullable public static @Nullable XMLGregorianCalendar asXMLGregorianCalendar(@Nullable @Nullable Date date)
Converts a java.util.Date into an instance of XMLGregorianCalendar- Parameters:
date- Instance of java.util.Date or a null reference- Returns:
- XMLGregorianCalendar instance whose value is based upon the value in the date parameter. If the date parameter is null then this method will simply return null.
-
asXMLGregorianCalendar
@Nullable public static @Nullable XMLGregorianCalendar asXMLGregorianCalendar(@Nullable @Nullable Long timeInMillis)
Returns null for null input, but also for value of 0L.
-
asXMLGregorianCalendar
@Nullable public static @Nullable XMLGregorianCalendar asXMLGregorianCalendar(@Nullable @Nullable Instant instant)
-
asInstant
@Nullable public static @Nullable Instant asInstant(@Nullable @Nullable XMLGregorianCalendar xgc)
-
asDate
public static Date asDate(XMLGregorianCalendar xgc)
Converts an XMLGregorianCalendar to an instance of java.util.Date- Parameters:
xgc- Instance of XMLGregorianCalendar or a null reference- Returns:
- java.util.Date instance whose value is based upon the value in the xgc parameter. If the xgc parameter is null then this method will simply return null.
-
asLong
public static Long asLong(XMLGregorianCalendar xgc)
-
asDate
public static Date asDate(int year, int month, int date, int hrs, int min, int sec)
-
carthesian
public static <T> void carthesian(List<? extends Collection<T>> valuesForDimensions, Processor<List<T>> tupleProcessor)
We have n dimensions (D1...Dn), each containing a number of values.This method sequentially creates all n-tuples of these values (one value from each dimension) and invokes tupleProcessor on them.
-
concat
public static String concat(Collection<String> stringCollection)
-
isAllNull
public static boolean isAllNull(Collection<?> collection)
-
isNoValue
public static boolean isNoValue(Collection<?> collection)
-
hasNoValue
public static boolean hasNoValue(Collection<?> collection)
-
isBetween
public static boolean isBetween(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end)
-
contains
public static <T> boolean contains(T element, T[] array)
-
getValuesFromDisplayableValues
public static <T> Collection<T> getValuesFromDisplayableValues(Collection<? extends DisplayableValue<T>> disps)
-
binaryToHex
@NotNull public static @NotNull String binaryToHex(@NotNull @org.jetbrains.annotations.NotNull byte[] bytes)
-
hexToBinary
public static byte[] hexToBinary(String hex)
-
binaryToHexPreview
@Nullable public static @Nullable String binaryToHexPreview(@Nullable @org.jetbrains.annotations.Nullable byte[] bytes)
Returns first 8 bytes from provided byte array as hexadecimal and adds length information. Returns null if null array is provided.
-
binaryToHexPreview
@Nullable public static @Nullable String binaryToHexPreview(@Nullable @org.jetbrains.annotations.Nullable byte[] bytes, int previewLen)
Returns couple of bytes from provided byte array as hexadecimal and adds length information. Returns null if null array is provided.- Parameters:
previewLen- max number of bytes in the hexadecimal preview
-
addAllIfNotPresent
public static <T> void addAllIfNotPresent(List<T> receivingList, List<T> supplyingList)
-
addIfNotPresent
public static <T> void addIfNotPresent(List<T> receivingList, T supplyingElement)
-
nullableCollectionsEqual
public static boolean nullableCollectionsEqual(Collection<?> c1, Collection<?> c2)
-
getFirstNonNull
@SafeVarargs public static <T> T getFirstNonNull(T... values)
-
extractSingleton
public static <T> T extractSingleton(Collection<T> collection)
-
extractSingleton
public static <T,E extends Throwable> T extractSingleton(Collection<T> collection, Supplier<E> exceptionSupplier) throws E extends Throwable
- Throws:
E extends Throwable
-
extractSingletonRequired
@NotNull public static <T,E extends Throwable> T extractSingletonRequired(Collection<T> collection, Supplier<E> multiExceptionSupplier, Supplier<E> noneExceptionSupplier) throws E extends Throwable
- Throws:
E extends Throwable
-
getSingleValue
public static <T> T getSingleValue(Collection<T> values, T defaultValue, String contextDescription)
-
isCollectionOf
public static boolean isCollectionOf(Object object, @NotNull @NotNull Class<?> memberClass)
-
last
public static <T> T last(List<T> list)
-
emptyIfNull
@NotNull public static <T> @NotNull Collection<T> emptyIfNull(Collection<T> collection)
-
streamOf
@NotNull public static <T> @NotNull Stream<T> streamOf(Collection<T> collection)
-
hasDuplicates
public static <T> boolean hasDuplicates(Collection<T> collection)
Returns true if the collection contains at least one pair of equals elements.
-
formatExceptionMessageWithCause
public static String formatExceptionMessageWithCause(Throwable t, int indent)
-
throwExceptionAsUnchecked
public static void throwExceptionAsUnchecked(Throwable t)
-
runChecked
public static <T> T runChecked(CheckedFunction<Producer<T>,T> function, CheckedProducer<T> checkedProducer) throws CommonException
- Throws:
CommonException
-
unwrapTunnelledException
public static <T> T unwrapTunnelledException(TunnelException te) throws CommonException
- Throws:
CommonException
-
unwrapTunnelledExceptionToRuntime
public static <T> T unwrapTunnelledExceptionToRuntime(TunnelException te)
-
filter
public static <T> Collection<T> filter(Collection<T> input, Predicate<? super T> predicate)
-
nonNullValues
@NotNull public static <V> @NotNull Collection<V> nonNullValues(@NotNull @NotNull Collection<V> values)
-
join
public static <T> List<T> join(Collection<T> a, Collection<T> b)
-
takeThreadDump
public static String takeThreadDump(@Nullable @Nullable Thread thread)
Thanks for this code go to https://crunchify.com/how-to-generate-java-thread-dump-programmatically/
-
writeZipFile
public static void writeZipFile(File file, String entryName, String content, Charset charset) throws IOException
- Throws:
IOException
-
readZipFile
public static String readZipFile(File file, Charset charset) throws IOException
- Throws:
IOException
-
singletonOrEmptySet
public static <T> Set<T> singletonOrEmptySet(T value)
-
singletonOrEmptyList
public static <T> List<T> singletonOrEmptyList(T value)
-
castSafely
public static <T> T castSafely(Object value, Class<T> expectedClass) throws SchemaException
- Throws:
SchemaException
-
determineCommonAncestor
public static Class<?> determineCommonAncestor(Collection<Class<?>> classes)
-
throwAsSame
@Experimental public static <T extends Throwable> void throwAsSame(Throwable original, String message) throws T extends Throwable
Re-throws the original exception wrapped in the same class (e.g. SchemaException as SchemaException) but with additional message. It is used to preserve meaning of the exception but adding some contextual information.- Throws:
T extends Throwable
-
createSame
@Experimental public static <T extends Throwable> T createSame(T original, String message)
-
getEarliestTimeIgnoringNull
public static XMLGregorianCalendar getEarliestTimeIgnoringNull(Collection<XMLGregorianCalendar> realValues)
-
find
public static <V> V find(Collection<V> values, V value, @NotNull @NotNull Comparator<V> comparator)
-
enumFromOrdinal
public static <T extends Enum<T>> T enumFromOrdinal(Class<T> enumType, Integer ordinal)
Converts integer ordinal number to enum value of the defined enum type.- Parameters:
enumType- type of enum (class object)ordinal- ordinal value- Returns:
- enum value or null if ordinal is null
- Throws:
IndexOutOfBoundsException- If the ordinal value is out of enum value range
-
enumOrdinal
@Nullable public static @Nullable Integer enumOrdinal(@Nullable @Nullable Enum<?> enumValue)
Returns ordinal value from nullable enum or returnsnull.
-
trimString
@Nullable public static @Nullable String trimString(@Nullable @Nullable String value, int size)
-
requireNonNull
public static <T> T requireNonNull(T value, Supplier<String> messageSupplier) throws SchemaException- Throws:
SchemaException
-
isSingleNull
public static boolean isSingleNull(Object[] values)
-
sleepWatchfully
public static void sleepWatchfully(long until, long increment, Supplier<Boolean> canRunSupplier)Sleeps watching "can run" flag.InterruptedExceptionis caught (causing checking can run flag) but not propagated.
-
sleepCatchingInterruptedException
public static void sleepCatchingInterruptedException(long delay)
Sleeps for specified time.InterruptedExceptionis caught (causing immediate exit) but not propagated.
-
sleepNonInterruptibly
public static void sleepNonInterruptibly(long delay)
Sleeps for specified time.InterruptedExceptionis ignored - after we get it, we simply continue sleeping, until the whole specified time elapses. This is e.g. to simulate non-interruptible execution of a task.
-
getRandomItem
public static <T> T getRandomItem(List<T> collection)
-
requireNonNull
public static <T,E extends Throwable> T requireNonNull(T value, MiscUtil.ExceptionSupplier<E> exceptionSupplier) throws E extends Throwable
- Throws:
E extends Throwable
-
checkCollectionImmutable
public static void checkCollectionImmutable(Collection<?> collection)
-
schemaCheck
public static void schemaCheck(boolean condition, String template, Object... arguments) throws SchemaException- Throws:
SchemaException
-
configCheck
public static void configCheck(boolean condition, String template, Object... arguments) throws ConfigurationException- Throws:
ConfigurationException
-
assertCheck
public static void assertCheck(boolean condition, String template, Object... arguments)
-
asListTreatingNull
public static <T> List<T> asListTreatingNull(T[] values)
LikeArrays.asList(Object[])but if there's a single null value at input, creates an empty list.
-
or0
public static int or0(Integer value)
-
or0
public static long or0(Long value)
-
or0
public static double or0(Double value)
-
fixFileName
public static String fixFileName(String originalName)
Crude attempt at replacing all illegal chars with underscores. Maybe too strict.
-
replaceIllegalCharInFileNameOnWindows
public static String replaceIllegalCharInFileNameOnWindows(String path)
Replacing illegal characters in file name on Windows.
-
onWindows
public static boolean onWindows()
Are we running on Windows? (Rough estimate.)
-
concat
@NotNull public static <T> @NotNull Collection<? extends T> concat(@NotNull @NotNull Collection<? extends T> collection1, @NotNull @NotNull Collection<? extends T> collection2)
Returns a concatenation of two collections. (No trying to remove multiplicities.) Optimizing the operation if one of collections is empty. The return value should not be used for modifications!
-
-