Class WeightedBag<T>

java.lang.Object
io.helidon.grpc.core.WeightedBag<T>
Type Parameters:
T - the type of elements in the bag
All Implemented Interfaces:
Iterable<T>

public class WeightedBag<T> extends Object implements Iterable<T>
A bag of values ordered by weight. All weights must be greater or equal to 0.

The higher the weight the higher the priority. For cases where the weight is the same, elements are returned in the order that they were added to the bag.

See Also:
  • Method Details

    • create

      public static <T> WeightedBag<T> create()
      Create a new WeightedBag where elements added with no weight will be given a default weight of 0.
      Type Parameters:
      T - the type of elements in the bag
      Returns:
      a new WeightedBag where elements added with no weight will be given Weighted.DEFAULT_WEIGHT
    • create

      public static <T> WeightedBag<T> create(double defaultWeight)
      Create a new WeightedBag where elements added with no weight will be given a default weight of 0.
      Type Parameters:
      T - the type of elements in the bag
      Parameters:
      defaultWeight - default weight for elements
      Returns:
      a new WeightedBag where elements added with no weight will be given Weighted.DEFAULT_WEIGHT
    • isEmpty

      public boolean isEmpty()
      Check if bag is empty.
      Returns:
      outcome of test
    • readOnly

      public WeightedBag<T> readOnly()
      Obtain an immutable copy of this WeightedBag.
      Returns:
      an immutable copy of this WeightedBag
    • merge

      public void merge(WeightedBag<? extends T> bag)
      Merge a WeightedBag into this WeightedBag.
      Parameters:
      bag - the bag to merge
    • copyMe

      public WeightedBag<T> copyMe()
      Obtain a copy of this WeightedBag.
      Returns:
      a copy of this WeightedBag
    • addAll

      public void addAll(Iterable<? extends T> values)
      Add elements to the bag.

      If the element's class is annotated with the Weight annotation then that value will be used to determine weight otherwise the default weight value will be used.

      Parameters:
      values - the elements to add
    • addAll

      public void addAll(Iterable<? extends T> values, double weight)
      Add elements to the bag with a given weight.
      Parameters:
      values - the elements to add
      weight - the weight to assign to the elements
    • add

      public void add(T value)
      Add an element to the bag.

      If the element's class is annotated with the Weight annotation then that value will be used to determine weight otherwise the default weight value will be used.

      Parameters:
      value - the element to add
    • add

      public void add(T value, double weight)
      Add an element to the bag with a specific weight.
      Parameters:
      value - the element to add
      weight - the weight of the element
    • stream

      public Stream<T> stream()
      Obtain the contents of this WeightedBag as an ordered Stream.
      Returns:
      the contents of this WeightedBag as an ordered Stream
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>