Class CollectionUtils
- Since:
- 1.58
- Version:
- 1.3
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternA regular expression to match sensitive key names. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncoveringIntRanges(SortedSet<Integer> set, int maxRangeLength) Create integer ranges from a set of integers to produce a reduced set of ranges that cover all integers in the source set.static <K,V> Dictionary<K, V> dictionaryForMap(Map<K, V> map) Convert a map to a dictionary.static <C extends Collection<T>,T>
CfilteredSubset(C superSet, C subSet, Supplier<C> supplier) Get a filtered subset of a "super" collection.static BigDecimalgetMapBigDecimal(String key, Map<String, ?> map) Get a BigDecimal value out of a Map.static BigIntegergetMapBigInteger(String key, Map<String, ?> map) Get a BigDecimal value out of a Map.static DoublegetMapDouble(String key, Map<String, ?> map) Get a Double value out of a Mapstatic FloatgetMapFloat(String key, Map<String, ?> map) Get a Float value out of a Map.static IntegergetMapInteger(String key, Map<String, ?> map) Get an Integer value out of a Map.static LonggetMapLong(String key, Map<String, ?> map) Get a Long value out of a Map.static ShortgetMapShort(String key, Map<String, ?> map) Get a Short value out of a Map.static StringgetMapString(String key, Map<String, ?> map) Get a String value out of a Map.static <K,V> Map<K, V> mapForDictionary(Dictionary<K, V> dict) Convert a dictionary to a map.sensitiveNamesToMask(Set<String> set) Extract a set of values from a set that have sensitive-sounding names.valuesMatching(Set<String> set, Pattern pattern) Extract a set of values from a set that match a regular expression.
-
Field Details
-
SENSITIVE_NAME_PATTERN
A regular expression to match sensitive key names.- Since:
- 1.2
- See Also:
-
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
coveringIntRanges
Create integer ranges from a set of integers to produce a reduced set of ranges that cover all integers in the source set.The resulting ranges are guaranteed to be equal to, or a super set of, the given source set. Thus the resulting ranges could include more values than the source set. The resulting ranges will not intersect any other but could be adjacent. They will also be ordered in ascending order.
This can be useful when you want to coalesce many discreet ranges into a smaller number of larger ranges, such as when querying a device for data in address registers, to reduce the number of overall requests required to read the complete set of registers. Instead of reading many small ranges of registers, fewer large ranges of registers can be requested. For example, if the a set
spassed to this method contained these ranges:- 0-1
- 3-5
- 20-28
- 404-406
- 412-418
then calling this method like
minimizeIntRanges(s, 32)would return a new set with these ranges:- 0-28
- 404-418
- Parameters:
set- the set of integers to reducemaxRangeLength- the maximum length of any combined range in the resulting set- Returns:
- a new set of ranges possibly combined, or
setif there are less than two ranges to start with, or null ifsetis null - Throws:
IllegalArgumentException- ifmaxRangeLengthis less than 1
-
mapForDictionary
Convert a dictionary to a map.This creates a new
Mapand copies all the key-value pairs from the givenDictionaryinto it.- Type Parameters:
K- the key typeV- the value type- Parameters:
dict- the dictionary to convert- Returns:
- the new map instance, or null if
dictis null
-
dictionaryForMap
Convert a map to a dictionary.This creates a new
Dictionaryand copies all the key-value pairs from the givenMapinto it.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map to convert- Returns:
- the new dictionary instance, or null if
mapis null
-
getMapString
Get a String value out of a Map.If the key exists but is not a String,
Object.toString()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found
- Since:
- 1.2
-
getMapShort
Get a Short value out of a Map.If the key exists, is not an Short, but is a Number,
Number.shortValue()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with Short
- Since:
- 1.2
-
getMapInteger
Get an Integer value out of a Map.If the key exists, is not an Integer, but is a Number,
Number.intValue()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with Integer
- Since:
- 1.2
-
getMapLong
Get a Long value out of a Map.If the key exists, is not a Long, but is a Number,
Number.longValue()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with Long
- Since:
- 1.2
-
getMapFloat
Get a Float value out of a Map.If the key exists, is not a Float, but is a Number,
Number.floatValue()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with Float
- Since:
- 1.2
-
getMapDouble
Get a Double value out of a MapIf the key exists, is not a Double, but is a Number,
Number.doubleValue()will be called on that object.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with Double
- Since:
- 1.2
-
getMapBigDecimal
Get a BigDecimal value out of a Map.If the key exists but is not a BigDecimal,
Object.toString()will be called on that object andBigDecimal(String)will be returned.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with BigDecimal
- Since:
- 1.2
-
getMapBigInteger
Get a BigDecimal value out of a Map.If the key exists but is not a BigDecimal,
Object.toString()will be called on that object andBigDecimal(String)will be returned.- Parameters:
key- the key of the object to getmap- the map to inspect, null is allowed- Returns:
- the value, or null if not found or not compatible with BigInteger
- Since:
- 1.2
-
sensitiveNamesToMask
Extract a set of values from a set that have sensitive-sounding names.The point of this method is to identify keys from a map that appear to have associated "sensitive" values, such as passwords, with the aim of then not printing those values somewhere, such as the application log.
This method calls
valuesMatching(Set, Pattern), passing in theSENSITIVE_NAME_PATTERNpattern.- Parameters:
set- the names to examine, null is allowed- Returns:
- the sensitive looking names, never null
- Since:
- 1.2
- See Also:
-
valuesMatching
Extract a set of values from a set that match a regular expression.- Parameters:
set- the names to examine, null is allowedpattern- the regular expression whose matches should be returned- Returns:
- the matching names, never null
- Since:
- 1.2
-
filteredSubset
public static <C extends Collection<T>,T> C filteredSubset(C superSet, C subSet, Supplier<C> supplier) Get a filtered subset of a "super" collection.- Type Parameters:
C- the collection typeT- the collection item type- Parameters:
superSet- the "super" collection that defines all possible collection item valuessubSet- the "sub" collection that defines a subset of possible collection item values, or a null or empty set for all valuessupplier- a supplier for a new result collection- Returns:
- a filtered sub-collection, or
superSetifsubSethas no values in common withsuperSetorsuperSetis null or empty - Since:
- 1.3
-