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.List;
026 import java.util.Map;
027 import java.util.Properties;
028 import java.util.Set;
029
030 import org.apache.camel.Converter;
031
032 /**
033 * Some core java.util Collection based
034 * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
035 *
036 * @version $Revision: 660275 $
037 */
038 @Converter
039 public final class CollectionConverter {
040
041 /**
042 * Utility classes should not have a public constructor.
043 */
044 private CollectionConverter() {
045 }
046
047 /**
048 * Converts a collection to an array
049 */
050 @Converter
051 public static Object[] toArray(Collection value) {
052 if (value == null) {
053 return null;
054 }
055 return value.toArray();
056 }
057
058 /**
059 * Converts an array to a collection
060 */
061 @Converter
062 public static List toList(Object[] array) {
063 return Arrays.asList(array);
064 }
065
066 /**
067 * Converts a collection to a List if it is not already
068 */
069 @Converter
070 public static List toList(Collection collection) {
071 return new ArrayList(collection);
072 }
073
074 @Converter
075 public static Set toSet(Object[] array) {
076 Set answer = new HashSet();
077 answer.addAll(Arrays.asList(array));
078 return answer;
079 }
080
081 @Converter
082 public static Set toSet(Collection collection) {
083 return new HashSet(collection);
084 }
085
086 @Converter
087 public static Set toSet(Map map) {
088 return map.entrySet();
089 }
090
091 @Converter
092 public static Properties toProperties(Map map) {
093 Properties answer = new Properties();
094 answer.putAll(map);
095 return answer;
096 }
097
098 @Converter
099 public static Hashtable toHashtable(Map map) {
100 return new Hashtable(map);
101 }
102
103 @Converter
104 public static HashMap toHashMap(Map map) {
105 return new HashMap(map);
106 }
107 }