001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025/**
026 * A profiled type that has a probability. Profiled types are naturally sorted in descending order
027 * of their probabilities.
028 */
029public abstract class AbstractProfiledItem<T> implements Comparable<AbstractProfiledItem<?>> {
030
031    protected final T item;
032    protected final double probability;
033
034    public AbstractProfiledItem(T item, double probability) {
035        assert item != null;
036        assert probability >= 0.0D && probability <= 1.0D;
037        this.item = item;
038        this.probability = probability;
039    }
040
041    protected T getItem() {
042        return item;
043    }
044
045    /**
046     * Returns the estimated probability of {@link #getItem()}.
047     *
048     * @return double value &ge; 0.0 and &le; 1.0
049     */
050    public double getProbability() {
051        return probability;
052    }
053
054    @Override
055    public int compareTo(AbstractProfiledItem<?> o) {
056        if (getProbability() > o.getProbability()) {
057            return -1;
058        } else if (getProbability() < o.getProbability()) {
059            return 1;
060        }
061        return 0;
062    }
063
064    @Override
065    public int hashCode() {
066        final int prime = 31;
067        int result = 1;
068        long temp;
069        temp = Double.doubleToLongBits(probability);
070        result = prime * result + (int) (temp ^ (temp >>> 32));
071        result = prime * result + item.hashCode();
072        return result;
073    }
074
075    @Override
076    public boolean equals(Object obj) {
077        if (this == obj) {
078            return true;
079        }
080        if (obj == null) {
081            return false;
082        }
083        if (getClass() != obj.getClass()) {
084            return false;
085        }
086        AbstractProfiledItem<?> other = (AbstractProfiledItem<?>) obj;
087        if (Double.doubleToLongBits(probability) != Double.doubleToLongBits(other.probability)) {
088            return false;
089        }
090        return item.equals(other.item);
091    }
092
093    @Override
094    public abstract String toString();
095}