Class CollectionUtils

java.lang.Object
net.solarnetwork.util.CollectionUtils

public final class CollectionUtils extends Object
Utility methods for dealing with collections.
Since:
1.58
Version:
1.3
Author:
matt
  • Field Details

    • SENSITIVE_NAME_PATTERN

      public static final Pattern SENSITIVE_NAME_PATTERN
      A regular expression to match sensitive key names.
      Since:
      1.2
      See Also:
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • coveringIntRanges

      public static List<IntRange> coveringIntRanges(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.

      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 s passed 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 reduce
      maxRangeLength - the maximum length of any combined range in the resulting set
      Returns:
      a new set of ranges possibly combined, or set if there are less than two ranges to start with, or null if set is null
      Throws:
      IllegalArgumentException - if maxRangeLength is less than 1
    • mapForDictionary

      public static <K, V> Map<K,V> mapForDictionary(Dictionary<K,V> dict)
      Convert a dictionary to a map.

      This creates a new Map and copies all the key-value pairs from the given Dictionary into it.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      dict - the dictionary to convert
      Returns:
      the new map instance, or null if dict is null
    • dictionaryForMap

      public static <K, V> Dictionary<K,V> dictionaryForMap(Map<K,V> map)
      Convert a map to a dictionary.

      This creates a new Dictionary and copies all the key-value pairs from the given Map into it.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map - the map to convert
      Returns:
      the new dictionary instance, or null if map is null
    • getMapString

      public static String getMapString(String key, Map<String,?> map)
      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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found
      Since:
      1.2
    • getMapShort

      public static Short getMapShort(String key, Map<String,?> map)
      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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with Short
      Since:
      1.2
    • getMapInteger

      public static Integer getMapInteger(String key, Map<String,?> map)
      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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with Integer
      Since:
      1.2
    • getMapLong

      public static Long getMapLong(String key, Map<String,?> map)
      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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with Long
      Since:
      1.2
    • getMapFloat

      public static Float getMapFloat(String key, Map<String,?> map)
      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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with Float
      Since:
      1.2
    • getMapDouble

      public static Double getMapDouble(String key, Map<String,?> map)
      Get a Double value out of a Map

      If 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 get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with Double
      Since:
      1.2
    • getMapBigDecimal

      public static BigDecimal getMapBigDecimal(String key, Map<String,?> map)
      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 and BigDecimal(String) will be returned.

      Parameters:
      key - the key of the object to get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with BigDecimal
      Since:
      1.2
    • getMapBigInteger

      public static BigInteger getMapBigInteger(String key, Map<String,?> map)
      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 and BigDecimal(String) will be returned.

      Parameters:
      key - the key of the object to get
      map - the map to inspect, null is allowed
      Returns:
      the value, or null if not found or not compatible with BigInteger
      Since:
      1.2
    • sensitiveNamesToMask

      public static Set<String> sensitiveNamesToMask(Set<String> set)
      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 the SENSITIVE_NAME_PATTERN pattern.

      Parameters:
      set - the names to examine, null is allowed
      Returns:
      the sensitive looking names, never null
      Since:
      1.2
      See Also:
    • valuesMatching

      public static Set<String> valuesMatching(Set<String> set, Pattern pattern)
      Extract a set of values from a set that match a regular expression.
      Parameters:
      set - the names to examine, null is allowed
      pattern - 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 type
      T - the collection item type
      Parameters:
      superSet - the "super" collection that defines all possible collection item values
      subSet - the "sub" collection that defines a subset of possible collection item values, or a null or empty set for all values
      supplier - a supplier for a new result collection
      Returns:
      a filtered sub-collection, or superSet if subSet has no values in common with superSet or superSet is null or empty
      Since:
      1.3