All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.commons.collections.DescenderIterator Maven / Gradle / Ivy

package org.aksw.commons.collections;

/**
 * @author Claus Stadler
 *         

* Date: 8/10/11 * Time: 12:03 PM */ import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Derived from * StackCartesianProductIterator it = new StackCartesianProductIterator(a, b,c); */ public class DescenderIterator implements Iterator> { // This list is used as a stack: // Every current element may be descended into //private List> collections = new ArrayList>(); private List> iterators = new ArrayList>(); private List current = new ArrayList(); //private List result; private List resultView; private Descender descender; //private Iterable currentChildren = null; private Iterator childIterator = null; private boolean canDescend = false; //private boolean hasNext = true; private boolean nextCalled = true; private boolean finished = false; public DescenderIterator(T base, Descender descender) { this.descender = descender; iterators.add(Collections.singleton(base).iterator()); current.add(null); resultView = Collections.unmodifiableList(current); } public static List getIndexesOfEmptySubIterables(List> iterables) { List result = new ArrayList(); for(int i = 0; i < iterables.size(); ++i) { Iterable iterable = iterables.get(i); if(Iterables.isEmpty(iterable)) { result.add(i); } } return result; } public void loadChildren() { if(current.isEmpty()) { canDescend = false; return; } if(childIterator != null) { return; } T item = current.get(current.size() - 1); childIterator = descender.getDescendCollection(item).iterator(); canDescend = childIterator.hasNext(); } public boolean canDescend() { loadChildren(); return canDescend; } /** * Set the iterator to the children of the current node. * */ public void descend() { loadChildren(); iterators.add(childIterator); current.add(null); nextCalled = true; childIterator = null; } @Override public boolean hasNext() { prepareNext(); return !finished; } private void prepareNext() { if (finished || !nextCalled) return; nextCalled = false; //adjustResultSize(); childIterator = null; // increment iterators for (int i = iterators.size() - 1; i >= 0; --i) { Iterator it = iterators.get(i); // if the iterator overflows => redo the loop and increment the // next iterator - otherwise break. if (!it.hasNext()) { if(i == 0) { finished = true; break; } iterators.remove(i); current.remove(i); } else { T item = it.next(); current.set(i, item); break; } } } @Override public List next() { prepareNext(); nextCalled = true; // TODO Potentially close the child iterator childIterator = null; return resultView; } @Override public void remove() { throw new RuntimeException("Operation not supported"); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy