Class UtilMisc


  • public final class UtilMisc
    extends java.lang.Object
    UtilMisc - Misc Utility Functions
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <K> java.math.BigDecimal addToBigDecimalInMap​(java.util.Map<K,​java.lang.Object> theMap, K mapKey, java.math.BigDecimal addNumber)
      Assuming theMap not null; if null will throw a NullPointerException
      static <K> void addToDoubleInMap​(java.util.Map<K,​java.lang.Object> theMap, K key, java.lang.Double value)
      Adds value to the key entry in theMap, or creates a new one if not already there
      static <K,​V>
      void
      addToListInMap​(V element, java.util.Map<K,​java.lang.Object> theMap, K listKey)  
      static <K,​V>
      void
      addToSetInMap​(V element, java.util.Map<K,​java.util.Set<V>> theMap, K setKey)  
      static <K,​V>
      void
      addToSortedSetInMap​(V element, java.util.Map<K,​java.util.Set<V>> theMap, K setKey)  
      static java.util.List<java.util.Locale> availableLocales()
      Returns a List of available locales sorted by display name
      static <T> java.util.Set<T> collectionToSet​(java.util.Collection<T> c)  
      static java.lang.String collectionToString​(java.util.Collection<? extends java.lang.Object> values, java.lang.String delimiter)
      Generates a String from given values delimited by delimiter.
      static <T> int compare​(java.lang.Comparable<T> obj1, T obj2)  
      static <E> int compare​(java.util.List<E> obj1, java.util.List<E> obj2)  
      static void copyFile​(java.io.File sourceLocation, java.io.File targetLocation)  
      static java.util.Locale ensureLocale​(java.lang.Object localeObject)
      The input can be a String, Locale, or even null and a valid Locale will always be returned; if nothing else works, returns the default locale.
      static java.util.List<java.lang.String> getHostHeadersAllowed()
      List of domains or IP addresses to be checked to prevent Host Header Injection, no spaces after commas, no wildcard, can be extended of course...
      static <K,​V>
      java.util.List<V>
      getListFromMap​(java.util.Map<K,​java.lang.Object> outerMap, K key)
      Assuming outerMap not null; if null will throw a NullPointerException
      static <K,​IK,​V>
      java.util.Map<IK,​V>
      getMapFromMap​(java.util.Map<K,​java.lang.Object> outerMap, K key)
      Assuming outerMap not null; if null will throw a NullPointerException
      static int getViewLastIndex​(int listSize, int viewSize)  
      static <T extends java.lang.Throwable>
      T
      initCause​(T throwable, java.lang.Throwable cause)  
      static <V> void makeArrayListSerializable​(java.util.ArrayList<java.lang.Object> arrayList)
      This change an ArrayList to be Serializable by removing all entries that are not Serializable.
      static <T> java.util.List<T> makeListWritable​(java.util.Collection<? extends T> col)  
      static <V> void makeMapSerializable​(java.util.Map<java.lang.String,​V> map)
      This change a Map to be Serializable by removing all entries with values that are not Serializable.
      static <K,​V>
      java.util.Map<K,​V>
      makeMapWritable​(java.util.Map<K,​? extends V> map)  
      static java.util.Locale parseLocale​(java.lang.String localeString)
      Parse a locale string Locale object
      static <K,​V>
      java.lang.String
      printMap​(java.util.Map<? extends K,​? extends V> theMap)  
      static <T> T removeFirst​(java.util.List<T> lst)  
      static java.util.List<java.util.Map<java.lang.Object,​java.lang.Object>> sortMaps​(java.util.List<java.util.Map<java.lang.Object,​java.lang.Object>> listOfMaps, java.util.List<? extends java.lang.String> sortKeys)
      Sort a List of Maps by specified consistent keys.
      static java.util.Map<java.lang.String,​java.lang.String> splitPhoneNumber​(java.lang.String phoneNumber, Delegator delegator)  
      static void staticWait​(long timeout)
      Deprecated.
      use Thread.sleep()
      static double toDouble​(java.lang.Object obj)
      Converts an Object to a double.
      static java.lang.Double toDoubleObject​(java.lang.Object obj)
      Converts an Object to a Double.
      static int toInteger​(java.lang.Object obj)
      Converts an Object to an int.
      static java.lang.Integer toIntegerObject​(java.lang.Object obj)
      Converts an Object to an Integer.
      static <T> java.util.Iterator<T> toIterator​(java.util.Collection<T> col)
      Get an iterator from a collection, returning null if collection is null
      static <T> java.util.List<T> toList​(T... data)
      Creates a list from passed objects.
      static <T> java.util.List<T> toListArray​(T[] data)  
      static long toLong​(java.lang.Object obj)
      Converts an Object to a long.
      static java.lang.Long toLongObject​(java.lang.Object obj)
      Converts an Object to a Long.
      static <K,​V>
      java.util.Map<K,​V>
      toMap​(java.lang.Object... kvs)
      Creates a pseudo-literal map corresponding to key-values.
      static <K,​V>
      java.util.Map<K,​V>
      toMap​(java.util.function.Supplier<java.util.Map<K,​V>> constructor, java.lang.Object... kvs)
      Creates a pseudo-literal map corresponding to key-values.
      static <T> java.util.Set<T> toSet​(java.util.Collection<T> collection)  
      static <T> java.util.Set<T> toSet​(T... data)
      Create a set from the passed objects.
      static <T> java.util.Set<T> toSetArray​(T[] data)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • initCause

        public static <T extends java.lang.Throwable> T initCause​(T throwable,
                                                                  java.lang.Throwable cause)
      • compare

        public static <T> int compare​(java.lang.Comparable<T> obj1,
                                      T obj2)
      • compare

        public static <E> int compare​(java.util.List<E> obj1,
                                      java.util.List<E> obj2)
      • toIterator

        public static <T> java.util.Iterator<T> toIterator​(java.util.Collection<T> col)
        Get an iterator from a collection, returning null if collection is null
        Parameters:
        col - The collection to be turned in to an iterator
        Returns:
        The resulting Iterator
      • toMap

        public static <K,​V> java.util.Map<K,​V> toMap​(java.lang.Object... kvs)
        Creates a pseudo-literal map corresponding to key-values.
        Parameters:
        kvs - the key-value pairs
        Returns:
        the corresponding map.
        Throws:
        java.lang.IllegalArgumentException - when the key-value list is not even.
      • toMap

        public static <K,​V> java.util.Map<K,​V> toMap​(java.util.function.Supplier<java.util.Map<K,​V>> constructor,
                                                                 java.lang.Object... kvs)
        Creates a pseudo-literal map corresponding to key-values.
        Parameters:
        constructor - the constructor used to instantiate the map
        kvs - the key-value pairs
        Returns:
        the corresponding map.
        Throws:
        java.lang.IllegalArgumentException - when the key-value list is not even.
      • printMap

        public static <K,​V> java.lang.String printMap​(java.util.Map<? extends K,​? extends V> theMap)
      • makeListWritable

        public static <T> java.util.List<T> makeListWritable​(java.util.Collection<? extends T> col)
      • makeMapWritable

        public static <K,​V> java.util.Map<K,​V> makeMapWritable​(java.util.Map<K,​? extends V> map)
      • makeMapSerializable

        public static <V> void makeMapSerializable​(java.util.Map<java.lang.String,​V> map)
        This change a Map to be Serializable by removing all entries with values that are not Serializable.
        Type Parameters:
        V -
        Parameters:
        map -
      • makeArrayListSerializable

        public static <V> void makeArrayListSerializable​(java.util.ArrayList<java.lang.Object> arrayList)
        This change an ArrayList to be Serializable by removing all entries that are not Serializable.
        Parameters:
        arrayList -
      • sortMaps

        public static java.util.List<java.util.Map<java.lang.Object,​java.lang.Object>> sortMaps​(java.util.List<java.util.Map<java.lang.Object,​java.lang.Object>> listOfMaps,
                                                                                                      java.util.List<? extends java.lang.String> sortKeys)
        Sort a List of Maps by specified consistent keys.
        Parameters:
        listOfMaps - List of Map objects to sort.
        sortKeys - List of Map keys to sort by.
        Returns:
        a new List of sorted Maps.
      • getMapFromMap

        public static <K,​IK,​V> java.util.Map<IK,​V> getMapFromMap​(java.util.Map<K,​java.lang.Object> outerMap,
                                                                                   K key)
        Assuming outerMap not null; if null will throw a NullPointerException
      • getListFromMap

        public static <K,​V> java.util.List<V> getListFromMap​(java.util.Map<K,​java.lang.Object> outerMap,
                                                                   K key)
        Assuming outerMap not null; if null will throw a NullPointerException
      • addToBigDecimalInMap

        public static <K> java.math.BigDecimal addToBigDecimalInMap​(java.util.Map<K,​java.lang.Object> theMap,
                                                                    K mapKey,
                                                                    java.math.BigDecimal addNumber)
        Assuming theMap not null; if null will throw a NullPointerException
      • removeFirst

        public static <T> T removeFirst​(java.util.List<T> lst)
      • collectionToSet

        public static <T> java.util.Set<T> collectionToSet​(java.util.Collection<T> c)
      • collectionToString

        public static java.lang.String collectionToString​(java.util.Collection<? extends java.lang.Object> values,
                                                          java.lang.String delimiter)
        Generates a String from given values delimited by delimiter.
        Parameters:
        values -
        delimiter -
        Returns:
        String
      • toSet

        @SafeVarargs
        public static <T> java.util.Set<T> toSet​(T... data)
        Create a set from the passed objects.
        Parameters:
        data -
        Returns:
        theSet
      • toSet

        public static <T> java.util.Set<T> toSet​(java.util.Collection<T> collection)
      • toSetArray

        public static <T> java.util.Set<T> toSetArray​(T[] data)
      • toList

        @SafeVarargs
        public static <T> java.util.List<T> toList​(T... data)
        Creates a list from passed objects.
        Parameters:
        data -
        Returns:
        list
      • toListArray

        public static <T> java.util.List<T> toListArray​(T[] data)
      • addToListInMap

        public static <K,​V> void addToListInMap​(V element,
                                                      java.util.Map<K,​java.lang.Object> theMap,
                                                      K listKey)
      • addToSetInMap

        public static <K,​V> void addToSetInMap​(V element,
                                                     java.util.Map<K,​java.util.Set<V>> theMap,
                                                     K setKey)
      • addToSortedSetInMap

        public static <K,​V> void addToSortedSetInMap​(V element,
                                                           java.util.Map<K,​java.util.Set<V>> theMap,
                                                           K setKey)
      • toDouble

        public static double toDouble​(java.lang.Object obj)
        Converts an Object to a double. Returns zero if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        double value
      • toDoubleObject

        public static java.lang.Double toDoubleObject​(java.lang.Object obj)
        Converts an Object to a Double. Returns null if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        Double
      • toInteger

        public static int toInteger​(java.lang.Object obj)
        Converts an Object to an int. Returns zero if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        int value
      • toIntegerObject

        public static java.lang.Integer toIntegerObject​(java.lang.Object obj)
        Converts an Object to an Integer. Returns null if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        Integer
      • toLong

        public static long toLong​(java.lang.Object obj)
        Converts an Object to a long. Returns zero if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        long value
      • toLongObject

        public static java.lang.Long toLongObject​(java.lang.Object obj)
        Converts an Object to a Long. Returns null if conversion is not possible.
        Parameters:
        obj - Object to convert
        Returns:
        Long
      • addToDoubleInMap

        public static <K> void addToDoubleInMap​(java.util.Map<K,​java.lang.Object> theMap,
                                                K key,
                                                java.lang.Double value)
        Adds value to the key entry in theMap, or creates a new one if not already there
        Parameters:
        theMap -
        key -
        value -
      • parseLocale

        public static java.util.Locale parseLocale​(java.lang.String localeString)
        Parse a locale string Locale object
        Parameters:
        localeString - The locale string (en_US)
        Returns:
        Locale The new Locale object or null if no valid locale can be interpreted
      • ensureLocale

        public static java.util.Locale ensureLocale​(java.lang.Object localeObject)
        The input can be a String, Locale, or even null and a valid Locale will always be returned; if nothing else works, returns the default locale.
        Parameters:
        localeObject - An Object representing the locale
      • availableLocales

        public static java.util.List<java.util.Locale> availableLocales()
        Returns a List of available locales sorted by display name
      • getHostHeadersAllowed

        public static java.util.List<java.lang.String> getHostHeadersAllowed()
        List of domains or IP addresses to be checked to prevent Host Header Injection, no spaces after commas, no wildcard, can be extended of course...
        Returns:
        List of domains or IP addresses to be checked to prevent Host Header Injection,
      • staticWait

        @Deprecated
        public static void staticWait​(long timeout)
                               throws java.lang.InterruptedException
        Deprecated.
        use Thread.sleep()
        Throws:
        java.lang.InterruptedException
      • copyFile

        public static void copyFile​(java.io.File sourceLocation,
                                    java.io.File targetLocation)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • getViewLastIndex

        public static int getViewLastIndex​(int listSize,
                                           int viewSize)
      • splitPhoneNumber

        public static java.util.Map<java.lang.String,​java.lang.String> splitPhoneNumber​(java.lang.String phoneNumber,
                                                                                              Delegator delegator)