From 8187f024bae57267af514c5dcb730de09e573e41 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 1 Jun 2015 11:41:16 +1000 Subject: Move packages, make lists shrink in size, generate lists instead of arrays as the 'primitive' operation (issues with generics) --- .../java/au/id/zancanaro/javacheck/Iterators.java | 177 +++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 src/main/java/au/id/zancanaro/javacheck/Iterators.java (limited to 'src/main/java/au/id/zancanaro/javacheck/Iterators.java') diff --git a/src/main/java/au/id/zancanaro/javacheck/Iterators.java b/src/main/java/au/id/zancanaro/javacheck/Iterators.java new file mode 100644 index 0000000..347a927 --- /dev/null +++ b/src/main/java/au/id/zancanaro/javacheck/Iterators.java @@ -0,0 +1,177 @@ +package au.id.zancanaro.javacheck; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; +import java.util.function.Predicate; + +public final class Iterators { + private Iterators() { + } + + public static RangeIterator rangeIterator(int countTo, Function fn) { + return new RangeIterator(countTo, fn); + } + + private static class RangeIterator implements Iterator { + private final Function action; + private final int countTo; + private int index = 0; + + public RangeIterator(int countTo, Function action) { + this.countTo = countTo; + this.action = action; + } + + @Override + public boolean hasNext() { + return index < countTo; + } + + @Override + public T next() { + return action.apply(index++); + } + } + + public static FlattenIterator flatten(Iterator> iterators) { + return new FlattenIterator<>(iterators); + } + public static class FlattenIterator implements Iterator { + private Iterator current; + + private Iterator> iterators; + + public FlattenIterator(Iterator> iterators) { + this.current = Iterators.emptyIterator(); + this.iterators = iterators; + } + + private Iterator getCurrent() { + while (!current.hasNext() && iterators.hasNext()) { + current = iterators.next(); + } + return current; + } + + @Override + public boolean hasNext() { + return getCurrent().hasNext(); + } + @Override + public T next() { + return getCurrent().next(); + } + + } + + public static ConcatIterator concat(Iterator left, Iterator right) { + return new ConcatIterator<>(left, right); + } + public static class ConcatIterator implements Iterator { + private final Iterator left; + + private final Iterator right; + + public ConcatIterator(Iterator left, Iterator right) { + this.left = left; + this.right = right; + } + + @Override + public boolean hasNext() { + return left.hasNext() || right.hasNext(); + } + @Override + public T next() { + if (left.hasNext()) { + return left.next(); + } else { + return right.next(); + } + } + + } + + public static EmptyIterator emptyIterator() { + return new EmptyIterator<>(); + } + public static class EmptyIterator implements Iterator { + + @Override + public boolean hasNext() { + return false; + } + @Override + public T next() { + return null; + } + + } + + public static MappingIterator mappingIterator(Function f, Iterator iterator) { + return new MappingIterator<>(f, iterator); + } + private static class MappingIterator implements Iterator { + private final Function mapping; + + private final Iterator iterator; + + public MappingIterator(Function mapping, Iterator iterator) { + this.mapping = mapping; + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + @Override + public R next() { + return mapping.apply(iterator.next()); + } + + } + + public static FilteringIterator filteringIterator(Predicate pred, Iterator iterator) { + return new FilteringIterator<>(pred, iterator); + } + + private static class FilteringIterator implements Iterator { + private final Predicate predicate; + private final Iterator iterator; + private List nextValue; + + public FilteringIterator(Predicate pred, Iterator iterator) { + this.predicate = pred; + this.iterator = iterator; + this.nextValue = null; + } + + private void populateNext() { + while (nextValue == null && iterator.hasNext()) { + T value = iterator.next(); + if (predicate.test(value)) { + nextValue = Collections.singletonList(value); + } else { + nextValue = null; + } + } + } + + @Override + public boolean hasNext() { + populateNext(); + return nextValue != null; + } + + @Override + public T next() { + populateNext(); + T result = nextValue.get(0); + nextValue = null; + return result; + } + } +} -- cgit v1.2.3