List of usage examples for java.util Iterator Iterator
Iterator
From source file:com.indeed.lsmtree.recordcache.PersistentRecordCache.java
/** * Performs lookup for multiple keys and returns a streaming iterator to results. * Each element in the iterator is one of * (1) an exception associated with a single lookup * (2) a key value tuple/*from w ww . j a va 2 s.c om*/ * * @param keys lookup keys * @param progress (optional) an AtomicInteger for tracking progress * @param skipped (optional) an AtomicInteger for tracking missing keys * @return iterator of lookup results */ public Iterator<Either<Exception, P2<K, V>>> getStreaming(final @Nonnull Iterator<K> keys, final @Nullable AtomicInteger progress, final @Nullable AtomicInteger skipped) { log.info("starting store lookups"); LongArrayList addressList = new LongArrayList(); int notFound = 0; while (keys.hasNext()) { final K key = keys.next(); final Long address; try { address = index.get(key); } catch (IOException e) { log.error("error", e); return Iterators.singletonIterator(Left.<Exception, P2<K, V>>of(new IndexReadException(e))); } if (address != null) { addressList.add(address); } else { notFound++; } } if (progress != null) progress.addAndGet(notFound); if (skipped != null) skipped.addAndGet(notFound); log.info("store lookups complete, sorting addresses"); final long[] addresses = addressList.elements(); Arrays.sort(addresses, 0, addressList.size()); log.info("initializing store lookup iterator"); final BlockingQueue<Runnable> taskQueue = new ArrayBlockingQueue<Runnable>(100); final Iterator<List<Long>> iterable = Iterators.partition(addressList.iterator(), 1000); final ExecutorService primerThreads = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, taskQueue, new NamedThreadFactory("store priming thread", true, log), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { taskQueue.put(r); } catch (InterruptedException e) { log.error("error", e); throw new RuntimeException(e); } } }); final BlockingQueue<List<Either<Exception, P2<K, V>>>> completionQueue = new ArrayBlockingQueue<List<Either<Exception, P2<K, V>>>>( 10); final AtomicLong runningTasks = new AtomicLong(0); final AtomicBoolean taskSubmitterRunning = new AtomicBoolean(true); new Thread(new Runnable() { @Override public void run() { while (iterable.hasNext()) { runningTasks.incrementAndGet(); final List<Long> addressesSublist = iterable.next(); primerThreads.submit(new FutureTask<List<Either<Exception, P2<K, V>>>>( new RecordLookupTask(addressesSublist)) { @Override protected void done() { try { final List<Either<Exception, P2<K, V>>> results = get(); if (progress != null) { progress.addAndGet(results.size()); } completionQueue.put(results); } catch (InterruptedException e) { log.error("error", e); throw new RuntimeException(e); } catch (ExecutionException e) { log.error("error", e); throw new RuntimeException(e); } } }); } taskSubmitterRunning.set(false); } }, "RecordLookupTaskSubmitterThread").start(); return new Iterator<Either<Exception, P2<K, V>>>() { Iterator<Either<Exception, P2<K, V>>> currentIterator; @Override public boolean hasNext() { if (currentIterator != null && currentIterator.hasNext()) return true; while (taskSubmitterRunning.get() || runningTasks.get() > 0) { try { final List<Either<Exception, P2<K, V>>> list = completionQueue.poll(1, TimeUnit.SECONDS); if (list != null) { log.debug("remaining: " + runningTasks.decrementAndGet()); currentIterator = list.iterator(); if (currentIterator.hasNext()) return true; } } catch (InterruptedException e) { log.error("error", e); throw new RuntimeException(e); } } primerThreads.shutdown(); return false; } @Override public Either<Exception, P2<K, V>> next() { return currentIterator.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:com.act.analysis.similarity.SubstructureSearch.java
public static Pair<List<String>, Iterator<Map<String, String>>> iterateOverDB(String host, Integer port, String dbName) throws Exception { MongoDB db = new MongoDB(host, port, dbName); final DBIterator iter = db.getIteratorOverChemicals(); Iterator<Map<String, String>> chemsIter = new Iterator<Map<String, String>>() { @Override//from w ww .j a v a2 s .c o m public boolean hasNext() { return iter.hasNext(); } @Override public Map<String, String> next() { Chemical c = db.getNextChemical(iter); return new HashMap<String, String>() { { put(FIELD_ID, c.getUuid().toString()); put(FIELD_INCHI, c.getInChI()); } }; } }; return Pair.of(Arrays.asList(FIELD_ID, FIELD_INCHI), chemsIter); }
From source file:CalendarUtils.java
/** * This constructs an Iterator that will start and stop over a date * range based on the focused date and the range style. For instance, * passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return * an Iterator that starts with Sunday, June 30, 2002 and ends with * Saturday, August 3, 2002.//from w ww . ja va 2 s. com */ public static Iterator getCalendarIterator(Calendar focus, int rangeStyle) { Calendar start = null; Calendar end = null; int startCutoff = Calendar.SUNDAY; int endCutoff = Calendar.SATURDAY; switch (rangeStyle) { case RANGE_MONTH_SUNDAY: case RANGE_MONTH_MONDAY: //Set start to the first of the month start = trunc(focus, Calendar.MONTH); //Set end to the last of the month end = (Calendar) start.clone(); end.add(Calendar.MONTH, 1); end.add(Calendar.DATE, -1); //Loop start back to the previous sunday or monday if (rangeStyle == RANGE_MONTH_MONDAY) { startCutoff = Calendar.MONDAY; endCutoff = Calendar.SUNDAY; } break; case RANGE_WEEK_SUNDAY: case RANGE_WEEK_MONDAY: case RANGE_WEEK_RELATIVE: case RANGE_WEEK_CENTER: //Set start and end to the current date start = trunc(focus, Calendar.DATE); end = trunc(focus, Calendar.DATE); switch (rangeStyle) { case RANGE_WEEK_SUNDAY: //already set by default break; case RANGE_WEEK_MONDAY: startCutoff = Calendar.MONDAY; endCutoff = Calendar.SUNDAY; break; case RANGE_WEEK_RELATIVE: startCutoff = focus.get(Calendar.DAY_OF_WEEK); endCutoff = startCutoff - 1; break; case RANGE_WEEK_CENTER: startCutoff = focus.get(Calendar.DAY_OF_WEEK) - 3; endCutoff = focus.get(Calendar.DAY_OF_WEEK) + 3; break; } break; default: throw new RuntimeException("The range style " + rangeStyle + " is not valid."); } if (startCutoff < Calendar.SUNDAY) { startCutoff += 7; } if (endCutoff > Calendar.SATURDAY) { endCutoff -= 7; } while (start.get(Calendar.DAY_OF_WEEK) != startCutoff) { start.add(Calendar.DATE, -1); } while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) { end.add(Calendar.DATE, 1); } final Calendar startFinal = start; final Calendar endFinal = end; Iterator it = new Iterator() { Calendar spot = null; { spot = startFinal; spot.add(Calendar.DATE, -1); } public boolean hasNext() { return spot.before(endFinal); } public Object next() { if (spot.equals(endFinal)) { throw new NoSuchElementException(); } spot.add(Calendar.DATE, 1); return spot.clone(); } public void remove() { throw new UnsupportedOperationException(); } }; return it; }
From source file:edu.brown.utils.CollectionUtil.java
public static <T> Iterable<T> iterable(final T values[]) { return (new Iterable<T>() { @Override/*from www. j av a2 s. c o m*/ public Iterator<T> iterator() { return new Iterator<T>() { private int idx = 0; @Override public boolean hasNext() { return (this.idx < values.length); } @Override public T next() { return (values[this.idx++]); } @Override public void remove() { throw new NotImplementedException(); } }; } }); }
From source file:net.sf.maltcms.chromaui.project.spi.descriptors.CachingChromatogram2D.java
/** * This iterator acts on the underlying collection of scans in * Chromatogram1D, so be careful with concurrent access / modification! *///from w w w .j a v a 2s .c om @Override public Iterator<IScan2D> iterator() { final Iterator<IScan2D> iter = new Iterator<IScan2D>() { private int currentPos = 0; @Override public boolean hasNext() { // init(); if (this.currentPos < getScans().size() - 1) { return true; } return false; } @Override public IScan2D next() { return getScan(this.currentPos++); } @Override public void remove() { throw new UnsupportedOperationException("Can not remove scans with iterator!"); } }; return iter; }
From source file:org.apache.flink.runtime.operators.DataSourceTask.java
private Iterator<InputSplit> getInputSplits() { final InputSplitProvider provider = getEnvironment().getInputSplitProvider(); return new Iterator<InputSplit>() { private InputSplit nextSplit; private boolean exhausted; @Override//from w ww. j a va 2 s.c o m public boolean hasNext() { if (exhausted) { return false; } if (nextSplit != null) { return true; } InputSplit split = provider.getNextInputSplit(); if (split != null) { this.nextSplit = split; return true; } else { exhausted = true; return false; } } @Override public InputSplit next() { if (this.nextSplit == null && !hasNext()) { throw new NoSuchElementException(); } final InputSplit tmp = this.nextSplit; this.nextSplit = null; return tmp; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:org.apache.hadoop.hive.ql.exec.persistence.AnalysisBuffer.java
public Iterator<Row> iterator() { return new Iterator<Row>() { int currentrowid = firstrowid; @Override/* w ww . j a va2 s . c o m*/ public boolean hasNext() { if (currentrowid <= lastrowid) return true; return false; } @Override public Row next() { return getByRowid(currentrowid++); } @Override public void remove() { } }; }
From source file:org.apache.sling.osgi.obr.Resource.java
private Iterator<Object> getMapListIterator(final Map<String, List<Object>> map) { return new Iterator<Object>() { private Iterator<List<Object>> mapIter; private Iterator<Object> listIter; private Object next; {/* w w w . jav a 2 s. co m*/ this.mapIter = map.values().iterator(); this.next = this.seek(); } public boolean hasNext() { return this.next != null; } public Object next() { if (this.next == null) { throw new NoSuchElementException("next"); } Object toReturn = this.next; this.next = this.seek(); return toReturn; } private Object seek() { while (this.listIter == null || !this.listIter.hasNext()) { if (this.mapIter.hasNext()) { List<Object> nextlist = this.mapIter.next(); this.listIter = nextlist.iterator(); } else { return null; } } return this.listIter.next(); } public void remove() { throw new UnsupportedOperationException("remove"); } }; }
From source file:org.apache.camel.util.ObjectHelper.java
/** * Creates an iterator over the value if the value is a collection, an * Object[] or a primitive type array; otherwise to simplify the caller's * code, we just create a singleton collection iterator over a single value * * @param value the value/*from w w w. ja va2 s. c o m*/ * @param delimiter delimiter for separating String values * @return the iterator */ @SuppressWarnings("unchecked") public static Iterator<Object> createIterator(Object value, String delimiter) { // if its a message than we want to iterate its body if (value instanceof Message) { value = ((Message) value).getBody(); } if (value == null) { return Collections.emptyList().iterator(); } else if (value instanceof Iterator) { return (Iterator) value; } else if (value instanceof Iterable) { return ((Iterable) value).iterator(); } else if (value.getClass().isArray()) { // TODO we should handle primitive array types? List<Object> list = Arrays.asList((Object[]) value); return list.iterator(); } else if (value instanceof NodeList) { // lets iterate through DOM results after performing XPaths final NodeList nodeList = (NodeList) value; return CastUtils.cast(new Iterator<Node>() { int idx = -1; public boolean hasNext() { return (idx + 1) < nodeList.getLength(); } public Node next() { idx++; return nodeList.item(idx); } public void remove() { throw new UnsupportedOperationException(); } }); } else if (value instanceof String) { final String s = (String) value; // this code is optimized to only use a Scanner if needed, eg there is a delimiter if (s.contains(delimiter)) { // use a scanner if it contains the delimiter Scanner scanner = new Scanner((String) value); scanner.useDelimiter(delimiter); return CastUtils.cast(scanner); } else { // use a plain iterator that returns the value as is as there are only a single value return CastUtils.cast(new Iterator<String>() { int idx = -1; public boolean hasNext() { // empty string should not be regarded as having next return idx + 1 == 0 && ObjectHelper.isNotEmpty(s); } public String next() { idx++; return s; } public void remove() { throw new UnsupportedOperationException(); } }); } } else { return Collections.singletonList(value).iterator(); } }
From source file:net.sf.ufsc.ServiceLoader.java
/** * Lazily loads the available providers of this loader's service. * * <p> The iterator returned by this method first yields all of the * elements of the provider cache, in instantiation order. It then lazily * loads and instantiates any remaining providers, adding each one to the * cache in turn./*from w w w . ja v a 2 s .c o m*/ * * <p> To achieve laziness the actual work of parsing the available * provider-configuration files and instantiating providers must be done by * the iterator itself. Its {@link java.util.Iterator#hasNext hasNext} and * {@link java.util.Iterator#next next} methods can therefore throw a * {@link ServiceConfigurationError} if a provider-configuration file * violates the specified format, or if it names a provider class that * cannot be found and instantiated, or if the result of instantiating the * class is not assignable to the service type, or if any other kind of * exception or error is thrown as the next provider is located and * instantiated. To write robust code it is only necessary to catch {@link * ServiceConfigurationError} when using a service iterator. * * <p> If such an error is thrown then subsequent invocations of the * iterator will make a best effort to locate and instantiate the next * available provider, but in general such recovery cannot be guaranteed. * * <blockquote style="font-size: smaller; line-height: 1.2"><span * style="padding-right: 1em; font-weight: bold">Design Note</span> * Throwing an error in these cases may seem extreme. The rationale for * this behavior is that a malformed provider-configuration file, like a * malformed class file, indicates a serious problem with the way the Java * virtual machine is configured or is being used. As such it is * preferable to throw an error rather than try to recover or, even worse, * fail silently.</blockquote> * * <p> The iterator returned by this method does not support removal. * Invoking its {@link java.util.Iterator#remove() remove} method will * cause an {@link UnsupportedOperationException} to be thrown. * * @return An iterator that lazily loads providers for this loader's * service */ public Iterator<S> iterator() { return new Iterator<S>() { Iterator<Map.Entry<String, S>> knownProviders = providers.entrySet().iterator(); public boolean hasNext() { if (knownProviders.hasNext()) return true; return lookupIterator.hasNext(); } public S next() { if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } }; }