RuneHive-Game
Loading...
Searching...
No Matches
WeightedChance.java
Go to the documentation of this file.
1
package
com.runehive.util.chance;
2
3
import
java.util.Objects;
4
5
/**
6
* An item with a common chance.
7
*
8
* @author Michael | Chex
9
*/
10
public
class
WeightedChance
<T>
implements
WeightedObject
<T> {
11
12
/** The representation. */
13
private
final
T
representation
;
14
15
/** The weight type. */
16
private
double
weight
;
17
18
public
WeightedChance
(
double
weight
, T
representation
) {
19
if
(
weight
<= 0) {
20
throw
new
IllegalArgumentException(
"The weight of an item must be larger than zero!"
);
21
}
22
this.representation = Objects.requireNonNull(
representation
);
23
this.weight =
weight
;
24
}
25
26
@Override
27
public
double
getWeight
() {
28
return
weight
;
29
}
30
31
@Override
32
public
T
get
() {
33
return
representation
;
34
}
35
36
@Override
37
public
int
compareTo
(
WeightedObject<T>
o) {
38
return
(
int
) (
getWeight
() - o.
getWeight
());
39
}
40
41
@Override
42
public
String
toString
() {
43
return
"[Object: "
+
get
() +
", "
+
"Weight: "
+
getWeight
() +
"]"
;
44
}
45
}
com.runehive.util.chance.WeightedChance.weight
double weight
The weight type.
Definition
WeightedChance.java:16
com.runehive.util.chance.WeightedChance.getWeight
double getWeight()
Gets the object's weight.
Definition
WeightedChance.java:27
com.runehive.util.chance.WeightedChance.representation
final T representation
The representation.
Definition
WeightedChance.java:13
com.runehive.util.chance.WeightedChance.toString
String toString()
Definition
WeightedChance.java:42
com.runehive.util.chance.WeightedChance.compareTo
int compareTo(WeightedObject< T > o)
Definition
WeightedChance.java:37
com.runehive.util.chance.WeightedChance.WeightedChance
WeightedChance(double weight, T representation)
Definition
WeightedChance.java:18
com.runehive.util.chance.WeightedObject
Represents a weighted object.
Definition
WeightedObject.java:8
com.runehive.util.chance.WeightedObject.getWeight
double getWeight()
Gets the object's weight.