net.anwiba.commons.lang.stream.SequencedStream Maven / Gradle / Ivy
/*
* #%L
* anwiba commons core
* %%
* Copyright (C) 2007 - 2016 Andreas Bartels
* %%
* 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, either version 2.1 of the
* License, or (at your option) any later version.
*
* 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package net.anwiba.commons.lang.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;
import net.anwiba.commons.lang.collection.IObjectList;
import net.anwiba.commons.lang.collection.ObjectList;
import net.anwiba.commons.lang.functional.IAcceptor;
import net.anwiba.commons.lang.functional.IAggregator;
import net.anwiba.commons.lang.functional.IConsumer;
import net.anwiba.commons.lang.functional.IConverter;
import net.anwiba.commons.lang.functional.IFactory;
import net.anwiba.commons.lang.functional.IIterable;
import net.anwiba.commons.lang.optional.IOptional;
import net.anwiba.commons.lang.optional.Optional;
class SequencedStream implements IStream {
private final IIterable iterable;
SequencedStream(final IIterable iterable) {
this.iterable = iterable;
}
@Override
public IStream convert(final IConverter converter) {
return new SequencedStream<>(new ConvertingIterableIterable<>(this.iterable, i -> i != null, converter));
}
@Override
public IStream distinct() {
final Set set = new HashSet<>();
return filter(i -> !set.add(i));
}
@Override
public IStream call(final IConsumer consumer) throws E {
this.foreach(consumer);
return this;
}
@Override
public IStream filter(final IAcceptor acceptor) {
return new SequencedStream<>(new FilteringIterableIterable<>(this.iterable, acceptor));
}
@Override
public void foreach(final IConsumer consumer) throws E {
this.iterable.foreach(consumer);
}
@Override
public IOptional first() throws E {
return Optional.create(this.iterable.first(v -> true));
}
@Override
public IOptional first(final IAcceptor acceptor) throws E {
return Optional.create(this.iterable.first(acceptor));
}
@Override
public Iterable asIterable() throws E {
return this.iterable.aggregate(new LinkedList(), (l, t) -> {
l.add(t);
return l;
});
}
@Override
public Collection asCollection() throws E {
return asList();
}
@SuppressWarnings("unchecked")
@Override
public List asList() throws E {
return this.iterable.aggregate(new ArrayList(), (l, t) -> {
l.add((O) t);
return l;
});
}
@Override
public IObjectList asObjectList() throws E {
return new ObjectList<>(asList());
}
@Override
public IOptional aggregate(final O identity, final IAggregator aggegator) throws E {
return Optional.create(this.iterable.aggregate(identity, aggegator));
}
@Override
public O[] asArray(final IntFunction factory) throws E {
return asList().toArray(factory.apply(asList().size()));
}
@Override
public Map asMap(final IFactory keyFactrory, final IFactory valueFactrory) throws E {
return this.iterable.aggregate(new LinkedHashMap(), (l, t) -> {
l.put(keyFactrory.create(t), valueFactrory.create(t));
return l;
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy