| 7 | import java.util.stream.StreamSupport; |
| 8 | |
| 9 | public abstract class Lazy<T> { |
| 10 | |
| 11 | private final int estimatedSize; |
| 12 | |
| 13 | public Lazy(int estimatedSize) { |
| 14 | this.estimatedSize = estimatedSize; |
| 15 | } |
| 16 | |
| 17 | public Lazy() { |
| 18 | this.estimatedSize = 10; |
| 19 | } |
| 20 | |
| 21 | public Lazy<T> reset() |
| 22 | { |
| 23 | future = initialize(); |
| 24 | return this; |
| 25 | } |
| 26 | |
| 27 | public Stream<T> stream() { |
| 28 | return StreamSupport.stream(Spliterators.spliteratorUnknownSize(internal, Spliterator.IMMUTABLE | Spliterator.ORDERED), false); |
| 29 | } |
| 30 | |
| 31 | protected abstract Iterator<T> initialize(); |
| 32 | |
| 33 | Iterator<T> future = null; |
| 34 | |
| 35 | private Iterator internal =new Iterator() { |
| 36 | @Override public boolean hasNext () { |
| 37 | if (future == null) future = pull(); |
| 38 | if (future == null) return false; |
| 39 | while (!future.hasNext()) |
| 40 | { |
| 41 | future = pull(); |
| 42 | if (future==null) return false; |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | @Override public T next () { |
| 48 | if (future==null) { |
| 49 | if (!hasNext()) return null; |
| 50 | T r = future.next(); |
| 51 | // System.out.println(" next returning 1:"+r); |
| 52 | return r; |
| 53 | } |
| 54 | if (!future.hasNext()) |
| 55 | { |
| 56 | if (!hasNext()) return null; |
| 57 | T r = future.next(); |
| 58 | // System.out.println(" next returning 2:"+r); |
| 59 | return r; |
| 60 | } |
| 61 | T r = future.next(); |
| 62 | // System.out.println(" next returning 3:"+r); |
| 63 | return r; |
| 64 | } |
| 65 | }; |
| 66 |
nothing calls this directly
no outgoing calls
no test coverage detected