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.ArrayList;
020    import java.util.Arrays;
021    import java.util.Collection;
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Hashtable;
025    import java.util.Iterator;
026    import java.util.LinkedList;
027    import java.util.List;
028    import java.util.Map;
029    import java.util.Properties;
030    import java.util.Set;
031    
032    import org.apache.camel.Converter;
033    
034    /**
035     * Some core java.util Collection based
036     * <a href="http://camel.apache.org/type-converter.html">Type Converters</a>
037     *
038     * @version $Revision: 747062 $
039     */
040    @Converter
041    public final class CollectionConverter {
042    
043        /**
044         * Utility classes should not have a public constructor.
045         */
046        private CollectionConverter() {
047        }
048    
049        /**
050         * Converts a collection to an array
051         */
052        @Converter
053        public static Object[] toArray(Collection value) {
054            if (value == null) {
055                return null;
056            }
057            return value.toArray();
058        }
059    
060        /**
061         * Converts an array to a collection
062         */
063        @Converter
064        public static List toList(Object[] array) {
065            return Arrays.asList(array);
066        }
067    
068        /**
069         * Converts a collection to a List if it is not already
070         */
071        @Converter
072        @SuppressWarnings("unchecked")
073        public static List toList(Collection collection) {
074            return new ArrayList(collection);
075        }
076        
077        /**
078         * Converts an {@link Iterator} to a {@link ArrayList}
079         */
080        @Converter
081        @SuppressWarnings("unchecked")
082        public static ArrayList toArrayList(Iterator it) {
083            ArrayList list = new ArrayList();
084            while (it.hasNext()) {
085                list.add(it.next());
086            }
087            return list;
088        }
089    
090        @Converter
091        @SuppressWarnings("unchecked")
092        public static Set toSet(Object[] array) {
093            Set answer = new HashSet();
094            answer.addAll(Arrays.asList(array));
095            return answer;
096        }
097    
098        @Converter
099        @SuppressWarnings("unchecked")
100        public static Set toSet(Collection collection) {
101            return new HashSet(collection);
102        }
103    
104        @Converter
105        public static Set toSet(Map map) {
106            return map.entrySet();
107        }
108    
109        @Converter
110        @SuppressWarnings("unchecked")
111        public static Properties toProperties(Map map) {
112            Properties answer = new Properties();
113            answer.putAll(map);
114            return answer;
115        }
116    
117        @Converter
118        @SuppressWarnings("unchecked")
119        public static Hashtable toHashtable(Map map) {
120            return new Hashtable(map);
121        }
122    
123        @Converter
124        @SuppressWarnings("unchecked")
125        public static HashMap toHashMap(Map map) {
126            return new HashMap(map);
127        }
128    
129        /**
130         * Converts an {@link Iterable} into a {@link List} 
131         */
132        @Converter
133        @SuppressWarnings("unchecked")
134        public static List toList(Iterable iterable) {
135            if (iterable instanceof List) {
136                return (List) iterable;
137            }
138            List result = new LinkedList();
139            for (Object value : iterable) {
140                result.add(value);
141            }
142            return result;
143        }
144    }