-
Notifications
You must be signed in to change notification settings - Fork 6
/
Weighted.java
95 lines (88 loc) · 3.09 KB
/
Weighted.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gr.james.sampling;
/**
* Represents an item with a weight.
* <p>
* This class is immutable and is meant for use in weighted random sampling algorithms.
*
* @param <T> the object type
* @author Giorgos Stamatelatos
*/
class Weighted<T> implements Comparable<Weighted<T>> {
/**
* The object associated with this instance.
*/
public final T object;
/**
* The weight associated with the {@link #object}.
*/
public final double weight;
/**
* Construct a new {@link Weighted} from a given object and weight.
* <p>
* For performance reasons, no checks are performed on the inputs.
*
* @param object the object
* @param weight the weight of the object
*/
public Weighted(T object, double weight) {
this.object = object;
this.weight = weight;
}
/**
* Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer
* as this object is less than, equal to, or greater than the specified object.
* <p>
* The comparison is based on the {@link #weight} values of the two {@link Weighted} objects. If the weights are of
* the same value, the comparison is based on {@link System#identityHashCode(Object)}. This means that
* {@code a.equals(b)} will evaluate to {@code 0} if and only if {@code a == b}.
*
* @param o the object to be compared
* @return a negative integer, zero, or a positive integer as the object is less than, equal to, or greater than the
* specified object
* @throws NullPointerException if {@code o} is {@code null}
*/
@Override
public int compareTo(Weighted<T> o) {
final int c = Double.compare(weight, o.weight);
if (c == 0) {
assert (Integer.compare(System.identityHashCode(this), System.identityHashCode(o)) == 0) == (this.equals(o));
return Integer.compare(System.identityHashCode(this), System.identityHashCode(o));
} else {
assert !this.equals(o);
return c;
}
}
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The implementation delegates to an invocation of {@link Object#equals(Object)}.
*
* @param obj the reference object with which to compare
* @return {@code true} if this object is the same as the {@code obj} argument; {@code false} otherwise
*/
@Override
public boolean equals(Object obj) {
assert !super.equals(obj) || super.hashCode() == obj.hashCode();
return super.equals(obj);
}
/**
* Returns a hash code value for the object.
* <p>
* The implementation delegates to an invocation of {@link Object#hashCode()}.
*
* @return a hash code value for this object
*/
@Override
public int hashCode() {
return super.hashCode();
}
/**
* Returns a string representation of this object.
*
* @return a string representation of this object
*/
@Override
public String toString() {
return String.format("{%s, %f}", object, weight);
}
}