/** * Factory method to create a SetList using the supplied list to retain order. * <p> * If the list contains duplicates, these are removed (first indexed one kept). * A <code>HashSet</code> is used for the set behaviour. * * @param list the list to decorate, must not be null * @throws IllegalArgumentException if list is null */ public static SetUniqueList decorate(java.util.List<Object> list) { if (list == null) { throw new java.lang.IllegalArgumentException("List must not be null"); } if (list.isEmpty()) { return new SetUniqueList(list, new java.util.HashSet<Object>()); } else { java.util.List<Object> temp = new java.util.ArrayList<Object>(list); list.clear(); SetUniqueList sl = new SetUniqueList(list, new java.util.HashSet<Object>()); sl.addAll(temp); return sl; } }
/** * Transform the collection by applying a Transformer to each element. * <p> * If the input collection or transformer is null, there is no change made. * <p> * This routine is best for Lists, for which set() is used to do the * transformations "in place." For other Collections, clear() and addAll() * are used to replace elements. * <p> * If the input collection controls its input, such as a Set, and the * Transformer creates duplicates (or are otherwise invalid), the * collection may reduce in size due to calling this method. * * @param collection the collection to get the input from, may be null * @param transformer the transformer to perform, may be null */ public static void transform(java.util.Collection<Object> collection, Transformer transformer) { if (collection != null && transformer != null) { if (collection is java.util.List<Object>) { java.util.List<Object> list = (java.util.List<Object>)collection; for (java.util.ListIterator<Object> it = list.listIterator(); it.hasNext(); ) { it.set(transformer.transform(it.next())); } } else { java.util.Collection<Object> resultCollection = collect(collection, transformer); collection.clear(); collection.addAll(resultCollection); } } }