Record Class WeightedSort<T>
java.lang.Object
java.lang.Record
johnsmith.enchantingoverhauled.api.collection.WeightedSort<T>
- Type Parameters:
T- The type of element being weighted and sorted.- Record Components:
item- The underlying element instance.weight- The computed randomized weight value used for sorting.
- All Implemented Interfaces:
Comparable<WeightedSort<T>>
public record WeightedSort<T>(T item, double weight)
extends Record
implements Comparable<WeightedSort<T>>
A utility record and helper for performing weighted random sorting (skewed shuffling)
over a collection of elements.
Each element is paired with a randomized weight score and sorted in descending order, allowing higher-weight elements (such as common enchantments) to statistically appear earlier in the resulting list while still preserving randomness.
-
Constructor Summary
ConstructorsConstructorDescriptionWeightedSort(T item, double weight) Creates an instance of aWeightedSortrecord class. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(WeightedSort<T> other) Compares this weighted entry to another in descending order of weight so that higher weights are ordered first when sorted viaCollections.sort(List).final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.item()Returns the value of theitemrecord component.static <T> List<T> skewedShuffle(Collection<T> originalList, ToIntFunction<T> weightExtractor, net.minecraft.util.RandomSource random) Produces a new list containing the elements oforiginalListshuffled with a bias toward elements with higher integer weights.final StringtoString()Returns a string representation of this record class.doubleweight()Returns the value of theweightrecord component.
-
Constructor Details
-
Method Details
-
compareTo
Compares this weighted entry to another in descending order of weight so that higher weights are ordered first when sorted viaCollections.sort(List).- Specified by:
compareToin interfaceComparable<T>- Parameters:
other- The otherWeightedSortentry to compare against.- Returns:
- A negative integer, zero, or a positive integer if this entry's weight is greater than, equal to, or less than the other entry's weight.
-
skewedShuffle
public static <T> List<T> skewedShuffle(Collection<T> originalList, ToIntFunction<T> weightExtractor, net.minecraft.util.RandomSource random) Produces a new list containing the elements oforiginalListshuffled with a bias toward elements with higher integer weights.Elements with a weight less than or equal to
0are assignedDouble.NEGATIVE_INFINITYand placed at the end of the list. Elements with positive weights have their weight divided by a uniform random factor in(0.0, 1.0]and are sorted in descending order.- Type Parameters:
T- The element type in the collection.- Parameters:
originalList- The source collection of elements to shuffle.weightExtractor- A function that extracts the non-negative integer base weight for each element.random- TheRandomSourceused to perturb weights.- Returns:
- A new
Listcontaining the elements ordered by their randomized weights.
-
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. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
item
Returns the value of theitemrecord component.- Returns:
- the value of the
itemrecord component
-
weight
public double weight()Returns the value of theweightrecord component.- Returns:
- the value of the
weightrecord component
-