Package nl.vpro.util
Class Helper
java.lang.Object
nl.vpro.util.Helper
Simple helper methods.
- Author:
- arne, peter, [email protected]
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
L extends K, V, W extends V>
voidAdds 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>
static boolean
isAssignableFrom
(Object toTest, String name) static boolean
isAssignableFrom
(String toTest, String name) static boolean
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
static <T> T
nvl
(T... values) Returns the first non-null value.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.static <T> List<T>
removeNullEntries
(List<T> source) static <T extends Comparable<? super T>>
List<T>Determine the similar elements in both lists.spliceNumber
(Number number, int maxPartSize) Will splice the number in chunks no larger then maxPartSizespliceNumber
(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 Details
-
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:
-
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
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:
-
nvl
-
firstNonNull
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:
-
firstNonNull
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:
-
add
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, void addToSetMapV, W extends V> (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
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
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
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
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
Compare, taking null into account (null <).- Parameters:
o1
- firsto2
- second- Returns:
- 1,0,-1
-
toSingleton
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
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
- 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
- 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'
-
getClass
-
getAnnotatedProperty
-
removeNullEntries
-
appendIfNotEmpty
-
join
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
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
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
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
-