Package com.evolveum.midpoint.util
Class MiscUtil
java.lang.Object
com.evolveum.midpoint.util.MiscUtil
- Author:
- semancik
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
addAllIfNotPresent
(List<T> receivingList, List<T> supplyingList) static <T> void
addIfNotPresent
(List<T> receivingList, T supplyingElement) static Boolean
n-ary and that ignores null values.static void
static <T> T
argNonNull
(T value, String template, Object... arguments) static <T> T
argNonNull
(T value, Supplier<String> messageSupplier) static Date
asDate
(int year, int month, int date, int hrs, int min, int sec) static Date
Converts an XMLGregorianCalendar to an instance of java.util.Datestatic @Nullable Instant
static @Nullable Instant
asInstant
(@Nullable XMLGregorianCalendar xgc) static <T> List<T>
Deprecated.static <T> @NotNull List<T>
asListExceptForNull
(T value) Separate method for efficiencystatic <T> @NotNull List<? extends T>
asListExceptForNull
(T... values) Fixed version ofasListTreatingNull(Object[])
that returns "extends T" list, and filters out all non-null values.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 Long
static void
assertCheck
(boolean condition, String template, Object... arguments) static <T> T
assertNonNull
(T value, String template, Object... arguments) static @Nullable XMLGregorianCalendar
asXMLGregorianCalendar
(@Nullable Long timeInMillis) Returns null for null input, but also for value of 0L.static @Nullable XMLGregorianCalendar
asXMLGregorianCalendar
(@Nullable Instant instant) static @Nullable XMLGregorianCalendar
asXMLGregorianCalendar
(@Nullable Date date) Converts a java.util.Date into an instance of XMLGregorianCalendarstatic @NotNull String
bytesToHex
(@org.jetbrains.annotations.NotNull byte[] bytes) static @Nullable String
bytesToHexPreview
(@org.jetbrains.annotations.Nullable byte[] bytes) Returns first 8 bytes from provided byte array as hexadecimal and adds length information.static @Nullable String
bytesToHexPreview
(@org.jetbrains.annotations.Nullable byte[] bytes, int previewLen) Returns a couple of bytes from provided byte array as hexadecimal and adds length information.static <T> void
cartesian
(List<? extends Collection<T>> valuesForDimensions, Processor<List<T>> tupleProcessor) We have n dimensions (D1...Dn), each containing a number of values.static <T> T
castSafely
(Object value, Class<T> expectedClass) static <T> T
castSafely
(Object value, Class<T> expectedClass, Object errorCtx) static void
checkCollectionImmutable
(Collection<?> collection) static <K,
V> Map<K, V> Shallow clonestatic Integer
compareNullLast
(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 String
concat
(Collection<String> stringCollection) static void
configCheck
(boolean condition, String template, Object... arguments) static <T> T
configNonNull
(T value, String template, Object... arguments) static <T> T
configNonNull
(T value, Supplier<String> messageSupplier) static <T> boolean
contains
(T element, T[] array) static void
copyDirectory
(File src, File dst) Copy a directory and its contents.static void
static <T> Collection<T>
createCollection
(T... items) static <T extends Throwable>
TcreateSame
(T original, String message) static Class<?>
determineCommonAncestor
(Collection<Class<?>> classes) static @NotNull String
static <T> @NotNull Collection<T>
emptyIfNull
(Collection<T> collection) static <T> @NotNull List<T>
emptyIfNull
(List<T> list) static <K,
V> @NotNull Map<K, V> emptyIfNull
(Map<K, V> map) 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 Integer
enumOrdinal
(@Nullable Enum<?> enumValue) Returns ordinal value from nullable enum or returnsnull
.static boolean
static <T> T
extractSingleton
(Collection<T> collection) static <T,
E extends Throwable>
TextractSingleton
(Collection<T> collection, Predicate<T> predicate, Supplier<E> exceptionSupplier) static <T,
E extends Throwable>
TextractSingleton
(Collection<T> collection, Supplier<E> exceptionSupplier) static <T> T
extractSingletonRequired
(@Nullable Collection<T> collection, @Nullable Predicate<T> predicate) static <T,
E extends Throwable>
TextractSingletonRequired
(@Nullable Collection<T> collection, @Nullable Predicate<T> predicate, @NotNull Supplier<E> multiExceptionSupplier, @NotNull Supplier<E> noneExceptionSupplier) static <T> T
extractSingletonRequired
(Collection<T> collection) 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>
static <V> V
find
(Collection<V> values, V value, @NotNull EqualsChecker<V> equalsChecker) static <V> V
findWithComparator
(Collection<V> values, V value, @NotNull Comparator<V> comparator) static <T> T
static String
fixFileName
(String originalName) Crude attempt at replacing all illegal chars with underscores.static String
static String
static String
formatExceptionMessageWithCause
(Throwable t, int indent) static String
static String
static String
static String
getDiagInfo
(Object o, int maxLength) static String
getDiagInfo
(Collection<?> objects, int maxItems, int maxLengthPerObject) static Object
static XMLGregorianCalendar
getEarliestTimeIgnoringNull
(Collection<XMLGregorianCalendar> realValues) static <T> T
getFirstNonNull
(T... values) static <T> T
getFirstNonNullFromList
(List<T> values) static <T> T
getFirstNonNullRequired
(T... values) static String
getFirstNonNullString
(Object... values) static String
static <T> T
getRandomItem
(List<T> collection) static String
static <T> T
getSingleValue
(Collection<T> values, T defaultValue, String contextDescription) static <T> Collection<T>
getValuesFromDisplayableValues
(Collection<? extends DisplayableValue<T>> disps) static String
getValueWithClass
(Object object) static <T> boolean
hasDuplicates
(Collection<T> collection) Returns true if the collection contains at least one pair of equals elements.static boolean
hasNoValue
(Collection<?> collection) static byte[]
hexToBytes
(String hex) static String
hexToUtf8String
(String hex) instancesOf
(Class<E> cls) static boolean
isAllNull
(Collection<?> collection) static boolean
isBetween
(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end) static boolean
isCollectionOf
(Object object, @NotNull Class<?> memberClass) static boolean
isNoValue
(Collection<?> collection) static boolean
isSingleNull
(Object[] values) static <T> List<T>
join
(Collection<T> a, Collection<T> b) static <T> T
static Integer
static Long
static Integer
static Long
static <V> @NotNull Collection<V>
nonNullValues
(@NotNull Collection<V> values) static boolean
nullableCollectionsEqual
(Collection<?> c1, Collection<?> c2) static String
static String
static double
static int
static long
static BigDecimal
or0
(BigDecimal value) static <T> T
AsObjects.requireNonNullElseGet(Object, Supplier)
but does not require the supplier to provide non-null value.static <K,
V> Map<K, V> paramsToMap
(Object[] params) static @NotNull String
Correctly quotes a string for use in diagnostic messages.static String
static String
readZipFile
(File file, Charset charset) static <T,
E extends Throwable>
TrequireNonNull
(T value, MiscUtil.ExceptionSupplier<E> exceptionSupplier) static <T> T
requireNonNull
(T value, String template, Object... arguments) static <T> T
requireNonNull
(T value, Supplier<String> messageSupplier) static Class<?>
resolvePrimitiveIfNecessary
(Class<?> aClass) Resolves primitive type like `int` to wrapper, like `Integer`.static <T> T
runChecked
(CheckedFunction<Producer<T>, T> function, CheckedProducer<T> checkedProducer) static void
schemaCheck
(boolean condition, String template, Object... arguments) static <T> List<T>
singletonOrEmptyList
(T value) static <T> Set<T>
singletonOrEmptySet
(T value) static void
sleepCatchingInterruptedException
(long delay) Sleeps for specified time.static void
sleepNonInterruptibly
(long delay) Sleeps for specified time.static void
sleepWatchfully
(long until, long increment, Supplier<Boolean> canRunSupplier) Sleeps watching "can run" flag.splitLines
(String string) static boolean
startsWith
(List<?> wholeList, List<?> subList) static void
stateCheck
(boolean condition, String template, Object... arguments) static <C extends Collection<?>>
CstateNonEmpty
(C collection, String template, Object... arguments) static <T> T
stateNonNull
(T value, String template, Object... arguments) static <T> T
stateNonNull
(T value, Supplier<String> messageSupplier) static <T> @NotNull Stream<T>
streamOf
(Collection<T> collection) static String
stripHtmlMarkup
(String htmlString) static String
takeThreadDump
(@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 <T extends Throwable>
voidthrowException
(Throwable exception) static void
static Integer
static Long
static String
static URL
toUrlUnchecked
(URI uri) static @Nullable String
trimString
(@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> boolean
unorderedArrayEquals
(T[] a, T[] b) static <T> boolean
unorderedArrayEquals
(T[] a, T[] b, EqualsChecker<T> equalsChecker) Only zero vs non-zero value of comparator is important.static <T> boolean
unorderedCollectionEquals
(Collection<? extends T> a, Collection<? extends T> b) static <A,
B> boolean unorderedCollectionEquals
(Collection<A> a, Collection<B> b, HeteroEqualsChecker<A, B> comparator) Only zero vs non-zero value of comparator is important.static <T> boolean
unorderedCollectionEqualsWithComparator
(Collection<T> a, Collection<T> b, Comparator<T> comparator) Only zero vs non-zero value of comparator is important.static <T> int
unorderedCollectionHashcode
(Collection<T> collection, Predicate<T> filter) static <T> void
static <T> void
static void
writeZipFile
(File file, String entryName, String content, Charset charset)
-
Constructor Details
-
MiscUtil
public MiscUtil()
-
-
Method Details
-
union
-
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) -
unorderedCollectionEqualsWithComparator
public static <T> boolean unorderedCollectionEqualsWithComparator(Collection<T> a, Collection<T> b, Comparator<T> comparator) Only zero vs non-zero value of comparator is important. Prefer otherunorderedCollectionEquals
whenever possible. If you are provided with comparator for good reason, you may use this. If you create comparator for this method, you probably should createEqualsChecker
instead. -
unorderedCollectionEquals
public static <A,B> boolean unorderedCollectionEquals(Collection<A> a, Collection<B> b, HeteroEqualsChecker<A, B> comparator) Only zero vs non-zero value of comparator is important. -
unorderedArrayEquals
public static <T> boolean unorderedArrayEquals(T[] a, T[] b) -
unorderedArrayEquals
Only zero vs non-zero value of comparator is important. -
unorderedCollectionHashcode
-
readFile
- Throws:
IOException
-
copyFile
- Throws:
IOException
-
copyDirectory
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
-
and
n-ary and that ignores null values. -
equals
-
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
-
asInstant
-
asDate
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.
-
asMillis
-
asDate
-
cartesian
public static <T> void cartesian(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
-
isAllNull
-
getValueWithClass
-
getDiagInfoLazy
-
getDiagInfo
@Experimental public static String getDiagInfo(Collection<?> objects, int maxItems, int maxLengthPerObject) -
getDiagInfo
-
getDiagInfo
-
q
Correctly quotes a string for use in diagnostic messages. TODO maybe we should somehow merge this withgetDiagInfo(Object)
method -
getClass
-
isNoValue
-
hasNoValue
-
cloneMap
Shallow clone -
toString
-
splitLines
-
isBetween
public static boolean isBetween(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end) -
contains
public static <T> boolean contains(T element, T[] array) -
stripHtmlMarkup
-
getValuesFromDisplayableValues
public static <T> Collection<T> getValuesFromDisplayableValues(Collection<? extends DisplayableValue<T>> disps) -
bytesToHex
@NotNull public static @NotNull String bytesToHex(@NotNull @org.jetbrains.annotations.NotNull byte[] bytes) -
hexToBytes
-
bytesToHexPreview
@Nullable public static @Nullable String bytesToHexPreview(@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. -
bytesToHexPreview
@Nullable public static @Nullable String bytesToHexPreview(@Nullable @org.jetbrains.annotations.Nullable byte[] bytes, int previewLen) Returns a 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
-
hexToUtf8String
-
addAllIfNotPresent
-
addIfNotPresent
-
nullableCollectionsEqual
-
getObjectName
-
compareNullLast
-
orElseGet
AsObjects.requireNonNullElseGet(Object, Supplier)
but does not require the supplier to provide non-null value. -
getFirstNonNull
-
getFirstNonNullRequired
-
getFirstNonNullFromList
-
getFirstNonNullString
-
first
-
extractSingleton
-
extractSingleton
public static <T,E extends Throwable> T extractSingleton(Collection<T> collection, Supplier<E> exceptionSupplier) throws E - Throws:
E extends Throwable
-
extractSingleton
public static <T,E extends Throwable> T extractSingleton(Collection<T> collection, Predicate<T> predicate, Supplier<E> exceptionSupplier) throws E - 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 - Throws:
E extends Throwable
-
extractSingletonRequired
@NotNull public static <T,E extends Throwable> T extractSingletonRequired(@Nullable @Nullable Collection<T> collection, @Nullable @Nullable Predicate<T> predicate, @NotNull @NotNull Supplier<E> multiExceptionSupplier, @NotNull @NotNull Supplier<E> noneExceptionSupplier) throws E - Throws:
E extends Throwable
-
extractSingletonRequired
-
extractSingletonRequired
@NotNull public static <T> T extractSingletonRequired(@Nullable @Nullable Collection<T> collection, @Nullable @Nullable Predicate<T> predicate) -
getSingleValue
-
isCollectionOf
-
instancesOf
-
last
-
emptyIfNull
-
emptyIfNull
-
emptyIfNull
-
emptyIfNull
-
streamOf
-
nullIfEmpty
-
nullIfBlank
-
hasDuplicates
Returns true if the collection contains at least one pair of equals elements. -
formatExceptionMessageWithCause
-
formatExceptionMessageWithCause
-
formatExceptionMessage
-
throwException
- Throws:
T extends Throwable
-
throwExceptionAsUnchecked
-
runChecked
public static <T> T runChecked(CheckedFunction<Producer<T>, T> function, CheckedProducer<T> checkedProducer) throws CommonException- Throws:
CommonException
-
unwrapTunnelledException
- Throws:
CommonException
-
unwrapTunnelledExceptionToRuntime
-
filter
-
filter
-
nonNullValues
@NotNull public static <V> @NotNull Collection<V> nonNullValues(@NotNull @NotNull Collection<V> values) -
toUrlUnchecked
-
join
-
takeThreadDump
Thanks for this code go to https://crunchify.com/how-to-generate-java-thread-dump-programmatically/ -
paramsToMap
-
writeZipFile
public static void writeZipFile(File file, String entryName, String content, Charset charset) throws IOException - Throws:
IOException
-
readZipFile
- Throws:
IOException
-
singletonOrEmptySet
-
singletonOrEmptyList
-
castSafely
- Throws:
SchemaException
-
castSafely
public static <T> T castSafely(Object value, Class<T> expectedClass, Object errorCtx) throws SchemaException - Throws:
SchemaException
-
determineCommonAncestor
-
throwAsSame
@Experimental public static <T extends Throwable> void throwAsSame(Throwable original, String message) throws T 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
-
getEarliestTimeIgnoringNull
public static XMLGregorianCalendar getEarliestTimeIgnoringNull(Collection<XMLGregorianCalendar> realValues) -
findWithComparator
public static <V> V findWithComparator(Collection<V> values, V value, @NotNull @NotNull Comparator<V> comparator) -
find
public static <V> V find(Collection<V> values, V value, @NotNull @NotNull EqualsChecker<V> equalsChecker) -
enumFromOrdinal
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
Returns ordinal value from nullable enum or returnsnull
. -
trimString
-
getSimpleClassName
-
requireNonNull
public static <T> T requireNonNull(T value, Supplier<String> messageSupplier) throws SchemaException - Throws:
SchemaException
-
requireNonNull
public static <T> T requireNonNull(T value, String template, Object... arguments) throws SchemaException - Throws:
SchemaException
-
configNonNull
public static <T> T configNonNull(T value, Supplier<String> messageSupplier) throws ConfigurationException - Throws:
ConfigurationException
-
configNonNull
public static <T> T configNonNull(T value, String template, Object... arguments) throws ConfigurationException - Throws:
ConfigurationException
-
argNonNull
-
argNonNull
-
stateNonNull
-
stateNonNull
-
assertNonNull
-
stateNonEmpty
public static <C extends Collection<?>> C stateNonEmpty(C collection, String template, Object... arguments) -
isSingleNull
-
sleepWatchfully
Sleeps watching "can run" flag.InterruptedException
is caught (causing checking can run flag) but not propagated. -
sleepCatchingInterruptedException
public static void sleepCatchingInterruptedException(long delay) Sleeps for specified time.InterruptedException
is caught (causing immediate exit) but not propagated. -
sleepNonInterruptibly
public static void sleepNonInterruptibly(long delay) Sleeps for specified time.InterruptedException
is 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
-
toLong
-
toInteger
-
resolvePrimitiveIfNecessary
Resolves primitive type like `int` to wrapper, like `Integer`. -
requireNonNull
public static <T,E extends Throwable> T requireNonNull(T value, MiscUtil.ExceptionSupplier<E> exceptionSupplier) throws E - Throws:
E extends Throwable
-
checkCollectionImmutable
-
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
-
stateCheck
-
assertCheck
-
argCheck
-
getClassWithMessage
-
asListTreatingNull
LikeArrays.asList(Object[])
but if there's a single null value at input, creates an empty list. -
asListExceptForNull
@Experimental @SafeVarargs @NotNull public static <T> @NotNull List<? extends T> asListExceptForNull(T... values) Fixed version ofasListTreatingNull(Object[])
that returns "extends T" list, and filters out all non-null values. -
asListExceptForNull
Deprecated. -
asListExceptForNull
Separate method for efficiency -
or0
-
or0
-
or0
-
or0
-
min
-
min
-
max
-
max
-
startsWith
-
fixFileName
Crude attempt at replacing all illegal chars with underscores. Maybe too strict. -
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!
-