Class MiscUtil


  • public class MiscUtil
    extends Object
    Author:
    semancik
    • Constructor Detail

      • MiscUtil

        public MiscUtil()
    • Method Detail

      • 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)
      • 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.
      • and

        public static Boolean and​(Boolean... operands)
        n-ary and that ignores null values.
      • equals

        public static boolean equals​(Object a,
                                     Object b)
      • 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 Long millis)
      • 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.
      • 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.

      • isAllNull

        public static boolean isAllNull​(Collection<?> collection)
      • getValueWithClass

        public static String getValueWithClass​(Object object)
      • getClass

        public static String getClass​(Object object)
      • isNoValue

        public static boolean isNoValue​(Collection<?> collection)
      • hasNoValue

        public static boolean hasNoValue​(Collection<?> collection)
      • cloneMap

        public static <K,​V> Map<K,​V> cloneMap​(Map<K,​V> orig)
        Shallow clone
      • contains

        public static <T> boolean contains​(T element,
                                           T[] array)
      • stripHtmlMarkup

        public static String stripHtmlMarkup​(String htmlString)
      • 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
      • hexToUtf8String

        public static String hexToUtf8String​(String hex)
      • 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)
      • getObjectName

        public static String getObjectName​(Object o)
      • getFirstNonNull

        @SafeVarargs
        public static <T> T getFirstNonNull​(T... values)
      • getFirstNonNullFromList

        public static <T> T getFirstNonNullFromList​(List<T> values)
      • getFirstNonNullString

        public static String getFirstNonNullString​(Object... values)
      • extractSingleton

        public static <T> T extractSingleton​(Collection<T> collection)
      • extractSingletonRequired

        @NotNull
        public static <T> T extractSingletonRequired​(Collection<T> collection)
      • 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 @NotNull String emptyIfNull​(String s)
      • emptyIfNull

        @NotNull
        public static <T> @NotNull List<T> emptyIfNull​(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)
      • nullIfEmpty

        public static String nullIfEmpty​(String s)
      • 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)
      • formatExceptionMessageWithCause

        public static String formatExceptionMessageWithCause​(Throwable t,
                                                             int indent)
      • throwExceptionAsUnchecked

        public static void throwExceptionAsUnchecked​(Throwable t)
      • unwrapTunnelledExceptionToRuntime

        public static <T> T unwrapTunnelledExceptionToRuntime​(TunnelException te)
      • filter

        public static <T> Set<T> filter​(Set<T> input,
                                        Predicate<? super T> predicate)
      • nonNullValues

        @NotNull
        public static <V> @NotNull Collection<V> nonNullValues​(@NotNull
                                                               @NotNull Collection<V> values)
      • toUrlUnchecked

        public static URL toUrlUnchecked​(URI uri)
      • 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/
      • paramsToMap

        public static <K,​V> Map<K,​V> paramsToMap​(Object[] params)
      • singletonOrEmptySet

        public static <T> Set<T> singletonOrEmptySet​(T value)
      • singletonOrEmptyList

        public static <T> List<T> singletonOrEmptyList​(T value)
      • 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
      • 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 returns null.
      • trimString

        @Nullable
        public static @Nullable String trimString​(@Nullable
                                                  @Nullable String value,
                                                  int size)
      • getSimpleClassName

        public static String getSimpleClassName​(Object o)
      • argNonNull

        public static <T> T argNonNull​(T value,
                                       Supplier<String> messageSupplier)
      • stateNonNull

        public static <T> T stateNonNull​(T value,
                                         Supplier<String> messageSupplier)
      • isSingleNull

        public static boolean isSingleNull​(Object[] values)
      • sleepWatchfully

        public static void sleepWatchfully​(long until,
                                           long increment,
                                           Supplier<Boolean> canRunSupplier)
        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

        public static <T> T getRandomItem​(List<T> collection)
      • toLong

        public static Long toLong​(Integer value)
      • toInteger

        public static Integer toInteger​(Long value)
      • checkCollectionImmutable

        public static void checkCollectionImmutable​(Collection<?> collection)
      • stateCheck

        public static void stateCheck​(boolean condition,
                                      String template,
                                      Object... arguments)
      • assertCheck

        public static void assertCheck​(boolean condition,
                                       String template,
                                       Object... arguments)
      • argCheck

        public static void argCheck​(boolean condition,
                                    String template,
                                    Object... arguments)
      • getClassWithMessage

        public static String getClassWithMessage​(Throwable e)
      • asListTreatingNull

        public static <T> List<T> asListTreatingNull​(T[] values)
        Like Arrays.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)
      • startsWith

        public static boolean startsWith​(List<?> wholeList,
                                         List<?> subList)
      • 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!