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 java.util.Arrays;
020    import java.util.Collection;
021    import java.util.HashSet;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import org.apache.camel.Converter;
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         * Utility classes should not have a public constructor.
039         */
040        private CollectionConverter() {        
041        }
042    
043        /**
044         * Converts a collection to an array
045         */
046        @Converter
047        public static Object[] toArray(Collection value) {
048            if (value == null) {
049                return null;
050            }
051            return value.toArray();
052        }
053    
054        /**
055         * Converts an array to a collection
056         */
057        @Converter
058        public static List toList(Object[] array) {
059            return Arrays.asList(array);
060        }
061    
062        @Converter
063        public static Set toSet(Object[] array) {
064            Set answer = new HashSet();
065            for (Object element : array) {
066                answer.add(element);
067            }
068            return answer;
069        }
070    
071        @Converter
072        public static Set toSet(Collection collection) {
073            return new HashSet(collection);
074        }
075    
076        @Converter
077        public static Set toSet(Map map) {
078            return map.entrySet();
079        }
080    }