1 /* 2 * #%L 3 * Nuiton Utils 4 * %% 5 * Copyright (C) 2004 - 2010 CodeLutin 6 * %% 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as 9 * published by the Free Software Foundation, either version 3 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Lesser Public License for more details. 16 * 17 * You should have received a copy of the GNU General Lesser Public 18 * License along with this program. If not, see 19 * <http://www.gnu.org/licenses/lgpl-3.0.html>. 20 * #L% 21 */ 22 23 package org.nuiton.util; 24 25 import java.util.Arrays; 26 import java.util.Collection; 27 import java.util.Collections; 28 import java.util.List; 29 import java.util.Set; 30 31 32 /** 33 * Created: 23 févr. 2006 09:03:39 34 * 35 * @author Benjamin Poussin - poussin@codelutin.com 36 */ 37 38 public class CollectionUtil { 39 40 /** 41 * Ajoute a la collection tous les elements passés en parametre 42 * 43 * @param <A> FIXME 44 * @param <E> FIXME 45 * @param col la collection 46 * @param e les elements a ajouter 47 * @return la collection passé en parametre 48 */ 49 public static <A, E extends Collection<A>> E addAll(E col, A... e) { 50 Collections.addAll(col, e); 51 return col; 52 } 53 54 /** 55 * Ajoute a la liste tous les elements passés en parametre 56 * 57 * @param <A> FIXME 58 * @param <E> FIXME 59 * @param col la liste 60 * @param pos le premier index où insérer les données 61 * @param e les elements a ajouter 62 * @return la liste passé en parametre 63 */ 64 public static <A, E extends List<A>> E addAll(E col, int pos, A... e) { 65 col.addAll(pos, Arrays.asList(e)); 66 return col; 67 } 68 69 /** 70 * Permet de convertir une liste non typée, en une liste typée. 71 * <p> 72 * La liste en entrée en juste bien castée. 73 * <p> 74 * On effectue une vérification sur le typage des élements de la liste. 75 * <p> 76 * Note : <b>Aucune liste n'est créee, ni recopiée</b> 77 * 78 * @param <O> data type 79 * @param list la liste à convertir 80 * @param type le type des éléments de la liste 81 * @return la liste typée 82 * @throws IllegalArgumentException si un élément de la liste en entrée n'est 83 * pas en adéquation avec le type voulue. 84 */ 85 @SuppressWarnings({"unchecked"}) 86 public static <O> List<O> toGenericList( 87 List<?> list, Class<O> type) throws IllegalArgumentException { 88 if (list.isEmpty()) { 89 return (List<O>) list; 90 } 91 for (Object o : list) { 92 if (!type.isAssignableFrom(o.getClass())) { 93 throw new IllegalArgumentException( 94 "can not cast List with object of type " + 95 o.getClass() + " to " + type + " type!"); 96 } 97 } 98 return (List<O>) list; 99 } 100 101 /** 102 * Permet de convertir une collection non typée, en une collection typée. 103 * <p> 104 * La collection en entrée en juste bien castée. 105 * <p> 106 * On effectue une vérification sur le typage des élements de la collection. 107 * <p> 108 * Note : <b>Aucune collection n'est créee, ni recopiée</b> 109 * 110 * @param <O> data type 111 * @param list la collection à convertir 112 * @param type le type des éléments de la collection 113 * @return la collection typée 114 * @throws IllegalArgumentException si un élément de la collection en entrée n'est 115 * pas en adéquation avec le type voulue. 116 */ 117 @SuppressWarnings({"unchecked"}) 118 public static <O> Collection<O> toGenericCollection( 119 Collection<?> list, Class<O> type) throws IllegalArgumentException { 120 if (list.isEmpty()) { 121 return (Collection<O>) list; 122 } 123 for (Object o : list) { 124 if (!type.isAssignableFrom(o.getClass())) { 125 throw new IllegalArgumentException( 126 "can not cast Collection with object of type " + 127 o.getClass() + " to " + type + " type!"); 128 } 129 } 130 return (Collection<O>) list; 131 } 132 133 /** 134 * Permet de convertir un ensemble non typée, en un ensemble typée. 135 * <p> 136 * L'ensemble en entrée en juste bien castée. 137 * <p> 138 * On effectue une vérification sur le typage des élements de la collection. 139 * <p> 140 * Note : <b>Aucun ensemble n'est créee, ni recopiée</b> 141 * 142 * @param <O> data type 143 * @param list l'ensemble à convertir 144 * @param type le type des éléments de l'ensemble 145 * @return l'ensemble typée 146 * @throws IllegalArgumentException si un élément de l'ensemble en entrée n'est 147 * pas en adéquation avec le type voulue. 148 */ 149 @SuppressWarnings({"unchecked"}) 150 public static <O> Set<O> toGenericSet( 151 Set<?> list, Class<O> type) throws IllegalArgumentException { 152 if (list.isEmpty()) { 153 return (Set<O>) list; 154 } 155 for (Object o : list) { 156 if (!type.isAssignableFrom(o.getClass())) { 157 throw new IllegalArgumentException( 158 "can not cast Set with object of type " + 159 o.getClass() + " to " + type + " type!"); 160 } 161 } 162 return (Set<O>) list; 163 } 164 165 /** 166 * Get data at given {@code index} from the given collection or 167 * {@code null} if no element at given position. 168 * 169 * @param collection the collection to scan 170 * @param index index to seek 171 * @param <T> type of data in collection 172 * @return the data found at given index, or {@code null} if not found 173 * @since 2.6.4 174 */ 175 public static <T> T getOrNull(Collection<T> collection, int index) { 176 T result = null; 177 if (collection != null) { 178 int i = 0; 179 for (T t : collection) { 180 if (index == i) { 181 result = t; 182 break; 183 } 184 i++; 185 } 186 } 187 return result; 188 } 189 190 /** 191 * Get data at given {@code index} from the given collection. 192 * 193 * @param collection the collection to scan 194 * @param index index to seek 195 * @param <T> type of data in collection 196 * @return the data found at given index 197 * @throws IndexOutOfBoundsException if there is such index in collection 198 * @since 2.6.4 199 */ 200 public static <T> T get(Collection<T> collection, int index) throws IndexOutOfBoundsException { 201 T result = null; 202 if (collection != null) { 203 int i = 0; 204 for (T t : collection) { 205 if (index == i) { 206 result = t; 207 break; 208 } 209 i++; 210 } 211 if (i != index) { 212 throw new IndexOutOfBoundsException("No element at index " + index); 213 } 214 } 215 return result; 216 } 217 218 /** 219 * Get data at given {@code index} from the given list. 220 * 221 * @param list the list to scan 222 * @param index index to seek 223 * @param <T> type of data in collection 224 * @return the data found at given index 225 * @throws IndexOutOfBoundsException if there is such index in list 226 * @since 2.6.4 227 */ 228 public static <T> T get(List<T> list, int index) throws IndexOutOfBoundsException { 229 T result = null; 230 if (list != null) { 231 if (index >= list.size()) { 232 throw new IndexOutOfBoundsException("No element at index " + index); 233 } 234 result = list.get(index); 235 } 236 return result; 237 } 238 239 /** 240 * Get data at given {@code index} from the given list or {@code null} if 241 * no such index in list. 242 * 243 * @param list the list to scan 244 * @param index index to seek 245 * @param <T> type of data in collection 246 * @return the data found at given index or {@code null} if no such index in list 247 * @since 2.6.4 248 */ 249 public static <T> T getOrNull(List<T> list, int index) { 250 T result = null; 251 if (list != null) { 252 if (index < list.size()) { 253 result = list.get(index); 254 } 255 } 256 return result; 257 } 258 } 259 260