1/*
2 * Copyright (c) 2003, 2010, 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 sun.jvmstat.monitor;
27
28/**
29 * Provides a typesafe enumeration for describing units of measurement
30 * attribute for instrumentation objects.
31 *
32 * @author   Brian Doherty
33 */
34public class Units implements java.io.Serializable {
35
36    /* The enumeration values for this typesafe enumeration must be
37     * kept in synchronization with the Units enum in the perfData.hpp file
38     * in the HotSpot source base.
39     */
40
41    private static final int NUNITS=8;
42
43    private static Units[] map = new Units[NUNITS];
44
45    private final String name;
46    private final int value;
47
48    /**
49     * An Invalid Units value.
50     */
51    public static final Units INVALID = new Units("Invalid", 0);
52
53    /**
54     * Units attribute representing unit-less quantities.
55     */
56    public static final Units NONE = new Units("None", 1);
57
58    /**
59     * Units attribute representing Bytes.
60     */
61    public static final Units BYTES = new Units("Bytes", 2);
62
63    /**
64     * Units attribute representing Ticks.
65     */
66    public static final Units TICKS = new Units("Ticks", 3);
67
68    /**
69     * Units attribute representing a count of events.
70     */
71    public static final Units EVENTS = new Units("Events", 4);
72
73    /**
74     * Units attribute representing String data. Although not really
75     * a unit of measure, this Units value serves to distinguish String
76     * instrumentation objects from instrumentation objects of other types.
77     */
78    public static final Units STRING = new Units("String", 5);
79
80    /**
81     * Units attribute representing Hertz (frequency).
82     */
83    public static final Units HERTZ = new Units("Hertz", 6);
84
85    /**
86     * Returns a string describing this Unit of measurement attribute
87     *
88     * @return String - a descriptive string for this enum.
89     */
90    public String toString() {
91        return name;
92    }
93
94    /**
95     * Returns the integer representation of this Units attribute
96     *
97     * @return int - an integer representation of this Units attribute.
98     */
99    public int intValue() {
100        return value;
101    }
102
103    /**
104     * Maps an integer value to its corresponding Units attribute.
105     * If the integer value does not have a corresponding Units enum
106     * value, then {@link Units#INVALID} is returned.
107     *
108     * @param value an integer representation of counter Units
109     * @return Units - the Units object for the given <code>value</code>
110     *                 or {@link Units#INVALID} if out of range.
111     */
112    public static Units toUnits(int value) {
113
114        if (value < 0 || value >= map.length || map[value] == null) {
115            return INVALID;
116        }
117
118        return map[value];
119    }
120
121    private Units(String name, int value) {
122        this.name = name;
123        this.value = value;
124        map[value] = this;
125    }
126
127    private static final long serialVersionUID = 6992337162326171013L;
128}
129