ArrayParser.java revision 1870:4aa2e64eff30
11573Srgrimes/*
21573Srgrimes * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41573Srgrimes *
51573Srgrimes * This code is free software; you can redistribute it and/or modify it
61573Srgrimes * under the terms of the GNU General Public License version 2 only, as
71573Srgrimes * published by the Free Software Foundation.
81573Srgrimes *
91573Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101573Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111573Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121573Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131573Srgrimes * accompanied this code).
141573Srgrimes *
151573Srgrimes * You should have received a copy of the GNU General Public License version
161573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181573Srgrimes *
191573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201573Srgrimes * or visit www.oracle.com if you need additional information or have any
211573Srgrimes * questions.
221573Srgrimes */
231573Srgrimes
241573Srgrimespackage jdk.test.failurehandler.value;
251573Srgrimes
261573Srgrimesimport java.lang.reflect.Array;
271573Srgrimesimport java.util.Objects;
281573Srgrimes
291573Srgrimespublic class ArrayParser implements ValueParser {
301573Srgrimes    private final ValueParser parser;
311573Srgrimes
321573Srgrimes    public ArrayParser(ValueParser parser) {
331573Srgrimes        Objects.requireNonNull(parser);
341573Srgrimes        this.parser = parser;
351573Srgrimes    }
3692986Sobrien
3792986Sobrien    @Override
381573Srgrimes    public Object parse(Class<?> type, String value, String delimiter) {
3971579Sdeischen        Class<?> component = type.getComponentType();
401573Srgrimes        if (component.isArray()) {
4171579Sdeischen            throw new IllegalArgumentException(
42101776Stjr                    "multidimensional array fields aren't supported");
4335129Sjb        }
441573Srgrimes        String[] values = (value == null || value.isEmpty())
4513545Sjulian                          ? new String[]{}
461573Srgrimes                          : value.split(delimiter);
471573Srgrimes        Object result = Array.newInstance(component, values.length);
4892889Sobrien        for (int i = 0, n = values.length; i < n; ++i) {
491573Srgrimes            Array.set(result, i, parser.parse(component, values[i], delimiter));
5013545Sjulian        }
5135129Sjb        return result;
52127198Stjr    }
53127198Stjr}
54126806Stjr