ClassHistogramElement.java revision 953:221a84ef44c0
1/*
2 * Copyright (c) 2010, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.ir.debug;
27
28import java.util.Comparator;
29
30/**
31 * Class histogram element for IR / Java object instrumentation
32 */
33public final class ClassHistogramElement {
34    /**
35     * Instance comparator
36     */
37    public static final Comparator<ClassHistogramElement> COMPARE_INSTANCES = new Comparator<ClassHistogramElement>() {
38        @Override
39        public int compare(final ClassHistogramElement o1, final ClassHistogramElement o2) {
40            return (int)Math.abs(o1.instances - o2.instances);
41        }
42    };
43
44    /**
45     * Bytes comparator
46     */
47    public static final Comparator<ClassHistogramElement> COMPARE_BYTES = new Comparator<ClassHistogramElement>() {
48        @Override
49        public int compare(final ClassHistogramElement o1, final ClassHistogramElement o2) {
50            return (int)Math.abs(o1.bytes - o2.bytes);
51        }
52    };
53
54    /**
55     * Classname comparator
56     */
57    public static final Comparator<ClassHistogramElement> COMPARE_CLASSNAMES = new Comparator<ClassHistogramElement>() {
58        @Override
59        public int compare(final ClassHistogramElement o1, final ClassHistogramElement o2) {
60            return o1.clazz.getCanonicalName().compareTo(o2.clazz.getCanonicalName());
61        }
62    };
63
64    private final Class<?> clazz;
65    private long instances;
66    private long bytes;
67
68    /**
69     * Constructor
70     * @param clazz class for which to construct histogram
71     */
72    public ClassHistogramElement(final Class<?> clazz) {
73        this.clazz = clazz;
74    }
75
76    /**
77     * Add an instance
78     * @param sizeInBytes byte count
79     */
80    public void addInstance(final long sizeInBytes) {
81        instances++;
82        this.bytes += sizeInBytes;
83    }
84
85    /**
86     * Get size in bytes
87     * @return size in bytes
88     */
89    public long getBytes() {
90        return bytes;
91    }
92
93    /**
94     * Get class
95     * @return class
96     */
97    public Class<?> getClazz() {
98        return clazz;
99    }
100
101    /**
102     * Get number of instances
103     * @return number of instances
104     */
105    public long getInstances() {
106        return instances;
107    }
108
109    @Override
110    public String toString() {
111        return "ClassHistogramElement[class=" + clazz.getCanonicalName() + ", instances=" + instances + ", bytes=" + bytes + "]";
112    }
113}
114