001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.propertyeditor;
018    
019    import java.beans.PropertyEditor;
020    import java.lang.reflect.Array;
021    import java.util.List;
022    import java.util.ListIterator;
023    
024    /**
025     * Adapter for editing array types.
026     *
027     * @version $Rev: 6687 $ $Date: 2005-12-28T21:08:56.733437Z $
028     */
029    public final class ArrayConverter extends AbstractCollectionConverter {
030        public ArrayConverter(Class type, PropertyEditor editor) {
031            super(type, editor);
032    
033            if (!type.isArray()) {
034                throw new IllegalArgumentException("type is not an array " + type.getSimpleName());
035            }
036    
037            if (type.getComponentType().isArray()) {
038                throw new IllegalArgumentException("type is a multi-dimensional array " + type.getSimpleName());
039            }
040    
041            if (editor == null) throw new NullPointerException("editor is null");
042        }
043    
044        protected Object createCollection(List list) {
045            Object array = Array.newInstance(getType().getComponentType(), list.size());
046            for (ListIterator iterator = list.listIterator(); iterator.hasNext();) {
047                Object item = iterator.next();
048                int index = iterator.previousIndex();
049                Array.set(array, index, item);
050            }
051            return array;
052        }
053    }