Example usage for java.util Iterator Iterator

List of usage examples for java.util Iterator Iterator

Introduction

In this page you can find the example usage for java.util Iterator Iterator.

Prototype

Iterator

Source Link

Usage

From source file:io.druid.data.input.impl.prefetch.PrefetchSqlFirehoseFactory.java

@Override
public Firehose connect(InputRowParser<Map<String, Object>> firehoseParser, @Nullable File temporaryDirectory)
        throws IOException {
    if (objects == null) {
        objects = ImmutableList.copyOf(Preconditions.checkNotNull(initObjects(), "objects"));
    }/*from   www  .ja  v  a2s.c o  m*/
    if (cacheManager.isEnabled() || prefetchConfig.getMaxFetchCapacityBytes() > 0) {
        Preconditions.checkNotNull(temporaryDirectory, "temporaryDirectory");
        Preconditions.checkArgument(temporaryDirectory.exists(), "temporaryDirectory[%s] does not exist",
                temporaryDirectory);
        Preconditions.checkArgument(temporaryDirectory.isDirectory(),
                "temporaryDirectory[%s] is not a directory", temporaryDirectory);
    }

    LOG.info("Create a new firehose for [%d] queries", objects.size());

    // fetchExecutor is responsible for background data fetching
    final ExecutorService fetchExecutor = Execs.singleThreaded("firehose_fetch_%d");
    final Fetcher<T> fetcher = new SqlFetcher<>(cacheManager, objects, fetchExecutor, temporaryDirectory,
            prefetchConfig, new ObjectOpenFunction<T>() {
                @Override
                public InputStream open(T object, File outFile) throws IOException {
                    return openObjectStream(object, outFile);
                }

                @Override
                public InputStream open(T object) throws IOException {
                    final File outFile = File.createTempFile("sqlresults_", null, temporaryDirectory);
                    return openObjectStream(object, outFile);
                }
            });

    return new SqlFirehose(new Iterator<JsonIterator<Map<String, Object>>>() {
        @Override
        public boolean hasNext() {
            return fetcher.hasNext();
        }

        @Override
        public JsonIterator<Map<String, Object>> next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            try {
                TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>() {
                };
                final OpenedObject<T> openedObject = fetcher.next();
                final InputStream stream = openedObject.getObjectStream();
                return new JsonIterator<>(type, stream, openedObject.getResourceCloser(), objectMapper);
            } catch (Exception ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }, firehoseParser, () -> {
        fetchExecutor.shutdownNow();
        try {
            Preconditions.checkState(
                    fetchExecutor.awaitTermination(prefetchConfig.getFetchTimeout(), TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ISE("Failed to shutdown fetch executor during close");
        }
    });
}

From source file:com.act.lcms.MzMLParser.java

public Iterator<S> getIterator(String inputFile)
        throws ParserConfigurationException, IOException, XMLStreamException {
    DocumentBuilderFactory docFactory = mkDocBuilderFactory();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

    return new Iterator<S>() {
        boolean inEntry = false;

        XMLEventReader xr = xmlInputFactory.createXMLEventReader(new FileInputStream(inputFile), "utf-8");
        // TODO: is the use of the XML version/encoding tag definitely necessary?
        StringWriter w = new StringWriter().append(XML_PREAMBLE).append("\n");
        XMLEventWriter xw = xmlOutputFactory.createXMLEventWriter(w);

        S next = null;//from  w ww  .java2s .co  m

        /* Because we're handling the XML as a stream, we can only determine whether we have another Spectrum to return
         * by attempting to parse the next one.  `this.next()` reads
         */
        private S getNextSpectrum() {
            S spectrum = null;
            if (xr == null || !xr.hasNext()) {
                return null;
            }

            try {
                while (xr.hasNext()) {
                    XMLEvent e = xr.nextEvent();
                    if (!inEntry && e.isStartElement()
                            && e.asStartElement().getName().getLocalPart().equals((SPECTRUM_OBJECT_TAG))) {
                        xw.add(e);
                        inEntry = true;
                    } else if (e.isEndElement()
                            && e.asEndElement().getName().getLocalPart().equals(SPECTRUM_OBJECT_TAG)) {
                        xw.add(e);
                        xw.flush();
                        /* TODO: the XMLOutputFactory docs don't make it clear if/how events can be written directly into a new
                         * document structure, so we incur the cost of extracting each spectrum entry, serializing it, and
                         * re-reading it into its own document so it can be handled by XPath.  Master this strange corner of the
                         * Java ecosystem and get rid of <></>his doc -> string -> doc conversion. */
                        Document doc = docBuilder.parse(new ReaderInputStream(new StringReader(w.toString())));
                        spectrum = handleSpectrumEntry(doc);
                        xw.close();
                        /* Note: this can also be accomplished with `w.getBuffer().setLength(0);`, but using a new event writer
                         * seems safer. */
                        w = new StringWriter();
                        w.append(XML_PREAMBLE).append("\n");
                        xw = xmlOutputFactory.createXMLEventWriter(w);
                        inEntry = false;
                        // Don't stop parsing if handleSpectrumEntry didn't like this spectrum document.
                        if (spectrum != null) {
                            break;
                        }
                    } else if (inEntry) {
                        // Add this element if we're in an entry
                        xw.add(e);
                    }
                }

                // We've reached the end of the document; close the reader to show that we're done.
                if (!xr.hasNext()) {
                    xr.close();
                    xr = null;
                }
            } catch (Exception e) {
                // TODO: do better.  We seem to run into this sort of thing with Iterators a lot...
                throw new RuntimeException(e);
            }

            return spectrum;
        }

        private S tryParseNext() {
            // Fail the attempt if the reader is closed.
            if (xr == null || !xr.hasNext()) {
                return null;
            }

            // No checks on whether we already have a spectrum stored: we expect the callers to do that.
            return getNextSpectrum();
        }

        @Override
        public boolean hasNext() {
            // Prime the pump if the iterator doesn't have a value stored yet.
            if (this.next == null) {
                this.next = tryParseNext();
            }

            // If we have an entry waiting, return true; otherwise read the next entry and return true if successful.
            return this.next != null;
        }

        @Override
        public S next() {
            // Prime the pump like we do in hasNext().
            if (this.next == null) {
                this.next = tryParseNext();
            }

            // Take available spectrum and return it.
            S res = this.next;
            /* Advance to the next element immediately, making next() do the heavy lifting most of the time.  Otherwise,
             * the parsing will resume on hasNext(), which seems like it ought to be a light-weight operation. */
            this.next = tryParseNext();

            return res;
        }

    };
}

From source file:de.tudarmstadt.lt.lm.service.UimaStringProvider.java

public Iterator<String> applySplitHeuristic(final String text, int aggressiveness) {
    if (aggressiveness >= patterns.length)
        return new Iterator<String>() {
            int i = 0;

            @Override//from   w w  w .j  a  va  2s  .c om
            public boolean hasNext() {
                return i < text.length();
            }

            @Override
            public String next() {
                int next_i = Math.min(i + 1000, text.length());
                String result = text.substring(i, next_i);
                i = next_i;
                return result;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("remove() is not supported");
            }
        };

    final Matcher m = patterns[aggressiveness].matcher(text);
    return new Iterator<String>() {
        int last = 0;
        boolean found = false;

        @Override
        public boolean hasNext() {
            found = m.find();
            return found || last < text.length();
        }

        @Override
        public String next() {
            int current;
            if (found)
                current = m.end() - 1;
            else
                current = text.length();
            String res = text.substring(last, current);
            last = current;
            return res;

        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() is not supported");
        }
    };

}

From source file:androidx.navigation.NavGraph.java

@NonNull
@Override//from   w w w  .  j av a  2 s .c o  m
public Iterator<NavDestination> iterator() {
    return new Iterator<NavDestination>() {
        private int mIndex = -1;
        private boolean mWentToNext = false;

        @Override
        public boolean hasNext() {
            return mIndex + 1 < mNodes.size();
        }

        @Override
        public NavDestination next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            mWentToNext = true;
            return mNodes.valueAt(++mIndex);
        }

        @Override
        public void remove() {
            if (!mWentToNext) {
                throw new IllegalStateException("You must call next() before you can remove an element");
            }
            mNodes.valueAt(mIndex).setParent(null);
            mNodes.removeAt(mIndex);
            mIndex--;
            mWentToNext = false;
        }
    };
}

From source file:org.apache.hadoop.hbase.regionserver.CompoundConfiguration.java

/**
 * Add ImmutableBytesWritable map to config list. This map is generally
 * created by HTableDescriptor or HColumnDescriptor, but can be abstractly
 * used.//  www . ja va2 s  . co  m
 *
 * @param map
 *          ImmutableBytesWritable map
 * @return this, for builder pattern
 */
public CompoundConfiguration add(final Map<ImmutableBytesWritable, ImmutableBytesWritable> map) {
    freezeMutableConf();

    // put new map at the front of the list (top priority)
    this.configs.add(0, new ImmutableConfigMap() {
        Map<ImmutableBytesWritable, ImmutableBytesWritable> m = map;

        @Override
        public String get(String key) {
            ImmutableBytesWritable ibw = new ImmutableBytesWritable(Bytes.toBytes(key));
            if (!m.containsKey(ibw))
                return null;
            ImmutableBytesWritable value = m.get(ibw);
            if (value == null || value.get() == null)
                return null;
            return Bytes.toString(value.get());
        }

        @Override
        public String getRaw(String key) {
            return get(key);
        }

        @Override
        public Class<?> getClassByName(String name) throws ClassNotFoundException {
            return null;
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return m.size();
        }

        @Override
        public String toString() {
            return m.toString();
        }

        @Override
        public Iterator<Entry<String, String>> iterator() {
            final Iterator<Entry<ImmutableBytesWritable, ImmutableBytesWritable>> entries = m.entrySet()
                    .iterator();
            return new Iterator<Entry<String, String>>() {

                @Override
                public boolean hasNext() {
                    return entries.hasNext();
                }

                @Override
                public Entry<String, String> next() {
                    final Entry<ImmutableBytesWritable, ImmutableBytesWritable> e = entries.next();
                    return new Entry<String, String>() {

                        @Override
                        public String setValue(String value) {
                            throw new UnsupportedOperationException(
                                    "Cannot set value on entry from a CompoundConfiguration!");
                        }

                        @Override
                        public String getValue() {
                            ImmutableBytesWritable bytes = e.getValue();
                            // unlike regular configuration, ImmutableBytesWritableMaps can take a null value
                            if (bytes != null) {
                                return Bytes.toString(bytes.get(), bytes.getOffset(), bytes.getLength());
                            }
                            return null;
                        }

                        @Override
                        public String getKey() {
                            ImmutableBytesWritable bytes = e.getKey();
                            return Bytes.toString(bytes.get(), bytes.getOffset(), bytes.getLength());
                        }
                    };
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException(
                            "Cannot remove an entry from a CompoundConfiguration iterator");
                }
            };

        }
    });
    return this;
}

From source file:com.oracle2hsqldb.dialect.GenericDialect.java

public Iterator getSequences(DataSource dataSource, String schemaName) throws SQLException {
    return new Iterator() {
        public boolean hasNext() {
            return false;
        }// w w w .j a v a  2 s  . c  o  m

        public Object next() {
            throw new NoSuchElementException("No elements at all");
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.jboss.dashboard.workspace.PanelInstance.java

public Map<String, String> getTitle() {
    LocaleManager localeManager = LocaleManager.lookup();
    final String[] langs = localeManager.getPlatformAvailableLangs();
    Map<String, String> title = new AbstractMap<String, String>() {
        public Set<Entry<String, String>> entrySet() {
            return new AbstractSet<Entry<String, String>>() {
                public int size() {
                    return langs.length;
                }/*from  w  w w.j  av  a 2 s  .co m*/

                public Iterator<Map.Entry<String, String>> iterator() {
                    return new Iterator<Map.Entry<String, String>>() {
                        int i = 0;

                        public void remove() {
                            throw new UnsupportedOperationException();
                        }

                        public boolean hasNext() {
                            return i < langs.length;
                        }

                        public Map.Entry<String, String> next() {
                            i++;
                            return new Map.Entry<String, String>() {
                                int index = i - 1;

                                public String getKey() {
                                    return langs[index];
                                }

                                public String getValue() {
                                    return getParameterValue(PARAMETER_TITLE, langs[index]);
                                }

                                public String setValue(String value) {
                                    throw new UnsupportedOperationException();
                                }
                            };
                        }
                    };
                }
            };
        }
    };
    return title;
}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Lists all IDs currently used in the store, in ascending or descending
 * order/*  w  w  w. ja v a  2  s  . com*/
 * 
 * @see #ASCENDING
 * @see #DESCENDING
 * 
 * @param order
 *            the order in which IDs should be returned.
 * @return all IDs currently used in the store
 */
public Iterator<Integer> listIDs(final boolean order) {
    return new Iterator<Integer>() {
        /**
         * List of files or directories in store not yet handled
         */
        List<FileObject> files = new ArrayList<FileObject>();

        /**
         * The next ID to return, when 0, all IDs have been returned
         */
        int nextID;

        /**
         * The last ID that was returned
         */
        int lastID;

        /**
         * The order in which the IDs should be returned, ascending or
         * descending
         */
        boolean order;

        @Override
        public boolean hasNext() {
            return nextID > 0;
        }

        @Override
        public Integer next() {
            if (nextID < 1) {
                throw new NoSuchElementException();
            }

            lastID = nextID;
            nextID = findNextID();
            return lastID;
        }

        @Override
        public void remove() {
            if (lastID == 0) {
                throw new IllegalStateException();
            }
            try {
                MCRStore.this.delete(lastID);
            } catch (final Exception ex) {
                throw new MCRException("Could not delete " + MCRStore.this.getID() + " " + lastID, ex);
            }
            lastID = 0;
        }

        /**
         * Initializes the enumeration and searches for the first ID to
         * return
         * 
         * @param order
         *            the return order, ascending or descending
         */
        Iterator<Integer> init(final boolean order) {
            this.order = order;
            try {
                addChildren(baseDirectory);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            nextID = findNextID();
            return this;
        }

        /**
         * Adds children of the given directory to the list of files to
         * handle next. Depending on the return sort order, ascending or
         * descending file name order is used.
         * 
         * @param dir
         *            the directory thats children should be added
         * @throws FileSystemException 
         */
        private void addChildren(final FileObject dir) throws FileSystemException {
            if (dir.getType() == FileType.FOLDER) {
                final FileObject[] children = dir.getChildren();
                Arrays.sort(children, new MCRFileObjectComparator());

                for (int i = 0; i < children.length; i++) {
                    files.add(order ? i : 0, children[i]);
                }
            }
        }

        /**
         * Finds the next ID used in the store.
         * 
         * @return the next ID, or 0 if there is no other ID any more
         */
        private int findNextID() {
            if (files.isEmpty()) {
                return 0;
            }

            final FileObject first = files.remove(0);
            // checks basename length against prefix (projectId_typeId), file suffix (.xml) and configured id length
            // if they match it should be a parseable id
            if (first.getName().getBaseName().length() == idLength + prefix.length() + suffix.length()) {
                return MCRStore.this.slot2id(first.getName().getBaseName());
            }

            try {
                addChildren(first);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            return findNextID();
        }
    }.init(order);
}

From source file:org.paxml.table.csv.CsvTable.java

@Override
protected Iterator<IRow> getAllRows() {
    readRow();//from  ww  w. j  a  va 2 s .  c o m
    return new Iterator<IRow>() {

        @Override
        public boolean hasNext() {
            return row == null;
        }

        @Override
        public IRow next() {
            CsvRow r = new CsvRow(CsvTable.this);
            readRow();
            return r;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    };

}

From source file:org.apache.hadoop.yarn.server.api.records.impl.pb.NodeStatusPBImpl.java

private synchronized void addSqueezedContainersToProto() {
    maybeInitBuilder();/*ww  w.  jav a2 s.c  om*/
    builder.clearSqueezedContainers();
    if (squeezedContainers == null)
        return;
    Iterable<ContainerSqueezeUnitProto> iterable = new Iterable<ContainerSqueezeUnitProto>() {
        @Override
        public Iterator<ContainerSqueezeUnitProto> iterator() {
            return new Iterator<ContainerSqueezeUnitProto>() {

                Iterator<ContainerSqueezeUnit> iter = squeezedContainers.iterator();

                @Override
                public boolean hasNext() {
                    return iter.hasNext();
                }

                @Override
                public ContainerSqueezeUnitProto next() {
                    return convertToProtoFormat(iter.next());
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();

                }
            };

        }
    };
    builder.addAllSqueezedContainers(iterable);
}