Class WbsCollectionUtil

java.lang.Object
wbs.utils.util.WbsCollectionUtil

public final class WbsCollectionUtil extends Object
A variety of Collection utilities, including statistical selections.
  • Method Details

    • getRandomWeighted

      @NotNull public static <T, N extends Number> T getRandomWeighted(Map<T,N> weightedMap)
      Gets a weighted value from a set
      Parameters:
      weightedMap - A map of values to their weights
      Returns:
      A random value from the key set of the provided map, weighted by the value.
      Throws:
      IllegalArgumentException - Thrown if the collection is empty.
    • getRandom

      @NotNull public static <T> T getRandom(Collection<T> collection)
      Gets a random element of a collection, including unordered ones.
      Parameters:
      collection - The collection containing
      Returns:
      A random value from the collection.
      Throws:
      IllegalArgumentException - Thrown if the collection is empty.
    • pseudoRandomAvoidRepeats

      @NotNull public static <T, N extends Number> T pseudoRandomAvoidRepeats(Map<T,N> weightedMap, List<T> history, double repeatRatio)
      Choose a pseudo random element from the provided map of values to chances, ignoring elements contained in the history list provided.

      This method makes use of the getRandomWeighted(Map) method.

      Type Parameters:
      T - The type of element to be selected.
      N - The type of number used in the map to represent weights.
      Parameters:
      weightedMap - A non-empty map of values to their weights
      history - A list forming a subset of the keys in the weighted map, already chosen randomly previously, which may not be chosen during this call. The intent of this parameter is to be passed into this method repeatedly, to allow it to avoid repeats, and automatically resize to maintain a buffer to prevent repeats within a certain number of operations.
      repeatRatio - How much of the history list is to be used as a buffer, relative to the size of the map given. For example, setting this to 3 with a map size of 60 would prevent a given element being chosen within 20 calls to this method.
      Returns:
      The chosen element.
      Throws:
      RuntimeException - Thrown when it takes more than 1000 * the size of the given map attempts to find a non-history value.
    • pseudoRandomAvoidRepeats

      @NotNull public static <T, N extends Number> T pseudoRandomAvoidRepeats(Map<T,N> weightedMap, List<T> history)
      Overload of pseudoRandomAvoidRepeats(Map, List, double) with repeatRatio set to 2.
    • getAvoidRepeats

      @NotNull public static <T> T getAvoidRepeats(Supplier<T> supplier, int size, List<T> history, double repeatRatio)
      Choose a pseudo random element from the provided map of values to chances, ignoring elements contained in the history list provided.

      This method makes use of the getRandomWeighted(Map) method.

      Type Parameters:
      T - The type of element to be selected.
      Parameters:
      supplier - A generator that returns
      size - The amount of unique possibilities that may be produced by supplier, usually the size of the collection being returned from.
      history - A list forming a subset of the keys in the weighted map, already chosen randomly previously, which may not be chosen during this call. The intent of this parameter is to be passed into this method repeatedly, to allow it to avoid repeats, and automatically resize to maintain a buffer to prevent repeats within a certain number of operations.
      repeatRatio - How much of the history list is to be used as a buffer, relative to the size of the map given. For example, setting this to 3 with a map size of 60 would prevent a given element being chosen within 20 calls to this method.
      Returns:
      The chosen element.
      Throws:
      RuntimeException - Thrown when it takes more than 1000 * the size of the given map attempts to find a non-history value.