001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy
006     * of the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations
014     * under the License.
015     */
016    package org.springframework.richclient.convert.support;
017    
018    import org.springframework.binding.convert.ConversionContext;
019    import org.springframework.binding.convert.Converter;
020    import org.springframework.binding.convert.support.AbstractConverter;
021    import org.springframework.core.ReflectiveVisitorHelper;
022    
023    import java.util.ArrayList;
024    import java.util.Arrays;
025    import java.util.Collection;
026    import java.util.Collections;
027    import java.util.List;
028    
029    /**
030     * This converter converts collection values from Object, Object[], Collection, List to Collection.class, List.class,
031     * Object[].class types
032     * 
033     * @author Mathias Broekelmann
034     * 
035     */
036    public class CollectionConverter extends AbstractConverter implements Converter {
037    
038        private static final Class[] SOURCE_CLASSES = new Class[] { Object.class, Collection.class, List.class,
039                Object[].class };
040    
041        private static final Class[] TARGET_CLASSES = new Class[] { Collection.class, List.class, Object[].class };
042    
043        private final ReflectiveVisitorHelper visitorHelper = new ReflectiveVisitorHelper();
044    
045        private Object visitor = new ValuesVisitor();
046    
047        protected Object doConvert(Object sourceValue, Class targetClass, ConversionContext context) throws Exception {
048            List values = (List) visitorHelper.invokeVisit(visitor, sourceValue);
049            if (Object[].class == targetClass) {
050                return values.toArray();
051            }
052            return values;
053        }
054    
055        public Class[] getSourceClasses() {
056            return SOURCE_CLASSES;
057        }
058    
059        public Class[] getTargetClasses() {
060            return TARGET_CLASSES;
061        }
062    
063        protected static class ValuesVisitor {
064            List visitNull() {
065                return Collections.EMPTY_LIST;
066            }
067    
068            List visit(Object value) {
069                return visit(new Object[] { value });
070            }
071    
072            List visit(Collection values) {
073                return new ArrayList(values);
074            }
075    
076            List visit(List values) {
077                return values;
078            }
079    
080            List visit(Object[] values) {
081                return Arrays.asList(values);
082            }
083        }
084    
085    }