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