Package nl.vpro.util
Class Helper
- java.lang.Object
-
- nl.vpro.util.Helper
-
public class Helper extends Object
Simple helper methods.- Author:
- arne, peter, [email protected]
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,L extends K,V,W extends V>
voidadd(Map<K,List<V>> listMap, L key, W value)
Adds a value to a map of lists.static <K,L extends K,V,W extends V>
voidaddToSetMap(Map<K,Set<V>> setMap, L key, W value)
Adds a value to a map of sets.static StringBuilder
appendIfNotEmpty(StringBuilder sb, String s)
static <T> boolean
arrayContains(T element, T... array)
Tries and find an element in an array.static int
compare(Comparable o1, Comparable o2)
Compare, taking null into account (null <).static <T> T
firstNonNull(List<T> values)
Returns the first non-null value.static <T> T
firstNonNull(Set<T> values)
Returns the first non-null value.static <W extends Annotation>
StringgetAnnotatedProperty(Class<?> clazz, Class<W> annClazz)
static <T> Class<T>
getClass(String name)
static boolean
isAssignableFrom(Object toTest, String name)
static boolean
isAssignableFrom(String toTest, String name)
static boolean
isNumber(String text)
Determines whether some string is a number.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 Stringstatic <N extends Number>
NnumberCast(Number value, Class<N> targetType)
Converts a number to another number, potentially rounding to fit the target type.static String
nvl(String... values)
static <T> T
nvl(T... values)
Returns the first non-null value.static <K,V>
voidputIfNotNull(Map<K,V> map, K key, V value)
Puts a value in a map when the value is not null.static <T> List<T>
removeNullEntries(List<T> source)
static <T extends Comparable<? super T>>
List<T>sim(List<T> a, List<T> b)
Determine the similar elements in both lists.static List<String>
spliceNumber(Number number, int maxPartSize)
Will splice the number in chunks no larger then maxPartSizestatic List<String>
spliceNumber(Number number, int minPartSize, int maxPartSize)
Will splice the number in chunks no larger then maxPartSizestatic <T> T
toSingleton(List<T> list)
Convert a singleton list into the singleton element.static <T> T
toSingleton(Set<T> set)
Convert a singleton set into the singleton element.static int
withDefault(int value, int def)
Gives a default in case the value is -1.static <T> T
withDefault(T value, T def)
Gives a default in case the value is null.
-
-
-
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 valuedef
- 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 valuedef
- 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 mapL
- actual used key typeV
- list type in the mapW
- 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 mapL
- actual used key typeV
- set type in the mapW
- 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 typeV
- the value type- Parameters:
map
- the map (not null)key
- the key, can be null when the map supports itvalue
- 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
iftext
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 converttargetType
- 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 useArrays.binarySearch(Object[], Object)
.- Type Parameters:
T
- the array type- Parameters:
element
- the element to findarray
- 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
- firsto2
- 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 isnull
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 isnull
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 testname
- 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 classpathname
- The FQN of the class to test against. The class should be in the classpath- Returns:
- true if the 'name' is assignable from 'toTest'
-
getAnnotatedProperty
public static <W extends Annotation> String getAnnotatedProperty(Class<?> clazz, Class<W> annClazz)
-
appendIfNotEmpty
public static StringBuilder appendIfNotEmpty(StringBuilder sb, String s)
-
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 joinsep
- 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 ab
- 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 splicemaxPartSize
- 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 spliceminPartSize
- The minimal size of a part, smaller will be skippedmaxPartSize
- The maximum size of a part in the path- Returns:
- A list of the spliced id
-
-