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.util.Collection; 021 import java.util.List; 022 import java.util.ArrayList; 023 import java.lang.reflect.Array; 024 025 /** 026 * @version $Rev: 6687 $ $Date: 2005-12-28T21:08:56.733437Z $ 027 */ 028 public 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) throw new NullPointerException("editor is null"); 040 this.editor = editor; 041 } 042 043 protected final Object toObjectImpl(String text) { 044 List list = CollectionUtil.toList(text, editor); 045 if (list == null) { 046 return null; 047 } 048 Object collection = createCollection(list); 049 return collection; 050 } 051 052 protected abstract Object createCollection(List list); 053 054 protected final String toStringImpl(Object value) { 055 Collection values; 056 if (value.getClass().isArray()) { 057 values = new ArrayList(Array.getLength(value)); 058 for (int i = 0; i < Array.getLength(value); i++) { 059 values.add(Array.get(value, i)); 060 } 061 } else { 062 values = (Collection) value; 063 } 064 065 String text = CollectionUtil.toString(values, editor); 066 return text; 067 } 068 }