ArrayParser.java revision 1870:4aa2e64eff30
199461Sobrien/* 2218822Sdim * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 499461Sobrien * 599461Sobrien * This code is free software; you can redistribute it and/or modify it 699461Sobrien * under the terms of the GNU General Public License version 2 only, as 799461Sobrien * published by the Free Software Foundation. 899461Sobrien * 999461Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT 1099461Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1199461Sobrien * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1299461Sobrien * version 2 for more details (a copy is included in the LICENSE file that 1399461Sobrien * accompanied this code). 1499461Sobrien * 1599461Sobrien * You should have received a copy of the GNU General Public License version 1699461Sobrien * 2 along with this work; if not, write to the Free Software Foundation, 1799461Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1899461Sobrien * 1999461Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20218822Sdim * or visit www.oracle.com if you need additional information or have any 21218822Sdim * questions. 2299461Sobrien */ 23218822Sdim 2499461Sobrienpackage jdk.test.failurehandler.value; 2599461Sobrien 2699461Sobrienimport java.lang.reflect.Array; 2799461Sobrienimport java.util.Objects; 2899461Sobrien 2999461Sobrienpublic class ArrayParser implements ValueParser { 3099461Sobrien private final ValueParser parser; 3199461Sobrien 3299461Sobrien public ArrayParser(ValueParser parser) { 33130561Sobrien Objects.requireNonNull(parser); 3499461Sobrien this.parser = parser; 3599461Sobrien } 3699461Sobrien 3799461Sobrien @Override 3899461Sobrien public Object parse(Class<?> type, String value, String delimiter) { 39130561Sobrien Class<?> component = type.getComponentType(); 4099461Sobrien if (component.isArray()) { 41130561Sobrien throw new IllegalArgumentException( 4299461Sobrien "multidimensional array fields aren't supported"); 4399461Sobrien } 44218822Sdim String[] values = (value == null || value.isEmpty()) 45107492Sobrien ? new String[]{} 46130561Sobrien : value.split(delimiter); 4799461Sobrien Object result = Array.newInstance(component, values.length); 4899461Sobrien for (int i = 0, n = values.length; i < n; ++i) { 49130561Sobrien Array.set(result, i, parser.parse(component, values[i], delimiter)); 50130561Sobrien } 51130561Sobrien return result; 52130561Sobrien } 5399461Sobrien} 54130561Sobrien