Package nl.vpro.util

Class Helper

    • Method Detail

      • withDefault

        public static <T> T withDefault​(T value,
                                        T def)
        Gives a default in case the value is null.
        Type Parameters:
        T - the type of the value
        Parameters:
        value - a value
        def - the default
        Returns:
        value when value is not null, def otherwise
        See Also:
        nvl(Object[])
      • withDefault

        public static int withDefault​(int value,
                                      int def)
        Gives a default in case the value is -1.
        Parameters:
        value - a value
        def - the default
        Returns:
        the value if it is not -1, def otherwise
      • nvl

        @SafeVarargs
        public static <T> T nvl​(T... values)
        Returns the first non-null value. This method is also known as coalesce.
        Type Parameters:
        T - the type of the value
        Parameters:
        values - the values
        Returns:
        the first non-null value in the arguments, or null when values is null or when all given values in it are all null
        See Also:
        withDefault(Object, Object)
      • firstNonNull

        public static <T> T firstNonNull​(List<T> values)
        Returns the first non-null value. This method is also known as coalesce.
        Parameters:
        values - the values
        Returns:
        the first non-null value in the arguments, or null when values is null or when all given values in it are all null
        See Also:
        withDefault(Object, Object)
      • firstNonNull

        public static <T> T firstNonNull​(Set<T> values)
        Returns the first non-null value. This method is also known as coalesce.
        Parameters:
        values - the values
        Returns:
        the first non-null value in the arguments, or null when values is null or when all given values in it are all null
        See Also:
        withDefault(Object, Object)
      • add

        public static <K,​L extends K,​V,​W extends V> void add​(Map<K,​List<V>> listMap,
                                                                               L key,
                                                                               W value)
        Adds a value to a map of lists. A value list is created when there is no entry in the map for the given key.
        Type Parameters:
        K - key type in the map
        L - actual used key type
        V - list type in the map
        W - actual used list type
        Parameters:
        listMap - the map of lists (not null)
        key - the key (may be null when the map supports null keys)
        value - the value to put in the list
      • addToSetMap

        public static <K,​L extends K,​V,​W extends V> void addToSetMap​(Map<K,​Set<V>> setMap,
                                                                                       L key,
                                                                                       W value)
        Adds a value to a map of sets. A value set is created when there is no entry in the map for the given key.
        Type Parameters:
        K - key type in the map
        L - actual used key type
        V - set type in the map
        W - actual used set type
        Parameters:
        setMap - the map of sets (not null)
        key - the key (may be null when the map supports null keys)
        value - the value to put in the set
      • putIfNotNull

        public static <K,​V> void putIfNotNull​(Map<K,​V> map,
                                                    K key,
                                                    V value)
        Puts a value in a map when the value is not null.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the map (not null)
        key - the key, can be null when the map supports it
        value - the value or null to do nothing
      • isNumber

        public static boolean isNumber​(String text)
        Determines whether some string is a number. No range check is performed.
        Parameters:
        text - the string of which must be determined if it's a number
        Returns:
        true if text is a number, false otherwise
      • numberCast

        public static <N extends Number> N numberCast​(Number value,
                                                      Class<N> targetType)
        Converts a number to another number, potentially rounding to fit the target type.

        Does all standard java types except Atomic* and Mutable*.

        Type Parameters:
        N - the target number type
        Parameters:
        value - the number to convert
        targetType - the return type (not null)
        Returns:
        value cast to type N or null when value is null. Note that the information may be discarded when the value is incompatible with the target type.
      • arrayContains

        @SafeVarargs
        public static <T> boolean arrayContains​(T element,
                                                T... array)
        Tries and find an element in an array. equals is used for comparisons. A linear search is performed - if the input array is sorted, it would be wiser to use Arrays.binarySearch(Object[], Object).
        Type Parameters:
        T - the array type
        Parameters:
        element - the element to find
        array - the array to search in
        Returns:
        true if the array contains the element, false otherwise
      • compare

        public static int compare​(Comparable o1,
                                  Comparable o2)
        Compare, taking null into account (null <).
        Parameters:
        o1 - first
        o2 - second
        Returns:
        1,0,-1
      • toSingleton

        public static <T> T toSingleton​(List<T> list)
        Convert a singleton list into the singleton element.
        Type Parameters:
        T - The type of the list
        Parameters:
        list - the list
        Returns:
        the singleton in the list, or null if the list is null or the size of the list is zero.
        Throws:
        IllegalStateException - when more than one element is found in the list.
      • toSingleton

        public static <T> T toSingleton​(Set<T> set)
        Convert a singleton set into the singleton element.
        Type Parameters:
        T - The type of the set
        Parameters:
        set - the set
        Returns:
        the singleton in the set, or null if the set is null or the size of the set is zero.
        Throws:
        IllegalStateException - when more than one element is found in the set.
      • isAssignableFrom

        public static boolean isAssignableFrom​(Object toTest,
                                               String name)
        Parameters:
        toTest - The object to test
        name - The FQN of the class to test againts. The class should be in the classpath
        Returns:
        true if the 'name' is assignable from 'toTest'
      • isAssignableFrom

        public static boolean isAssignableFrom​(String toTest,
                                               String name)
        Parameters:
        toTest - The FQN object to test. The class should be in the classpath
        name - The FQN of the class to test against. The class should be in the classpath
        Returns:
        true if the 'name' is assignable from 'toTest'
      • getClass

        public static <T> Class<T> getClass​(String name)
      • getAnnotatedProperty

        public static <W extends AnnotationString getAnnotatedProperty​(Class<?> clazz,
                                                                         Class<W> annClazz)
      • removeNullEntries

        public static <T> List<T> removeNullEntries​(List<T> source)
      • join

        public static <T> String join​(Collection<T> list,
                                      String sep)
        joins all items in the given list using String.valueOf on each item seperated by the sep String
        Parameters:
        list - The list to join
        sep - The separator String
        Returns:
        the result
      • sim

        public static <T extends Comparable<? super T>> List<T> sim​(List<T> a,
                                                                    List<T> b)
        Determine the similar elements in both lists. Result will be sorted.
        Parameters:
        a - List a
        b - List b
        Returns:
        The similar elements for both lists
      • spliceNumber

        public static List<String> spliceNumber​(Number number,
                                                int maxPartSize)
        Will splice the number in chunks no larger then maxPartSize
        Parameters:
        number - The number to splice
        maxPartSize - The maximum size of a part in the path
        Returns:
        A list of the spliced id
      • spliceNumber

        public static List<String> spliceNumber​(Number number,
                                                int minPartSize,
                                                int maxPartSize)
        Will splice the number in chunks no larger then maxPartSize
        Parameters:
        number - The number to splice
        minPartSize - The minimal size of a part, smaller will be skipped
        maxPartSize - The maximum size of a part in the path
        Returns:
        A list of the spliced id