DefaultParser.java revision 1870:4aa2e64eff30
1/*
2 * Copyright (c) 2015, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.failurehandler.value;
25
26import java.util.HashMap;
27import java.util.Map;
28
29public class DefaultParser implements ValueParser {
30    private static final Map<Class<?>, BasicParser> PARSERS = new HashMap<>();
31
32    static {
33        BasicParser.init();
34    }
35
36    @Override
37    public Object parse(Class<?> type, String value, String s) {
38        if (type.isArray()) {
39            return new ArrayParser(this).parse(type, value, s);
40        }
41        ValueParser parser = PARSERS.get(type);
42        if (parser == null) {
43            throw new IllegalArgumentException("can't find parser for "
44                    + type.getName());
45        }
46
47        return parser.parse(type, value, s);
48    }
49
50    private static enum BasicParser implements ValueParser {
51        BOOL(boolean.class, Boolean.class) {
52            @Override
53            public Object parse(Class<?> type, String value, String s) {
54                return Boolean.valueOf(value);
55            }
56        },
57        BYTE(byte.class, Byte.class) {
58            @Override
59            public Object parse(Class<?> type, String value, String s) {
60                return Byte.decode(value);
61            }
62        },
63        CHAR(char.class, Character.class) {
64            @Override
65            public Object parse(Class<?> type, String value, String s) {
66                if (value.length() != 1) {
67                    throw new IllegalArgumentException(
68                            String.format("can't cast %s to char", value));
69                }
70                return value.charAt(0);
71            }
72        },
73        SHORT(short.class, Short.class) {
74            @Override
75            public Object parse(Class<?> type, String value, String s) {
76                return Short.decode(value);
77            }
78        },
79        INT(int.class, Integer.class) {
80            @Override
81            public Object parse(Class<?> type, String value, String s) {
82                return Integer.decode(value);
83            }
84        },
85        LONG(long.class, Long.class) {
86            @Override
87            public Object parse(Class<?> type, String value, String s) {
88                return Long.decode(value);
89            }
90        },
91        FLOAT(float.class, Float.class) {
92            @Override
93            public Object parse(Class<?> type, String value, String s) {
94                return Float.parseFloat(value);
95            }
96        },
97        DOUBLE(double.class, Double.class) {
98            @Override
99            public Object parse(Class<?> type, String value, String s) {
100                return Double.parseDouble(value);
101            }
102        },
103        STRING(String.class, Object.class) {
104            @Override
105            public Object parse(Class<?> type, String value, String s) {
106                return value;
107            }
108        };
109
110        private BasicParser(Class<?>... classes) {
111            for (Class<?> aClass : classes) {
112                DefaultParser.PARSERS.put(aClass, this);
113            }
114        }
115
116        private static void init() {
117            // no-op used to provoke <cinit>
118        }
119    }
120}
121