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:org.apache.druid.data.input.impl.AbstractTextFilesFirehoseFactory.java

@Override
public Firehose connect(StringInputRowParser firehoseParser, File temporaryDirectory) throws IOException {
    initializeObjectsIfNeeded();//from  w  w w  .  j a va  2 s. c o m
    final Iterator<T> iterator = objects.iterator();
    return new FileIteratingFirehose(new Iterator<LineIterator>() {
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public LineIterator next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            final T object = iterator.next();
            try {
                return IOUtils.lineIterator(wrapObjectStream(object, openObjectStream(object)),
                        StandardCharsets.UTF_8);
            } catch (Exception e) {
                LOG.error(e, "Exception reading object[%s]", object);
                throw Throwables.propagate(e);
            }
        }
    }, firehoseParser);
}

From source file:com.github.drochetti.javassist.maven.ClassnameExtractor.java

/**
 * Wrapping passed iterator (as reference) of class file names and extract full qualified class name on
 * {@link Iterator#next()}.//from w w  w  .  ja  v a 2 s . c o  m
 * <p>
 * It is possible that {@link Iterator#hasNext()} returns <code>true</code>
 * and {@link Iterator#next()} returns <code>null</code>.
 * 
 * @param parentDirectory
 * @param classFiles
 * @return iterator of full qualified class names based on passed classFiles
 *         or <code>null</code>
 * @see #extractClassNameFromFile(File, File)
 */
