Projekt

Obecné

Profil

Stáhnout (2.86 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2

    
3
/**
4
 * <p>
5
 * Enum class that describes the type of a field encoded inside a classdesc description.
6
 * </p>
7
 *
8
 * <p>
9
 * This stores both information on the type (reference/array vs. primitive) and, in cases
10
 * of reference or array types, the name of the class being referred to.  
11
 * </p>
12
 */
13
public enum fieldtype {
14
    BYTE ('B', "byte"),
15
    CHAR ('C', "char"),
16
    DOUBLE ('D', "double"), 
17
    FLOAT ('F', "float"),
18
    INTEGER ('I', "int"),
19
    LONG ('J', "long"),
20
    SHORT ('S', "String"),
21
    BOOLEAN ('Z', "boolean"),
22
    ARRAY ('['),
23
    OBJECT ('L');
24
    private final char ch;
25
    private final String javatype;
26

    
27
    /**
28
     * Constructor for non-object (primitive) types.
29
     *
30
     * @param ch the character representing the type (must match one of those listed in
31
     * prim_typecode or obj_typecode in the Object Serialization Stream Protocol)
32
     */
33
    fieldtype(char ch) {
34
        this(ch, null);
35
    }
36

    
37
    /**
38
     * Constructor.
39
     *
40
     * @param ch the character representing the type (must match one of those listed in
41
     * prim_typecode or obj_typecode in the Object Serialization Stream Protocol)
42
     * @param javatype the name of the object class, where applicable (or null if not)
43
     */
44
    fieldtype(char ch, String javatype) {
45
        this.ch = ch;
46
        this.javatype = javatype;
47
    }
48

    
49
    /**
50
     * Gets the class name for a reference or array type.
51
     *
52
     * @return the name of the class being referred to, or null if this is not a
53
     * reference/array type
54
     */
55
    public String getJavaType() {
56
        return this.javatype;
57
    }
58

    
59
    /**
60
     * Gets the type character for this field.
61
     *
62
     * @return the type code character for this field; values will be one of those in
63
     * prim_typecode or obj_typecode in the protocol spec
64
     */
65
    public char ch() { return ch; }
66

    
67
    /**
68
     * Given a byte containing a type code, return the corresponding enum.
69
     *
70
     * @param b the type code; must be one of the charcaters in obj_typecode or
71
     * prim_typecode in the protocol spec
72
     * @return the corresponding fieldtype enum
73
     * @throws ValidityException if the type code is invalid
74
     */
75
    public static fieldtype get(byte b) throws ValidityException {
76
        switch(b) {
77
            case 'B': 
78
                return BYTE;
79
            case 'C':
80
                return CHAR;
81
            case 'D':
82
                return DOUBLE;
83
            case 'F':
84
                return FLOAT;
85
            case 'I':
86
                return INTEGER;
87
            case 'J':
88
                return LONG;
89
            case 'S':
90
                return SHORT;
91
            case 'Z':
92
                return BOOLEAN;
93
            case '[':
94
                return ARRAY;
95
            case 'L':
96
                return OBJECT;
97
            default:
98
                throw new ValidityException("invalid field type char: " + b);
99
        }
100
    }
101
}
102

    
(17-17/20)