Class ArrayUtils

java.lang.Object
net.solarnetwork.util.ArrayUtils

public final class ArrayUtils extends Object
Utilities for dealing with arrays.
Since:
1.42
Version:
1.2
Author:
matt
  • Method Details

    • arrayWithLength

      public static <T> T[] arrayWithLength(T[] array, int count, Class<T> itemClass, org.springframework.beans.factory.ObjectFactory<? extends T> factory)
      Adjust an array to a specific length, filling in any new elements with newly objects.

      This method can shorten or lengthen an array of objects. After adjusting the array length, any null element in the array will be initialized to an object returned from factory, or if factory is not provided a new instance of itemClass via Class.newInstance(). The ObjectFactory.getObject() method (or class constructor if no factory provided) will be called for each null array index.

      Note that if a size adjustment is made, a new array instance is returned from this method, with elements copied from array where appropriate. If no size adjustment is necessary, then array is returned directly.

      Type Parameters:
      T - the array item type
      Parameters:
      array - the source array, or null
      count - the desired length of the array; if less than zero will be treated as zero
      itemClass - the class of array items
      factory - a factory to create new array items, or null to create itemClass instances directly
      Returns:
      a copy of array with the adjusted length, or array if no adjustment was necessary
    • filterByEnabledDisabled

      public static String[] filterByEnabledDisabled(String[] list, String[] enabled, String[] disabled)
      Filter a set of strings based on enabled/disabled patterns.

      The enabled patterns are applied first, followed by disabled. Both arguments are optional. The patterns are treated in a case-insensitive manner, using Matcher.find() to find partial matches.

      Parameters:
      list - the list to filter
      enabled - an optional list of regular expressions to limit the result to
      disabled - an optional list of regular expressions to exclude from the results
      Returns:
      the filtered list of protocols
      Since:
      1.1
    • isOnlyNull

      public static boolean isOnlyNull(Object[] array)
      Test if an array has only null elements or is itself null or empty.

      This method will perform a linear search for the first non-null element.

      Parameters:
      array - the array to test
      Returns:
      true if array is null, empty, or has only null elements
      Since:
      1.2