1/*
2 * Copyright (c) 2004, 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.perfdata.monitor.v2_0;
27
28/**
29 * A typesafe enumeration for describing standard Java type codes.
30 *
31 * @author Brian Doherty
32 * @since 1.5
33 */
34public class TypeCode {
35
36    private final String name;
37    private final char value;
38
39    public static final TypeCode BOOLEAN = new TypeCode("boolean", 'Z');
40    public static final TypeCode CHAR    = new TypeCode("char",    'C');
41    public static final TypeCode FLOAT   = new TypeCode("float",   'F');
42    public static final TypeCode DOUBLE  = new TypeCode("double",  'D');
43    public static final TypeCode BYTE    = new TypeCode("byte",    'B');
44    public static final TypeCode SHORT   = new TypeCode("short",   'S');
45    public static final TypeCode INT     = new TypeCode("int",     'I');
46    public static final TypeCode LONG    = new TypeCode("long",    'J');
47    public static final TypeCode OBJECT  = new TypeCode("object",  'L');
48    public static final TypeCode ARRAY   = new TypeCode("array",   '[');
49    public static final TypeCode VOID    = new TypeCode("void",    'V');
50
51    private static TypeCode basicTypes[] = {
52        LONG, BYTE, BOOLEAN, CHAR, FLOAT, DOUBLE,
53        SHORT, INT, OBJECT, ARRAY, VOID
54    };
55
56    /**
57     * Convert enumeration value to a String.
58     *
59     * @return String - the string representation for the enumeration.
60     */
61    public String toString() {
62        return name;
63    }
64
65    /**
66     * Convert enumeration to its character representation.
67     *
68     * @return int - the integer representation for the enumeration.
69     */
70    public int toChar() {
71        return value;
72    }
73
74    /**
75     * Map a character value to its corresponding TypeCode object.
76     *
77     * @param c an character representing a Java TypeCode
78     * @return TypeCode - The TypeCode enumeration object for the given
79     *                    character.
80     * @throws IllegalArgumentException Thrown if <code>c</code> is not
81     *                                  a valid Java TypeCode.
82     */
83    public static TypeCode toTypeCode(char c) {
84        for (int j = 0; j < basicTypes.length; j++) {
85            if (basicTypes[j].value == c) {
86                return (basicTypes[j]);
87            }
88        }
89        throw new IllegalArgumentException();
90    }
91
92    /**
93     * Map a character value to its corresponding TypeCode object.
94     *
95     * @param b a byte representing a Java TypeCode. This value is
96     *          converted into a char and used to find the corresponding
97     *          TypeCode.
98     * @return TypeCode - The TypeCode enumeration object for the given byte.
99     * @throws IllegalArgumentException Thrown if <code>v</code> is not
100     *                                  a valid Java TypeCode.
101     */
102    public static TypeCode toTypeCode(byte b) {
103        return toTypeCode((char)b);
104    }
105
106    private TypeCode(String name, char value) {
107        this.name = name;
108        this.value = value;
109    }
110}
111