Record Class WeightedRandomSelector

java.lang.Object
java.lang.Record
net.datafaker.service.WeightedRandomSelector

public record WeightedRandomSelector(Random random) extends Record
A utility class for selecting a random element from a list based on assigned weights.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance of a WeightedRandomSelector record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the random record component.
    <T> T
    Returns a weighted random element from the given list, where each element is represented as a Map containing a weight and the corresponding value.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • WeightedRandomSelector

      public WeightedRandomSelector(Random random)
      Creates an instance of a WeightedRandomSelector record class.
      Parameters:
      random - the value for the random record component
  • Method Details

    • select

      public <T> T select(List<Map<String,Object>> items)
      Returns a weighted random element from the given list, where each element is represented as a Map containing a weight and the corresponding value.

      Type Parameters:
      T - The type of the element to be selected from the list. The value associated with the weight can be of any type.
      Parameters:
      items - A list of maps, where each map contains: - weight: A Double representing the weight of the element, influencing its selection probability. - value: The actual element of type T to be randomly selected based on its weight.
      Returns:
      A randomly selected element based on its weight.
      Throws:
      IllegalArgumentException - if: - the list is null or empty, - any item in the list is null or empty, - the item does not contain 'weight' or 'value' keys, - any weight is null, non-positive, NaN or infinite, - any values in the list are not unique or null, - the sum of weights exceeds Double.MAX_VALUE.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • random

      public Random random()
      Returns the value of the random record component.
      Returns:
      the value of the random record component