| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of the GNU
|
|---|
| 7 | * General Public License Version 3 only ("GPL").
|
|---|
| 8 | * You may not use this file except in compliance with the License.
|
|---|
| 9 | * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
|
|---|
| 10 | * See the License for the specific language governing permissions and limitations under the License.
|
|---|
| 11 | *
|
|---|
| 12 | * When distributing the software, include this License Header Notice in each file.
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | package org.jopendocument.util;
|
|---|
| 17 |
|
|---|
| 18 | import java.util.Collection;
|
|---|
| 19 | import java.util.Iterator;
|
|---|
| 20 | import java.util.List;
|
|---|
| 21 | import java.util.RandomAccess;
|
|---|
| 22 |
|
|---|
| 23 | import org.apache.commons.collections4.Transformer;
|
|---|
| 24 | import org.apache.commons.collections4.TransformerUtils;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Une classe regroupant des méthodes utilitaires pour les collections.
|
|---|
| 28 | *
|
|---|
| 29 | * @author ILM Informatique 30 sept. 2004
|
|---|
| 30 | */
|
|---|
| 31 | public class CollectionUtils {
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Concatene une collection. Cette méthode va appliquer un transformation sur chaque élément
|
|---|
| 35 | * avant d'appeler toString(). join([-1, 3, 0], " ,", doubleTransformer) == "-2, 6, 0"
|
|---|
| 36 | *
|
|---|
| 37 | * @param <E> type of items
|
|---|
| 38 | * @param c la collection a concaténer.
|
|---|
| 39 | * @param sep le séparateur entre chaque élément.
|
|---|
| 40 | * @param tf la transformation à appliquer à chaque élément.
|
|---|
| 41 | * @return la chaine composée de chacun des éléments séparés par <code>sep</code>.
|
|---|
| 42 | */
|
|---|
| 43 | static public final <E> String join(final Collection<E> c, final String sep, final Transformer<? super E, ?> tf) {
|
|---|
| 44 | if (c.size() == 0)
|
|---|
| 45 | return "";
|
|---|
| 46 |
|
|---|
| 47 | final StringBuffer res = new StringBuffer(c.size() * 4);
|
|---|
| 48 | if (c instanceof RandomAccess && c instanceof List) {
|
|---|
| 49 | final List<E> list = (List<E>) c;
|
|---|
| 50 | final int stop = c.size() - 1;
|
|---|
| 51 | for (int i = 0; i < stop; i++) {
|
|---|
| 52 | res.append(tf.transform(list.get(i)));
|
|---|
| 53 | res.append(sep);
|
|---|
| 54 |
|
|---|
| 55 | }
|
|---|
| 56 | res.append(tf.transform(list.get(stop)));
|
|---|
| 57 | } else {
|
|---|
| 58 | final Iterator<E> iter = c.iterator();
|
|---|
| 59 | while (iter.hasNext()) {
|
|---|
| 60 | final E elem = iter.next();
|
|---|
| 61 | res.append(tf.transform(elem));
|
|---|
| 62 | if (iter.hasNext())
|
|---|
| 63 | res.append(sep);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return res.toString();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Concatene une collection en appelant simplement toString() sur chaque élément.
|
|---|
| 71 | *
|
|---|
| 72 | * @param <T> type of collection
|
|---|
| 73 | * @param c la collection a concaténer.
|
|---|
| 74 | * @param sep le séparateur entre chaque élément.
|
|---|
| 75 | * @return la chaine composée de chacun des éléments séparés par <code>sep</code>.
|
|---|
| 76 | */
|
|---|
| 77 | static public <T> String join(Collection<T> c, String sep) {
|
|---|
| 78 | return join(c, sep, TransformerUtils.<T>nopTransformer());
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|