Class ObjectDistribution<O>

java.lang.Object
org.opentrafficsim.core.distributions.ObjectDistribution<O>
Type Parameters:
O - Type of the object returned by the draw method
All Implemented Interfaces:
Supplier<O>

public class ObjectDistribution<O> extends Object implements Supplier<O>
Generic implementation of a set of objects that have a draw method with corresponding probabilities / frequencies.

Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Peter Knoppers
  • Constructor Details

    • ObjectDistribution

      public ObjectDistribution(List<FrequencyAndObject<O>> objects, StreamInterface stream)
      Construct a new ObjectDistribution.
      Parameters:
      objects - the objects and their frequencies (or probabilities)
      stream - source for randomness
      Throws:
      NullPointerException - when objects is null or stream is null
      IllegalArgumentException - when a frequency (or probability) is negative
    • ObjectDistribution

      public ObjectDistribution(StreamInterface stream)
      Construct a new ObjectDistribution with no objects.
      Parameters:
      stream - source for randomness
      Throws:
      NullPointerException - when objects is null or stream is null
  • Method Details

    • get

      public O get()
      Specified by:
      get in interface Supplier<O>
    • add

      public ObjectDistribution<O> add(FrequencyAndObject<O> object)
      Append an object to the internally stored list.
      Parameters:
      object - the object to add
      Returns:
      this
      Throws:
      NullPointerException - when object is null
    • add

      public ObjectDistribution<O> add(int index, FrequencyAndObject<O> object)
      Insert an object at the specified position in the internally stored list.
      Parameters:
      index - position to store the object
      object - the object to add
      Returns:
      this
      Throws:
      IndexOutOfBoundsException - when index is < 0 or ≥ size
      NullPointerException - when object is null
    • remove

      public ObjectDistribution<O> remove(int index)
      Remove the object at the specified position from the internally stored list.
      Parameters:
      index - the position
      Returns:
      this
      Throws:
      IndexOutOfBoundsException - when index is < 0 or ≥ size
    • set

      public ObjectDistribution<O> set(int index, FrequencyAndObject<O> object)
      Replace the object at the specified position.
      Parameters:
      index - the position of the object that must be replaced
      object - the new object and the frequency (or probability)
      Returns:
      this
      Throws:
      IndexOutOfBoundsException - when index is < 0 or ≥ size
      NullPointerException - when object is null
    • modifyFrequency

      public ObjectDistribution<O> modifyFrequency(int index, double frequency)
      Alter the frequency (or probability) of one of the stored objects.
      Parameters:
      index - index of the stored object
      frequency - new frequency (or probability)
      Returns:
      this
      Throws:
      IndexOutOfBoundsException - when index is < 0 or ≥ size
      IllegalArgumentException - when the frequency (or probability) < 0
    • clear

      public ObjectDistribution<O> clear()
      Empty the internally stored list.
      Returns:
      this
    • get

      public FrequencyAndObject<O> get(int index)
      Retrieve one of the internally stored objects.
      Parameters:
      index - the index of the FrequencyAndObject to retrieve
      Returns:
      the object stored at position index
      Throws:
      IndexOutOfBoundsException - when index < 0 or ≥ size()
    • size

      public int size()
      Report the number of objects.
      Returns:
      the number of objects
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object