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 */
017package org.apache.camel.converter;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Hashtable;
025import java.util.Iterator;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.Map;
029import java.util.Properties;
030import java.util.Set;
031
032import 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 
039 */
040@Converter
041public 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        return value.toArray();
055    }
056
057    /**
058     * Converts an array to a collection
059     */
060    @Converter
061    public static List<Object> toList(Object[] array) {
062        return Arrays.asList(array);
063    }
064
065    /**
066     * Converts a collection to a List if it is not already
067     */
068    @Converter
069    public static <T> List<T> toList(Collection<T> collection) {
070        return new ArrayList<T>(collection);
071    }
072    
073    /**
074     * Converts an {@link Iterator} to a {@link ArrayList}
075     */
076    @Converter
077    public static <T> ArrayList<T> toArrayList(Iterator<T> it) {
078        ArrayList<T> list = new ArrayList<T>();
079        while (it.hasNext()) {
080            list.add(it.next());
081        }
082        return list;
083    }
084
085    @Converter
086    public static Set<Object> toSet(Object[] array) {
087        Set<Object> answer = new HashSet<Object>();
088        answer.addAll(Arrays.asList(array));
089        return answer;
090    }
091
092    @Converter
093    public static <T> Set<T> toSet(Collection<T> collection) {
094        return new HashSet<T>(collection);
095    }
096
097    @Converter
098    public static <K, V> Set<Map.Entry<K, V>> toSet(Map<K, V> map) {
099        return map.entrySet();
100    }
101
102    @Converter
103    public static Properties toProperties(Map<Object, Object> map) {
104        Properties answer = new Properties();
105        answer.putAll(map);
106        return answer;
107    }
108
109    @Converter
110    public static <K, V> Hashtable<K, V> toHashtable(Map<? extends K, ? extends V> map) {
111        return new Hashtable<K, V>(map);
112    }
113
114    @Converter
115    public static <K, V> HashMap<K, V>  toHashMap(Map<? extends K, ? extends V> map) {
116        return new HashMap<K, V>(map);
117    }
118
119    /**
120     * Converts an {@link Iterable} into a {@link List} 
121     */
122    @Converter
123    public static <T> List<T> toList(Iterable<T> iterable) {
124        if (iterable instanceof List) {
125            return (List<T>) iterable;
126        }
127        List<T> result = new LinkedList<T>();
128        for (T value : iterable) {
129            result.add(value);
130        }
131        return result;
132    }
133}