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

    Constructors
    Constructor
    Description
    WeightedSort(T item, double weight)
    Creates an instance of a WeightedSort record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares this weighted entry to another in descending order of weight so that higher weights are ordered first when sorted via Collections.sort(List).
    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 item record component.
    static <T> List<T>
    skewedShuffle(Collection<T> originalList, ToIntFunction<T> weightExtractor, net.minecraft.util.RandomSource random)
    Produces a new list containing the elements of originalList shuffled with a bias toward elements with higher integer weights.
    final String
    Returns a string representation of this record class.
    double
    Returns the value of the weight record component.

    Methods inherited from class java.lang.Object

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

    • WeightedSort

      public WeightedSort(T item, double weight)
      Creates an instance of a WeightedSort record class.
      Parameters:
      item - the value for the item record component
      weight - the value for the weight record component
  • Method Details

    • compareTo

      public int compareTo(WeightedSort<T> other)
      Compares this weighted entry to another in descending order of weight so that higher weights are ordered first when sorted via Collections.sort(List).
      Specified by:
      compareTo in interface Comparable<T>
      Parameters:
      other - The other WeightedSort entry 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 of originalList shuffled with a bias toward elements with higher integer weights.

      Elements with a weight less than or equal to 0 are assigned Double.NEGATIVE_INFINITY and 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 - The RandomSource used to perturb weights.
      Returns:
      A new List containing the elements ordered by their randomized weights.
    • 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      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.
    • item

      public T item()
      Returns the value of the item record component.
      Returns:
      the value of the item record component
    • weight

      public double weight()
      Returns the value of the weight record component.
      Returns:
      the value of the weight record component