Back to project page DKO.
The source code is released under:
GNU Lesser General Public License
If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.kered.dko; // w w w. ja va 2s.c o m import java.util.ArrayList; import java.util.Iterator; import java.util.List; class LazyCacheIterable<T extends Table> implements Iterable<T> { private final Iterable<T> src; private Iterator<T> srci = null; private final List<T> cache; int count = 0; boolean completelyLoaded = false; public LazyCacheIterable(final Iterable<T> src) { this.src = src; cache = new ArrayList<T>(); } public LazyCacheIterable(final Iterable<T> src, final int size) { this.src = src; cache = new ArrayList<T>(size); } @Override public Iterator<T> iterator() { if (srci==null) initSourceIterator(); return new ClosableIterator<T>() { int position = 0; @Override public boolean hasNext() { if (position==count && completelyLoaded) return false; if (!completelyLoaded) peekNext(); return position < count; } @Override public T next() { return cache.get(position++); } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public synchronized void close() { if (srci instanceof ClosableIterator) ((ClosableIterator)srci).close(); } }; } private synchronized void peekNext() { if (srci.hasNext()) { cache.add(srci.next()); ++count; } else { completelyLoaded = true; } } private synchronized void initSourceIterator() { if (srci!=null) return; srci = src.iterator(); } }