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: 36321 $
037 */
038 @Converter
039 public final class CollectionConverter {
040 /**
041 * Utility classes should not have a public constructor.
042 */
043 private CollectionConverter() {
044 }
045
046 /**
047 * Converts a collection to an array
048 */
049 @Converter
050 public static Object[] toArray(Collection value) {
051 if (value == null) {
052 return null;
053 }
054 return value.toArray();
055 }
056
057 /**
058 * Converts an array to a collection
059 */
060 @Converter
061 public static List 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 List toList(Collection collection) {
070 return new ArrayList(collection);
071 }
072
073 @Converter
074 public static Set toSet(Object[] array) {
075 Set answer = new HashSet();
076 for (Object element : array) {
077 answer.add(element);
078 }
079 return answer;
080 }
081
082 @Converter
083 public static Set toSet(Collection collection) {
084 return new HashSet(collection);
085 }
086
087 @Converter
088 public static Set toSet(Map map) {
089 return map.entrySet();
090 }
091
092 @Converter
093 public static Properties toProperties(Map map) {
094 Properties answer = new Properties();
095 answer.putAll(map);
096 return answer;
097 }
098
099 @Converter
100 public static Hashtable toHashtable(Map map) {
101 return new Hashtable(map);
102 }
103
104 @Converter
105 public static HashMap toHashMap(Map map) {
106 return new HashMap(map);
107 }
108 }