// DANGEROUS call by reference
public static Iterator<String> iterateClassnames(final File parentDirectory,
        final Iterator<File> classFileIterator) {
    return new Iterator<String>() {

        // @Override
        public boolean hasNext() {
            return classFileIterator == null ? false : classFileIterator.hasNext();
        }

        // @Override
        public String next() {
            final File classFile = classFileIterator.next();
            try {
                // possible returns null
                return extractClassNameFromFile(parentDirectory, classFile);
            } catch (final IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        // @Override
        public void remove() {
            classFileIterator.remove();
        }
    };
}

From source file:org.apache.hadoop.hdfs.server.blockmanagement.DatanodeStorageInfo.java

static Iterable<StorageType> toStorageTypes(final Iterable<DatanodeStorageInfo> infos) {
    return new Iterable<StorageType>() {
        @Override//from w  ww .j a va2  s.c o m
        public Iterator<StorageType> iterator() {
            return new Iterator<StorageType>() {
                final Iterator<DatanodeStorageInfo> i = infos.iterator();

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

                @Override
                public StorageType next() {
                    return i.next().getStorageType();
                }

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

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.CSVParse.java

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {//from  www.  java2 s .c om
        Map<String, Cell> m = new HashMap<String, Cell>();

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

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    final String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        int p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if (v.trim().toLowerCase().startsWith("missing ")) {
                        Object o = null;
                        Cell ri = new Cell("null", o);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    URI uri = null;
                    try {
                        uri = new URI(v);
                    } catch (Exception _e) {
                        uri = null;
                    }
                    if (uri != null) {
                        Cell ri = processURI(v);
                        if (ri != null) {
                            m.put(k, ri);
                            continue;
                        }
                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (NoSuchElementException e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

From source file:org.jsonman.node.MapNode.java

@Override
public Iterable<Node> getChildren() {
    return new Iterable<Node>() {
        @Override//from ww w.ja va  2  s.com
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {
                @Override
                public boolean hasNext() {
                    return entries.hasNext();
                }

                @Override
                public Node next() {
                    Map.Entry<String, Object> n = entries.next();
                    return NodeFactory.create(n.getValue());
                }

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

                private Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
            };
        }
    };
}

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.TSVParse.java

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {// w  ww .  j  a  v a2s . co  m
        Map<String, Cell> m = new HashMap<String, Cell>();

        final Pattern languagePattern = Pattern.compile("\"(.+)\"@(\\w\\w)$");

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

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            int p;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if ((v.trim().startsWith("<")) && (v.trim().endsWith(">"))) {
                        p = v.indexOf('<');
                        int p1 = v.indexOf('>');
                        if ((p >= 0) && (p1 > 0))
                            v = v.substring(p + 1, p1);
                        URI uri = null;
                        try {
                            uri = new URI(v);
                        } catch (Exception _e) {
                            uri = null;
                        }
                        if (uri != null) {
                            Cell ri = processURI(v);
                            if (ri != null) {
                                m.put(k, ri);
                                continue;
                            }
                        }

                        Matcher patmatch = languagePattern.matcher(v);
                        if ((patmatch.matches()) && (patmatch.groupCount() > 0)) {
                            String s = patmatch.group(1);
                            String lang = null;
                            if (patmatch.groupCount() > 1)
                                lang = patmatch.group(2);
                            if ((s != null) && (lang != null)) {
                                Cell ri = new Cell(s, lang);
                                ri.setUnparsedValue(v);
                                m.put(k, ri);
                                continue;
                            }
                        }

                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (Exception e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

From source file:tools.datasync.db2db.dao.GenericJDBCDao.java

public Iterator<JSON> selectAll(final String entityName) {

    try {//from w  w  w.  j  av a 2  s . c  o m
        String query = "select * from " + entityName;
        final Connection connection = dataSource.getConnection();
        final Statement statement = connection.createStatement();
        logger.finest("selectAll() - " + query);
        final ResultSet result = statement.executeQuery(query);

        return new Iterator<JSON>() {
            boolean last = false;
            int count = 0;

            public boolean hasNext() {
                try {
                    if (last || result.isLast()) {
                        last = true;
                        logger.finest("selectAll() - end of result set after [" + count + "] records.");
                    }
                    return (!last);
                } catch (SQLException e) {
                    exceptionHandler.handle(e, Level.INFO, "result set error - hasNext().");
                    return false;
                }
            }

            public JSON next() {
                try {
                    result.next();
                    JSON json = new JSON(entityName);
                    int count = result.getMetaData().getColumnCount();
                    for (int index = 0; index < count; index++) {
                        String columnName = result.getMetaData().getColumnName(index);
                        String value = result.getNString(index);

                        json.set(columnName, value);
                    }
                    count++;
                    logger.finest("selectAll() - returning " + entityName + " - " + json);
                    return json;
                } catch (SQLException e) {
                    exceptionHandler.handle(e, Level.INFO, "result set error - next().");
                    return null;
                } finally {
                    try {
                        if (last || result.isLast()) {
                            logger.finest("selectAll() - closing resultset");
                            last = true;
                            result.close();
                            statement.close();
                            connection.close();
                        }
                    } catch (SQLException e) {
                        exceptionHandler.handle(e, Level.INFO, "error while closing result set.");
                    }
                }
            }

            public void remove() {
                //TODO: implement;
            }
        };

    } catch (SQLException e) {
        exceptionHandler.handle(e, Level.INFO, "result set error.");
        List<JSON> jsonList = new ArrayList<JSON>();
        return jsonList.iterator();
    }
}

From source file:Utils.java

/**
 * Treat an {@link Enumeration} as an {@link Iterable} so it can be used in an enhanced for-loop.
 * Bear in mind that the enumeration is "consumed" by the loop and so should be used only once.
 * Generally it is best to put the code which obtains the enumeration inside the loop header.
 * <div class="nonnormative">/*from  w  w  w . j  ava2  s .c o  m*/
 * <p>Example of correct usage:</p>
 * <pre>
 * ClassLoader loader = ...;
 * String name = ...;
 * for (URL resource : NbCollections.iterable(loader.{@link ClassLoader#getResources getResources}(name))) {
 *     // ...
 * }
 * </pre>
 * </div>
 * @param enumeration an enumeration
 * @return an iterable wrapper which will traverse the enumeration once
 *         ({@link Iterator#remove} is not supported)
 * @throws NullPointerException if the enumeration is null
 * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6349852">Java bug #6349852</a>
 * @since org.openide.util 7.5
 */
public static <E> Iterable<E> iterable(final Enumeration<E> enumeration) {
    if (enumeration == null) {
        throw new NullPointerException();
    }
    return new Iterable<E>() {
        public Iterator<E> iterator() {
            return new Iterator<E>() {
                public boolean hasNext() {
                    return enumeration.hasMoreElements();
                }

                public E next() {
                    return enumeration.nextElement();
                }

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

From source file:org.carewebframework.common.MiscUtil.java

/**
 * Returns a list iterator that produces only collection elements of the specified type.
 *
 * @param <T> Class of collection elements.
 * @param <S> Subclass of T to be used by iterator.
 * @param collection Collection to iterate.
 * @param type Type of element to return.
 * @return An iterator.//  w w w. j a  v  a  2s  . c o m
 */
public static <T, S extends T> Iterator<S> iteratorForType(Collection<T> collection, Class<S> type) {

    return new Iterator<S>() {

        Iterator<T> iter = collection.iterator();

        S next;

        boolean needsNext = true;

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

        @Override
        public S next() {
            S result = nxt();
            needsNext = true;
            return result;
        }

        @Override
        public void remove() {
            iter.remove();
        }

        @SuppressWarnings("unchecked")
        private S nxt() {
            if (needsNext) {
                next = null;
                needsNext = false;

                while (iter.hasNext()) {
                    T nxt = iter.next();

                    if (type.isInstance(nxt)) {
                        next = (S) nxt;
                        break;
                    }
                }
            }

            return next;
        }

    };
}

From source file:com.ikanow.aleph2.analytics.services.DeduplicationService.java

@Override
public void onObjectBatch(final Stream<Tuple2<Long, IBatchRecord>> batch, final Optional<Integer> batch_size,
        final Optional<JsonNode> grouping_key) {
    if (_deduplication_is_disabled.get()) {
        // no deduplication, generally shouldn't be here...
        //.. but if we are, make do the best we can
        batch.forEach(t2 -> _context.get().emitImmutableObject(t2._1(), t2._2().getJson(), Optional.empty(),
                Optional.empty(), Optional.empty()));
        return;/*from   w ww.  j a  va 2s  . c  om*/
    }

    // Create big query

    final Tuple3<QueryComponent<JsonNode>, List<Tuple2<JsonNode, Tuple2<Long, IBatchRecord>>>, Either<String, List<String>>> fieldinfo_dedupquery_keyfields = getDedupQuery(
            batch, _dedup_fields.get(), _db_mapper.get());

    // Get duplicate results

    final Tuple2<List<String>, Boolean> fields_include = getIncludeFields(_policy.get(), _dedup_fields.get(),
            _timestamp_field.get());

    final CompletableFuture<Iterator<JsonNode>> dedup_res = fieldinfo_dedupquery_keyfields._2().isEmpty()
            ? CompletableFuture.completedFuture(Collections.<JsonNode>emptyList().iterator())
            : _dedup_context.get().getObjectsBySpec(fieldinfo_dedupquery_keyfields._1(), fields_include._1(),
                    fields_include._2()).thenApply(cursor -> cursor.iterator());

    // Wait for it to finsh

    //(create handy results structure if so)
    final LinkedHashMap<JsonNode, LinkedList<Tuple3<Long, IBatchRecord, ObjectNode>>> mutable_obj_map = fieldinfo_dedupquery_keyfields
            ._2().stream()
            .collect(Collector.of(
                    () -> new LinkedHashMap<JsonNode, LinkedList<Tuple3<Long, IBatchRecord, ObjectNode>>>(),
                    (acc, t2) -> {
                        // (ie only the first element is added, duplicate elements are removed)
                        final Tuple3<Long, IBatchRecord, ObjectNode> t3 = Tuples._3T(t2._2()._1(), t2._2()._2(),
                                _mapper.createObjectNode());
                        acc.compute(t2._1(), (k, v) -> {
                            final LinkedList<Tuple3<Long, IBatchRecord, ObjectNode>> new_list = (null == v)
                                    ? new LinkedList<>()
                                    : v;
                            new_list.add(t3);
                            return new_list;
                        });
                    }, (map1, map2) -> {
                        map1.putAll(map2);
                        return map1;
                    }));

    //TODO (ALEPH-20): add timestamps to annotation
    //TODO (ALEPH-20): support different timestamp fields for the different buckets
    //TODO (ALEPH-20): really need to support >1 current enrichment job 
    //                 ^^(Really really longer term you should be able to decide what objects you want and what you don't  <- NOTE: don't remember what i meant here)

    final Iterator<JsonNode> cursor = dedup_res.join();

    // Handle the results

    final Stream<JsonNode> records_to_delete = Lambdas.get(() -> {
        if (isCustom(_doc_schema.get().deduplication_policy())
                || _doc_schema.get().delete_unhandled_duplicates()) {
            return Optionals.streamOf(cursor, true)
                    .collect(Collectors.groupingBy(
                            ret_obj -> getKeyFieldsAgain(ret_obj, fieldinfo_dedupquery_keyfields._3())))
                    .entrySet().stream().<JsonNode>flatMap(kv -> {

                        final Optional<JsonNode> maybe_key = kv.getKey();
                        final Optional<LinkedList<Tuple3<Long, IBatchRecord, ObjectNode>>> matching_records = maybe_key
                                .map(key -> mutable_obj_map.get(key));

                        // Stats:
                        _mutable_stats.duplicate_keys++;
                        _mutable_stats.duplicates_existing += kv.getValue().size();
                        _mutable_stats.duplicates_incoming += matching_records.map(l -> l.size()).orElse(0);

                        //DEBUG
                        //System.out.println("?? " + kv.getValue().size() + " vs " + maybe_key + " vs " + matching_records.map(x -> Integer.toString(x.size())).orElse("(no match)"));

                        return matching_records
                                .<Stream<JsonNode>>map(records -> handleDuplicateRecord(_doc_schema.get(),
                                        _custom_handler.optional().map(
                                                handler -> Tuples._2T(handler, this._custom_context.get())),
                                        _timestamp_field.get(), records, kv.getValue(), maybe_key.get(),
                                        mutable_obj_map))
                                .orElse(Stream.empty());
                    });
        } else {
            Optionals.streamOf(cursor, true).forEach(ret_obj -> {
                final Optional<JsonNode> maybe_key = getKeyFieldsAgain(ret_obj,
                        fieldinfo_dedupquery_keyfields._3());
                final Optional<LinkedList<Tuple3<Long, IBatchRecord, ObjectNode>>> matching_records = maybe_key
                        .map(key -> mutable_obj_map.get(key));

                //DEBUG
                //System.out.println("?? " + ret_obj + " vs " + maybe_key + " vs " + matching_record.map(x -> x._2().getJson().toString()).orElse("(no match)"));

                // Stats:
                _mutable_stats.duplicate_keys++;
                _mutable_stats.duplicates_existing++;
                _mutable_stats.duplicates_incoming += matching_records.map(l -> l.size()).orElse(0);

                matching_records.ifPresent(records -> handleDuplicateRecord(_doc_schema.get(),
                        _custom_handler.optional()
                                .map(handler -> Tuples._2T(handler, this._custom_context.get())),
                        _timestamp_field.get(), records, Arrays.asList(ret_obj), maybe_key.get(),
                        mutable_obj_map));
            });
            return Stream.<JsonNode>empty();
        }
    });

    final List<Object> ids = records_to_delete.map(j -> jsonToObject(j)).filter(j -> null != j)
            .collect(Collectors.toList());

    if (!ids.isEmpty()) { // fire a bulk deletion request
        mutable_uncompleted_deletes.add(
                _dedup_context.get().deleteObjectsBySpec(CrudUtils.allOf().withAny(AnnotationBean._ID, ids)));

        _mutable_stats.deleted += ids.size();

        //(quickly see if we can reduce the number of outstanding requests)
        final Iterator<CompletableFuture<Long>> it = mutable_uncompleted_deletes.iterator();
        while (it.hasNext()) {
            final CompletableFuture<Long> cf = it.next();
            if (cf.isDone()) {
                it.remove();
            } else
                break; // ie stop as soon as we hit one that isn't complete)
        }
    }

    _mutable_stats.nonduplicate_keys += mutable_obj_map.size();

    if (Optional.ofNullable(_doc_schema.get().custom_finalize_all_objects()).orElse(false)) {
        mutable_obj_map.entrySet().stream()
                .forEach(kv -> handleCustomDeduplication(
                        _custom_handler.optional()
                                .map(handler -> Tuples._2T(handler, this._custom_context.get())),
                        kv.getValue(), Collections.emptyList(), kv.getKey()));
    } else { // Just emit the last element of each grouped object set
        mutable_obj_map.values().stream().map(t -> t.peekLast())
                .forEach(t -> _context.get().emitImmutableObject(t._1(), t._2().getJson(), Optional.of(t._3()),
                        Optional.empty(), Optional.empty()));
    }
}