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 com.oracle.graal.debug;
024
025import java.util.*;
026
027/**
028 * Facility for recording value frequencies.
029 */
030public interface DebugHistogram {
031
032    /**
033     * Gets the name specified when this objected was {@linkplain Debug#createHistogram(String)
034     * created}.
035     */
036    String getName();
037
038    /**
039     * Increments the count for a given value.
040     */
041    void add(Object value);
042
043    void add(Object value, long count);
044
045    /**
046     * A value and a frequency. The ordering imposed by {@link #compareTo(CountedValue)} places
047     * values with higher frequencies first.
048     */
049    public class CountedValue implements Comparable<CountedValue> {
050
051        private long count;
052        private final Object value;
053
054        public CountedValue(long count, Object value) {
055            this.count = count;
056            this.value = value;
057        }
058
059        public int compareTo(CountedValue o) {
060            if (count < o.count) {
061                return 1;
062            } else if (count > o.count) {
063                return -1;
064            }
065            return 0;
066        }
067
068        @Override
069        public String toString() {
070            return count + " -> " + value;
071        }
072
073        public void inc() {
074            count++;
075        }
076
077        public void add(long n) {
078            count += n;
079        }
080
081        public long getCount() {
082            return count;
083        }
084
085        public Object getValue() {
086            return value;
087        }
088    }
089
090    /**
091     * Gets a list of the counted values, sorted in descending order of frequency.
092     */
093    List<CountedValue> getValues();
094
095    /**
096     * Interface for a service that can render a visualization of a histogram.
097     */
098    public interface Printer {
099
100        void print(DebugHistogram histogram);
101    }
102}