Class Distribution<O>

  • Type Parameters:
    O - Type of the object returned by the draw method
    All Implemented Interfaces:
    Serializable, Generator<O>

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

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

    Version:
    $Revision$, $LastChangedDate$, by $Author$, initial version Mar 1, 2016
    Author:
    Peter Knoppers
    See Also:
    Serialized Form
    • Constructor Detail

      • Distribution

        public Distribution​(List<Distribution.FrequencyAndObject<O>> generators,
                            StreamInterface stream)
                     throws ProbabilityException
        Construct a new Distribution.
        Parameters:
        generators - List<FrequencyAndObject<O>>; the generators and their frequencies (or probabilities)
        stream - StreamInterface; source for randomness
        Throws:
        ProbabilityException - when a frequency (or probability) is negative, or when generators is null or stream is null
      • Distribution

        public Distribution​(StreamInterface stream)
                     throws ProbabilityException
        Construct a new Distribution with no generators.
        Parameters:
        stream - StreamInterface; source for randomness
        Throws:
        ProbabilityException - when a frequency (or probability) is negative, or when generators is null or stream is null
    • Method Detail

      • set

        public final Distribution<O> set​(int index,
                                         Distribution.FrequencyAndObject<O> generator)
                                  throws ProbabilityException
        Replace the generator at the specified position.
        Parameters:
        index - int; the position of the generator that must be replaced
        generator - FrequencyAndObject<O>; the new generator and the frequency (or probability)
        Returns:
        this
        Throws:
        ProbabilityException - when the frequency (or probability) < 0, or when index is < 0 or >= size
      • modifyFrequency

        public final Distribution<O> modifyFrequency​(int index,
                                                     double frequency)
                                              throws ProbabilityException
        Alter the frequency (or probability) of one of the stored generators.
        Parameters:
        index - int; index of the stored generator
        frequency - double; new frequency (or probability)
        Returns:
        this
        Throws:
        ProbabilityException - when the frequency (or probability) < 0, or when index is < 0 or >= size
      • clear

        public final Distribution<O> clear()
        Empty the internally stored list.
        Returns:
        this
      • size

        public final int size()
        Report the number of generators.
        Returns:
        int; the number of generators
      • hashCode

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

        public final boolean equals​(Object obj)
        Overrides:
        equals in class Object