Package net.datafaker.service
Record Class WeightedRandomSelector
java.lang.Object
java.lang.Record
net.datafaker.service.WeightedRandomSelector
A utility class for selecting a random element from a list based on assigned weights.
-
Constructor Summary
ConstructorsConstructorDescriptionWeightedRandomSelector(Random random) Creates an instance of aWeightedRandomSelectorrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.random()Returns the value of therandomrecord component.<T> TReturns a weighted random element from the given list, where each element is represented as a Map containing a weight and the corresponding value.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
WeightedRandomSelector
Creates an instance of aWeightedRandomSelectorrecord class.- Parameters:
random- the value for therandomrecord component
-
-
Method Details
-
select
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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
random
Returns the value of therandomrecord component.- Returns:
- the value of the
randomrecord component
-