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://activemq.apache.org/camel/type-converter.html">Type Converters</a> 037 * 038 * @version $Revision: 51094 $ 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 public static List toList(Collection collection) { 073 return new ArrayList(collection); 074 } 075 076 /** 077 * Converts an {@link Iterator} to a {@link ArrayList} 078 */ 079 @Converter 080 public static ArrayList toArrayList(Iterator it) { 081 ArrayList list = new ArrayList(); 082 while (it.hasNext()) { 083 list.add(it.next()); 084 } 085 return list; 086 } 087 088 @Converter 089 public static Set toSet(Object[] array) { 090 Set answer = new HashSet(); 091 answer.addAll(Arrays.asList(array)); 092 return answer; 093 } 094 095 @Converter 096 public static Set toSet(Collection collection) { 097 return new HashSet(collection); 098 } 099 100 @Converter 101 public static Set toSet(Map map) { 102 return map.entrySet(); 103 } 104 105 @Converter 106 public static Properties toProperties(Map map) { 107 Properties answer = new Properties(); 108 answer.putAll(map); 109 return answer; 110 } 111 112 @Converter 113 public static Hashtable toHashtable(Map map) { 114 return new Hashtable(map); 115 } 116 117 @Converter 118 public static HashMap toHashMap(Map map) { 119 return new HashMap(map); 120 } 121 122 /** 123 * Converts an {@link Iterable} into a {@link List} 124 */ 125 @Converter 126 public static List toList(Iterable iterable) { 127 if (iterable instanceof List) { 128 return (List) iterable; 129 } 130 List result = new LinkedList(); 131 for (Object value : iterable) { 132 result.add(value); 133 } 134 return result; 135 } 136 }