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 */ 017package org.apache.xbean.propertyeditor; 018 019import java.beans.PropertyEditor; 020import java.util.Collection; 021import java.util.List; 022import java.util.ArrayList; 023import java.lang.reflect.Array; 024 025/** 026 * @version $Rev: 6687 $ $Date: 2005-12-28T21:08:56.733437Z $ 027 */ 028public abstract class AbstractCollectionConverter extends AbstractConverter { 029 private final PropertyEditor editor; 030 031 public AbstractCollectionConverter(Class type) { 032 super(type); 033 this.editor = new StringEditor(); 034 } 035 036 public AbstractCollectionConverter(Class type, PropertyEditor editor) { 037 super(type); 038 039 if (editor == null) { 040 throw new NullPointerException("editor is null"); 041 } 042 this.editor = editor; 043 } 044 045 protected final Object toObjectImpl(String text) { 046 List list = CollectionUtil.toList(text, getEditor()); 047 if (list == null) { 048 return null; 049 } 050 Object collection = createCollection(list); 051 return collection; 052 } 053 054 protected abstract Object createCollection(List list); 055 056 protected final String toStringImpl(Object value) { 057 Collection values; 058 if (value.getClass().isArray()) { 059 values = new ArrayList(Array.getLength(value)); 060 for (int i = 0; i < Array.getLength(value); i++) { 061 values.add(Array.get(value, i)); 062 } 063 } else { 064 values = (Collection) value; 065 } 066 067 String text = CollectionUtil.toString(values, getEditor()); 068 return text; 069 } 070 071 protected PropertyEditor getEditor() { 072 return editor; 073 } 074}