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.camel.converter;
018    
019    import org.apache.camel.Converter;
020    
021    import java.util.Arrays;
022    import java.util.Collection;
023    import java.util.HashSet;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Set;
027    
028    /**
029     * Some core java.util Collection based
030     * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
031     *
032     * @version $Revision: 524215 $
033     */
034    @Converter
035    public class CollectionConverter {
036    
037        /**
038         * Converts a collection to an array
039         */
040        @Converter
041        public static Object[] toArray(Collection value) {
042            if (value == null) {
043                return null;
044            }
045            return value.toArray();
046        }
047    
048        /**
049         * Converts an array to a collection
050         */
051        @Converter
052        public static List toList(Object[] array) {
053            return Arrays.asList(array);
054        }
055    
056        @Converter
057        public static Set toSet(Object[] array) {
058            Set answer = new HashSet();
059            for (Object element : array) {
060                answer.add(element);
061            }
062            return answer;
063        }
064    
065        @Converter
066        public static Set toSet(Collection collection) {
067            return new HashSet(collection);
068        }
069    
070        @Converter
071        public static Set toSet(Map map) {
072            return map.entrySet();
073        }
074    }