pl.jalokim.utils.collection.Elements Maven / Gradle / Ivy
package pl.jalokim.utils.collection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static pl.jalokim.utils.collection.CollectionUtils.isLastIndex;
/**
* Simpler API than native java Stream API.
* Contains some shortcut methods for return some types.
*
* @param type of elements.
*/
public final class Elements {
private final Stream stream;
private Elements(Stream stream) {
this.stream = stream;
}
public static Elements elements(Iterable iterable) {
return new Elements<>(StreamSupport.stream(iterable.spliterator(), false));
}
public static Elements elements(T... array) {
return new Elements<>(Stream.of(array));
}
public static Elements elements(Stream stream) {
return new Elements<>(stream);
}
public Elements filter(Predicate predicate) {
return new Elements<>(stream.filter(predicate));
}
public Elements map(Function super T, ? extends R> mapper) {
return new Elements<>(this.stream.map(mapper));
}
public Elements flatMap(Function super T, ? extends Stream extends R>> mapper) {
return new Elements<>(this.stream.flatMap(mapper));
}
public T getFirst() {
return stream.findFirst().get();
}
public T getLast() {
return CollectionUtils.getLast(asList());
}
public List asList() {
return unmodifiableList(new ArrayList<>(stream.collect(toList())));
}
public Set asSet() {
return unmodifiableSet(new HashSet<>(stream.collect(toSet())));
}
@SuppressWarnings("PMD.UseVarargs")
public T[] asArray(T[] array) {
return stream.collect(toList()).toArray(array);
}
public Stream asStream() {
return stream;
}
/**
* For each with index and element.
*
* @param consumer consumer of index and element.
*/
public void forEach(BiConsumer consumer) {
AtomicInteger currentIndex = new AtomicInteger();
stream.forEach(element -> consumer.accept(currentIndex.getAndIncrement(), element));
}
/**
* For each with index and element and useful method isFirst, isLast.
*
* @param consumer for IndexedElement which holds element with T type and index
*/
public void forEach(Consumer> consumer) {
List elements = asList();
int index = 0;
for (T element : elements) {
consumer.accept(new IndexedElement<>(index, element, index == 0, isLastIndex(elements, index)));
index++;
}
}
}