net.sf.staccatocommons.collections.iterable.internal.IterablesInternal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-collections Show documentation
Show all versions of commons-collections Show documentation
Collections library of the Staccato-Commons project, focused on providing new abstractions
that mix object oriented and functional programming style for dealing with iterable objects.
The newest version!
/**
* Copyright (c) 2011, The Staccato-Commons Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package net.sf.staccatocommons.collections.iterable.internal;
import java.util.Collection;
import java.util.Iterator;
import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.Evaluable;
/**
* This class is not part of of the public API of collections-extra.
*
* @author fbulgarelli
*/
public class IterablesInternal {
/***/
public static final String ITERABLE = "iterable";
/***/
public static void addAllInternal(Collection collection, Iterable iterable) {
for (T element : iterable)
collection.add(element);
}
/***/
public static > C takeInternal(Iterable iterable, int amountOfElements,
C outputCollection) {
Iterator iter = iterable.iterator();
for (int i = 0; i < amountOfElements && iter.hasNext(); i++)
outputCollection.add(iter.next());
return outputCollection;
}
/***/
public static > C filterInternal(Iterable iterable, Evaluable predicate,
C collection) {
for (T element : iterable)
if (predicate.eval(element))
collection.add(element);
return collection;
}
/***/
public static > C collectInternal(Iterable iterable,
Applicable function, C outputCollection) {
for (I element : iterable)
outputCollection.add(function.apply(element));
return outputCollection;
}
/***/
public static boolean containsInternal(Iterable iterable, T element) {
for (T each : iterable)
if (each.equals(element))
return true;
return false;
}
}