43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package jdk.vm.ci.meta;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* A profiled type that has a probability. Profiled types are naturally sorted in descending order
|
|
27 |
* of their probabilities.
|
|
28 |
*/
|
|
29 |
public abstract class AbstractProfiledItem<T> implements Comparable<AbstractProfiledItem<?>> {
|
|
30 |
|
|
31 |
protected final T item;
|
|
32 |
protected final double probability;
|
|
33 |
|
|
34 |
public AbstractProfiledItem(T item, double probability) {
|
|
35 |
assert item != null;
|
|
36 |
assert probability >= 0.0D && probability <= 1.0D;
|
|
37 |
this.item = item;
|
|
38 |
this.probability = probability;
|
|
39 |
}
|
|
40 |
|
|
41 |
protected T getItem() {
|
|
42 |
return item;
|
|
43 |
}
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Returns the estimated probability of {@link #getItem()}.
|
|
47 |
*
|
|
48 |
* @return double value ≥ 0.0 and ≤ 1.0
|
|
49 |
*/
|
|
50 |
public double getProbability() {
|
|
51 |
return probability;
|
|
52 |
}
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Returns -1 if the {@linkplain #getProbability() probability} of this item is greater than
|
|
56 |
* {@code o}'s probability, 0 if there are equal otherwise 1.
|
|
57 |
*/
|
|
58 |
@Override
|
|
59 |
public int compareTo(AbstractProfiledItem<?> o) {
|
|
60 |
// Need to swap the order of operands so that higher probabilities are sorted first
|
|
61 |
return Double.compare(o.getProbability(), getProbability());
|
|
62 |
}
|
|
63 |
|
|
64 |
@Override
|
|
65 |
public int hashCode() {
|
|
66 |
final int prime = 31;
|
|
67 |
int result = 1;
|
|
68 |
long temp;
|
|
69 |
temp = Double.doubleToLongBits(probability);
|
|
70 |
result = prime * result + (int) (temp ^ (temp >>> 32));
|
|
71 |
result = prime * result + item.hashCode();
|
|
72 |
return result;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public boolean equals(Object obj) {
|
|
77 |
if (this == obj) {
|
|
78 |
return true;
|
|
79 |
}
|
|
80 |
if (obj == null) {
|
|
81 |
return false;
|
|
82 |
}
|
|
83 |
if (getClass() != obj.getClass()) {
|
|
84 |
return false;
|
|
85 |
}
|
|
86 |
AbstractProfiledItem<?> other = (AbstractProfiledItem<?>) obj;
|
|
87 |
if (Double.doubleToLongBits(probability) != Double.doubleToLongBits(other.probability)) {
|
|
88 |
return false;
|
|
89 |
}
|
|
90 |
return item.equals(other.item);
|
|
91 |
}
|
|
92 |
|
|
93 |
@Override
|
|
94 |
public abstract String toString();
|
|
95 |
}
|