net.anwiba.commons.lang.stream.SequencedStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anwiba-commons-core Show documentation
Show all versions of anwiba-commons-core Show documentation
anwiba commons core library project
/*
* #%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.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
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.IConverter;
import net.anwiba.commons.lang.functional.IProcedure;
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 -> {
try {
return set.contains(i);
} finally {
set.add(i);
}
});
}
@Override
public IStream filter(final IAcceptor acceptor) {
return new SequencedStream<>(new FilteringIterableIterable<>(this.iterable, acceptor));
}
@Override
public void consum(final IProcedure procedure) throws E {
final IIterator iterator = this.iterable.iterator();
while (iterator.hasNext()) {
procedure.execute(iterator.next());
}
}
@Override
public IOptional first() throws E {
final IIterator iterator = this.iterable.iterator();
if (iterator.hasNext()) {
return Optional. create(iterator.next());
}
return Optional.create(null);
}
@Override
public IOptional first(final IAcceptor acceptor) throws E {
final IIterator iterator = this.iterable.iterator();
if (iterator.hasNext()) {
final T next = iterator.next();
if (acceptor.accept(next)) {
return Optional.create(next);
}
}
return Optional.create(null);
}
@Override
public Iterable asIterable() throws E {
return asList();
}
@Override
public Collection asCollection() throws E {
return asList();
}
@Override
public List asList() throws E {
final List result = new LinkedList<>();
final IIterator iterator = this.iterable.iterator();
while (iterator.hasNext()) {
result.add(iterator.next());
}
return result;
}
@Override
public IObjectList asObjectList() throws E {
return new ObjectList<>(asList());
}
@Override
public IOptional forAll(final O identity, final IAccumulator adder) throws E {
final IIterator iterator = this.iterable.iterator();
O dummy = identity;
while (iterator.hasNext()) {
dummy = adder.add(dummy, iterator.next());
}
return Optional.create(dummy);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy