@SuppressWarnings("unchecked") @Override public <O> List<O> asList() throws E { return this.iterable.aggregate(new ArrayList<O>(), (l, t) -> { l.add((O) t); return l; }); }
@Override public IStream<T, E> foreach(final IAssimilator<Integer, T, E> assimilator) { try { this.iterable.foreach(assimilator); return this; } catch (final Exception exception) { return stream(this.exceptionClass, exception); } }
default IIterator<O, E> iterator(final IAcceptor<O> acceptor) { return new IIterator<O, E>() { private final IIterator<O, E> iterator = iterator(); private O item = null; @Override public boolean hasNext() throws E { while (this.iterator.hasNext()) { final O i = this.iterator.next(); if (acceptor.accept(i) && (this.item = i) != null) { return true; } } return false; } @Override public O next() throws E { try { if (this.item != null || hasNext()) { return this.item; } throw new NoSuchElementException(); } finally { this.item = null; } } }; }
@Override public IOptional<T, E> first(final IAcceptor<T> acceptor) { try { return Optional.of(this.exceptionClass, this.iterable.first(acceptor)); } catch (final Exception exception) { return optional(this.exceptionClass, exception); } }
@Override public IOptional<T, E> first() { try { return Optional.of(this.exceptionClass, this.iterable.first(v -> true)); } catch (final Exception exception) { return optional(this.exceptionClass, exception); } }
@Override public IIterator<O, E> iterator() { return new IteratorFlattingIterator<>(this.input.iterator(), this.acceptor, this.converter); } }
@Override public Iterable<T> asIterable() throws E { return this.iterable.aggregate(new LinkedList<T>(), (l, t) -> { l.add(t); return l; }); }
@Override public IStream<T, E> foreach(final IConsumer<T, E> consumer) { try { this.iterable.foreach(consumer); return this; } catch (final Exception exception) { return stream(this.exceptionClass, exception); } }
@Override public IIterator<T, E> iterator() { return new IteratorFilteringIterator<>(this.input.iterator(), this.acceptor); } }
@SuppressWarnings("unchecked") @Override public <O> Set<O> asSet() throws E { return this.iterable.aggregate(new LinkedHashSet<O>(), (l, t) -> { l.add((O) t); return l; }); }
@Override public IIterator<O, E> iterator() { return new IteratorConvertingIterator<>(this.input.iterator(), this.acceptor, this.converter); } }
@Override public <K, V> Map<K, V> asMap(final IFactory<T, K, E> keyFactrory, final IFactory<T, V, E> valueFactrory) throws E { return this.iterable.aggregate(new LinkedHashMap<K, V>(), (l, t) -> { l.put(keyFactrory.create(t), valueFactrory.create(t)); return l; }); }
@Override public IIterator<O, E> iterator() { return new IteratorCountingIterator<>(this.input.iterator(), this.acceptor, this.aggegator); }
@Override public <O> IOptional<O, E> aggregate(final O identity, final IAggregator<O, T, O, E> aggegator) { try { return Optional.of(this.exceptionClass, this.iterable.aggregate(identity, aggegator)); } catch (final Exception exception) { return optional(this.exceptionClass, exception); } }
default O first(final IAcceptor<O> acceptor) throws E { final IIterator<O, E> iterator = this.iterator(); while (iterator.hasNext()) { final O value = iterator.next(); if (acceptor.accept(value)) { return value; } } return null; }
default <R> R aggregate(final R identity, final IAggregator<R, O, R, E> adder) throws E { final IIterator<O, E> iterator = this.iterator(); R result = identity; while (iterator.hasNext()) { result = adder.aggregate(result, iterator.next()); } return result; }
default void foreach(final IConsumer<O, E> consumer) throws E { final IIterator<O, E> iterator = this.iterator(); while (iterator.hasNext()) { final O value = iterator.next(); consumer.consume(value); } }
default void foreach(final IAssimilator<Integer, O, E> assimilator) throws E { final IIterator<O, E> iterator = this.iterator(); final IntCounter counter = new IntCounter(-1); while (iterator.hasNext()) { final O value = iterator.next(); assimilator.assimilate(counter.next(), value); } }