RuneHive-Game
Loading...
Searching...
No Matches
WeightedChance.java
Go to the documentation of this file.
1package com.runehive.util.chance;
2
3import java.util.Objects;
4
5/**
6 * An item with a common chance.
7 *
8 * @author Michael | Chex
9 */
10public 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
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
38 return (int) (getWeight() - o.getWeight());
39 }
40
41 @Override
42 public String toString() {
43 return "[Object: " + get() + ", " + "Weight: " + getWeight() + "]";
44 }
45}
double getWeight()
Gets the object's weight.
final T representation
The representation.
WeightedChance(double weight, T representation)
Represents a weighted object.
double getWeight()
Gets the object's weight.