DefaultParser.java revision 1870:4aa2e64eff30
1132451Sroberto/*
2132451Sroberto * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3132451Sroberto * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4290000Sglebius *
5290000Sglebius * This code is free software; you can redistribute it and/or modify it
6132451Sroberto * under the terms of the GNU General Public License version 2 only, as
7290000Sglebius * published by the Free Software Foundation.
8132451Sroberto *
9132451Sroberto * This code is distributed in the hope that it will be useful, but WITHOUT
10132451Sroberto * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11132451Sroberto * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12132451Sroberto * version 2 for more details (a copy is included in the LICENSE file that
13132451Sroberto * accompanied this code).
14132451Sroberto *
15132451Sroberto * You should have received a copy of the GNU General Public License version
16132451Sroberto * 2 along with this work; if not, write to the Free Software Foundation,
17132451Sroberto * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18132451Sroberto *
19132451Sroberto * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20132451Sroberto * or visit www.oracle.com if you need additional information or have any
21132451Sroberto * questions.
22132451Sroberto */
23132451Sroberto
24132451Srobertopackage jdk.test.failurehandler.value;
25182007Sroberto
26182007Srobertoimport java.util.HashMap;
27182007Srobertoimport java.util.Map;
28132451Sroberto
29132451Srobertopublic class DefaultParser implements ValueParser {
30132451Sroberto    private static final Map<Class<?>, BasicParser> PARSERS = new HashMap<>();
31132451Sroberto
32132451Sroberto    static {
33182007Sroberto        BasicParser.init();
34132451Sroberto    }
35290000Sglebius
36132451Sroberto    @Override
37132451Sroberto    public Object parse(Class<?> type, String value, String s) {
38132451Sroberto        if (type.isArray()) {
39132451Sroberto            return new ArrayParser(this).parse(type, value, s);
40132451Sroberto        }
41132451Sroberto        ValueParser parser = PARSERS.get(type);
42290000Sglebius        if (parser == null) {
43290000Sglebius            throw new IllegalArgumentException("can't find parser for "
44290000Sglebius                    + type.getName());
45132451Sroberto        }
46132451Sroberto
47132451Sroberto        return parser.parse(type, value, s);
48132451Sroberto    }
49132451Sroberto
50132451Sroberto    private static enum BasicParser implements ValueParser {
51132451Sroberto        BOOL(boolean.class, Boolean.class) {
52132451Sroberto            @Override
53132451Sroberto            public Object parse(Class<?> type, String value, String s) {
54132451Sroberto                return Boolean.valueOf(value);
55132451Sroberto            }
56132451Sroberto        },
57132451Sroberto        BYTE(byte.class, Byte.class) {
58132451Sroberto            @Override
59132451Sroberto            public Object parse(Class<?> type, String value, String s) {
60132451Sroberto                return Byte.decode(value);
61132451Sroberto            }
62132451Sroberto        },
63132451Sroberto        CHAR(char.class, Character.class) {
64132451Sroberto            @Override
65132451Sroberto            public Object parse(Class<?> type, String value, String s) {
66132451Sroberto                if (value.length() != 1) {
67132451Sroberto                    throw new IllegalArgumentException(
68132451Sroberto                            String.format("can't cast %s to char", value));
69132451Sroberto                }
70132451Sroberto                return value.charAt(0);
71132451Sroberto            }
72132451Sroberto        },
73132451Sroberto        SHORT(short.class, Short.class) {
74132451Sroberto            @Override
75132451Sroberto            public Object parse(Class<?> type, String value, String s) {
76132451Sroberto                return Short.decode(value);
77132451Sroberto            }
78290000Sglebius        },
79290000Sglebius        INT(int.class, Integer.class) {
80290000Sglebius            @Override
81132451Sroberto            public Object parse(Class<?> type, String value, String s) {
82132451Sroberto                return Integer.decode(value);
83132451Sroberto            }
84132451Sroberto        },
85132451Sroberto        LONG(long.class, Long.class) {
86132451Sroberto            @Override
87132451Sroberto            public Object parse(Class<?> type, String value, String s) {
88132451Sroberto                return Long.decode(value);
89132451Sroberto            }
90132451Sroberto        },
91132451Sroberto        FLOAT(float.class, Float.class) {
92182007Sroberto            @Override
93132451Sroberto            public Object parse(Class<?> type, String value, String s) {
94182007Sroberto                return Float.parseFloat(value);
95132451Sroberto            }
96182007Sroberto        },
97132451Sroberto        DOUBLE(double.class, Double.class) {
98132451Sroberto            @Override
99132451Sroberto            public Object parse(Class<?> type, String value, String s) {
100132451Sroberto                return Double.parseDouble(value);
101132451Sroberto            }
102132451Sroberto        },
103132451Sroberto        STRING(String.class, Object.class) {
104132451Sroberto            @Override
105132451Sroberto            public Object parse(Class<?> type, String value, String s) {
106132451Sroberto                return value;
107132451Sroberto            }
108182007Sroberto        };
109132451Sroberto
110132451Sroberto        private BasicParser(Class<?>... classes) {
111132451Sroberto            for (Class<?> aClass : classes) {
112132451Sroberto                DefaultParser.PARSERS.put(aClass, this);
113132451Sroberto            }
114132451Sroberto        }
115132451Sroberto
116132451Sroberto        private static void init() {
117132451Sroberto            // no-op used to provoke <cinit>
118132451Sroberto        }
119132451Sroberto    }
120132451Sroberto}
121132451Sroberto