Class Distribution<O>
java.lang.Object
org.opentrafficsim.core.distributions.Distribution<O>
- Type Parameters:
 O- Type of the object returned by the draw method
- All Implemented Interfaces:
 java.io.Serializable,Generator<O>
public class Distribution<O> extends java.lang.Object implements Generator<O>, java.io.Serializable
Generic implementation of a set of objects that have a draw method with corresponding probabilities / frequencies.
 
 Copyright (c) 2013-2020 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
 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDistribution.FrequencyAndObject<O>Immutable storage for a frequency (or probability) plus a Generator. - 
Constructor Summary
Constructors Constructor Description Distribution(java.util.List<Distribution.FrequencyAndObject<O>> generators, StreamInterface stream)Construct a new Distribution.Distribution(StreamInterface stream)Construct a new Distribution with no generators. - 
Method Summary
Modifier and Type Method Description Distribution<O>add(int index, Distribution.FrequencyAndObject<O> generator)Insert a generator at the specified position in the internally stored list.Distribution<O>add(Distribution.FrequencyAndObject<O> generator)Append a generator to the internally stored list.Distribution<O>clear()Empty the internally stored list.Odraw()Generate the next object.booleanequals(java.lang.Object obj)Distribution.FrequencyAndObject<O>get(int index)Retrieve one of the internally stored generators.inthashCode()Distribution<O>modifyFrequency(int index, double frequency)Alter the frequency (or probability) of one of the stored generators.Distribution<O>remove(int index)Remove the generator at the specified position from the internally stored list.Distribution<O>set(int index, Distribution.FrequencyAndObject<O> generator)Replace the generator at the specified position.intsize()Report the number of generators.java.lang.StringtoString() 
- 
Constructor Details
- 
Distribution
public Distribution(java.util.List<Distribution.FrequencyAndObject<O>> generators, StreamInterface stream) throws ProbabilityExceptionConstruct 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
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 Details
- 
draw
Generate the next object.- Specified by:
 drawin interfaceGenerator<O>- Returns:
 - O; an object randomly selected from the stored collection
 - Throws:
 ProbabilityException- if the stored collection is empty
 - 
add
public final Distribution<O> add(Distribution.FrequencyAndObject<O> generator) throws ProbabilityExceptionAppend a generator to the internally stored list.- Parameters:
 generator- FrequencyAndObject<O>; the generator to add- Returns:
 - Distribution<O>; this
 - Throws:
 ProbabilityException- when frequency less than zero
 - 
add
public final Distribution<O> add(int index, Distribution.FrequencyAndObject<O> generator) throws ProbabilityExceptionInsert a generator at the specified position in the internally stored list.- Parameters:
 index- int; position to store the generatorgenerator- FrequencyAndObject<O>; the generator to add- Returns:
 - Distribution<O>; this
 - Throws:
 ProbabilityException- when frequency less than zero
 - 
remove
public final Distribution<O> remove(int index) throws java.lang.IndexOutOfBoundsException, ProbabilityExceptionRemove the generator at the specified position from the internally stored list.- Parameters:
 index- int; the position- Returns:
 - this
 - Throws:
 java.lang.IndexOutOfBoundsException- when index is < 0 or >= sizeProbabilityException- if the sum of the remaining probabilities or frequencies adds up to 0
 - 
set
public final Distribution<O> set(int index, Distribution.FrequencyAndObject<O> generator) throws ProbabilityExceptionReplace the generator at the specified position.- Parameters:
 index- int; the position of the generator that must be replacedgenerator- 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 ProbabilityExceptionAlter the frequency (or probability) of one of the stored generators.- Parameters:
 index- int; index of the stored generatorfrequency- double; new frequency (or probability)- Returns:
 - this
 - Throws:
 ProbabilityException- when the frequency (or probability) < 0, or when index is < 0 or >= size
 - 
clear
Empty the internally stored list.- Returns:
 - this
 
 - 
get
Retrieve one of the internally stored generators.- Parameters:
 index- int; the index of the FrequencyAndObject to retrieve- Returns:
 - FrequencyAndObject<O>; the generator stored at position index
 - Throws:
 ProbabilityException- when index < 0 or >= size()
 - 
size
public final int size()Report the number of generators.- Returns:
 - int; the number of generators
 
 - 
hashCode
public final int hashCode()- Overrides:
 hashCodein classjava.lang.Object
 - 
equals
public final boolean equals(java.lang.Object obj)- Overrides:
 equalsin classjava.lang.Object
 - 
toString
public final java.lang.String toString()- Overrides:
 toStringin classjava.lang.Object
 
 -