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>
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 Summary
ConstructorsConstructorDescriptionObjectDistribution(List<FrequencyAndObject<O>> objects, StreamInterface stream) Construct a new ObjectDistribution.ObjectDistribution(StreamInterface stream) Construct a new ObjectDistribution with no objects. -
Method Summary
Modifier and TypeMethodDescriptionadd(int index, FrequencyAndObject<O> object) Insert an object at the specified position in the internally stored list.add(FrequencyAndObject<O> object) Append an object to the internally stored list.clear()Empty the internally stored list.booleanget()get(int index) Retrieve one of the internally stored objects.inthashCode()modifyFrequency(int index, double frequency) Alter the frequency (or probability) of one of the stored objects.remove(int index) Remove the object at the specified position from the internally stored list.set(int index, FrequencyAndObject<O> object) Replace the object at the specified position.intsize()Report the number of objects.toString()
-
Constructor Details
-
ObjectDistribution
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 nullIllegalArgumentException- when a frequency (or probability) is negative
-
ObjectDistribution
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
-
add
Append an object to the internally stored list.- Parameters:
object- the object to add- Returns:
- this
- Throws:
NullPointerException- when object is null
-
add
Insert an object at the specified position in the internally stored list.- Parameters:
index- position to store the objectobject- the object to add- Returns:
- this
- Throws:
IndexOutOfBoundsException- when index is < 0 or ≥ sizeNullPointerException- when object is null
-
remove
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
Replace the object at the specified position.- Parameters:
index- the position of the object that must be replacedobject- the new object and the frequency (or probability)- Returns:
- this
- Throws:
IndexOutOfBoundsException- when index is < 0 or ≥ sizeNullPointerException- when object is null
-
modifyFrequency
Alter the frequency (or probability) of one of the stored objects.- Parameters:
index- index of the stored objectfrequency- new frequency (or probability)- Returns:
- this
- Throws:
IndexOutOfBoundsException- when index is < 0 or ≥ sizeIllegalArgumentException- when the frequency (or probability) < 0
-
clear
Empty the internally stored list.- Returns:
- this
-
get
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() -
equals
-
toString
-