RuneHive-Game
Loading...
Searching...
No Matches
WeightedCollection.java
Go to the documentation of this file.
1
package
com.runehive.game.world.entity.mob.npc.drop;
2
3
import
java.util.NavigableMap;
4
import
java.util.Random;
5
import
java.util.TreeMap;
6
7
public
class
WeightedCollection
<E> {
8
private
final
NavigableMap<Double, E>
map
=
new
TreeMap<>();
9
private
final
Random
random
;
10
private
double
total
= 0;
11
12
public
double
getTotal
() {
13
return
total
;
14
}
15
16
public
WeightedCollection
() {
17
this
(
new
Random());
18
}
19
20
public
WeightedCollection
(Random
random
) {
21
this.random =
random
;
22
}
23
24
public
WeightedCollection<E>
add
(
double
weight, E result) {
25
if
(weight <= 0)
return
this
;
26
total
+= weight;
27
map
.put(
total
, result);
28
return
this
;
29
}
30
31
public
E
next
() {
32
double
value =
random
.nextDouble() *
total
;
33
return
map
.higherEntry(value).getValue();
34
}
35
}
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.next
E next()
Definition
WeightedCollection.java:31
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.WeightedCollection
WeightedCollection()
Definition
WeightedCollection.java:16
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.getTotal
double getTotal()
Definition
WeightedCollection.java:12
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.random
final Random random
Definition
WeightedCollection.java:9
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.map
final NavigableMap< Double, E > map
Definition
WeightedCollection.java:8
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.add
WeightedCollection< E > add(double weight, E result)
Definition
WeightedCollection.java:24
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.WeightedCollection
WeightedCollection(Random random)
Definition
WeightedCollection.java:20
com.runehive.game.world.entity.mob.npc.drop.WeightedCollection.total
double total
Definition
WeightedCollection.java:10