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:com.github.jsonj.JsonArray.java

/**
 * Convenience method to prevent casting JsonElement to String when iterating in the common case that you have
 * an array of strings./*from  ww  w .ja  v  a 2  s  .co m*/
 *
 * @return iterable that iterates over Strings instead of JsonElements.
 */
public Iterable<String> strings() {
    final JsonArray parent = this;
    return new Iterable<String>() {

        @Override
        public Iterator<String> iterator() {
            final Iterator<JsonElement> iterator = parent.iterator();
            return new Iterator<String>() {

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

                @Override
                public String next() {
                    return iterator.next().asString();
                }

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

From source file:com.quinsoft.zeidon.standardoe.EntityInstanceImpl.java

Iterable<AttributeDef> getNonNullAttributeList() {
    return new Iterable<AttributeDef>() {
        @Override/*w  ww  .  jav  a 2s.  c om*/
        public Iterator<AttributeDef> iterator() {
            return new Iterator<AttributeDef>() {
                private int attributeNumber = -1;
                private int nextAttributeNumber = -1;

                @Override
                public boolean hasNext() {
                    if (nextAttributeNumber <= attributeNumber) {
                        nextAttributeNumber = attributeNumber + 1;
                        while (nextAttributeNumber < getEntityDef().getAttributeCount()) {
                            AttributeDef attributeDef = getEntityDef().getAttribute(nextAttributeNumber);
                            AttributeValue attrib = getInternalAttribute(attributeDef);
                            if (!attrib.isNull(getTask(), attributeDef) || attrib.isUpdated())
                                break;

                            nextAttributeNumber++;
                        }
                    }

                    return nextAttributeNumber < getEntityDef().getAttributeCount();
                }

                @Override
                public AttributeDef next() {
                    attributeNumber = nextAttributeNumber;
                    return getEntityDef().getAttribute(attributeNumber);
                }

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

From source file:gobblin.couchbase.writer.CouchbaseWriterTest.java

@Test
public void testMultiJsonDocumentWriteWithAsyncWriter()
        throws IOException, DataConversionException, ExecutionException, InterruptedException {

    final int numRecords = 1000;

    Iterator<Object> recordIterator = new Iterator<Object>() {
        private int currentIndex;

        @Override/* w w w.  j  av  a 2s. co  m*/
        public boolean hasNext() {
            return (currentIndex < numRecords);
        }

        @Override
        public Object next() {

            String testContent = "hello world" + currentIndex;
            String key = "hello" + currentIndex;
            HashMap<String, String> contentMap = new HashMap<>();
            contentMap.put("key", key);
            contentMap.put("value", testContent);
            currentIndex++;
            return contentMap;
        }

        @Override
        public void remove() {
        }

    };
    writeRecordsWithAsyncWriter(new JsonDocumentIterator(recordIterator));
}

From source file:com.github.jsonj.JsonArray.java

/**
 * Convenience method to prevent casting JsonElement to Double when iterating in the common case that you have
 * an array of doubles.//from  ww w . j ava 2 s . c om
 *
 * @return iterable that iterates over Doubles instead of JsonElements.
 */
public Iterable<Double> doubles() {
    final JsonArray parent = this;
    return new Iterable<Double>() {

        @Override
        public Iterator<Double> iterator() {
            final Iterator<JsonElement> iterator = parent.iterator();
            return new Iterator<Double>() {

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

                @Override
                public Double next() {
                    return iterator.next().asDouble();
                }

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

From source file:com.simiacryptus.mindseye.lang.Tensor.java

/**
 * Coord stream stream./*from  ww w  .jav a2 s .  com*/
 *
 * @param parallel the safe
 * @return the stream
 */
@Nonnull
public Stream<Coordinate> coordStream(boolean parallel) {
    //ConcurrentHashSet<Object> distinctBuffer = new ConcurrentHashSet<>();
    //assert distinctBuffer.add(coordinate.copy()) : String.format("Duplicate: %s in %s", coordinate, distinctBuffer);
    return StreamSupport.stream(Spliterators.spliterator(new Iterator<Coordinate>() {

        int cnt = 0;
        @Nonnull
        Coordinate coordinate = new Coordinate();
        @Nonnull
        int[] val = new int[dimensions.length];
        @Nonnull
        int[] safeCopy = new int[dimensions.length];

        @Override
        public boolean hasNext() {
            return cnt < length();
        }

        @Nonnull
        @Override
        public synchronized Coordinate next() {
            if (0 < cnt) {
                for (int i = 0; i < val.length; i++) {
                    if (++val[i] >= dimensions[i]) {
                        val[i] = 0;
                    } else {
                        break;
                    }
                }
            }
            System.arraycopy(val, 0, safeCopy, 0, val.length);
            coordinate.setIndex(cnt++);
            coordinate.setCoords(safeCopy);
            return parallel ? coordinate.copy() : coordinate;
        }
    }, length(), Spliterator.ORDERED), parallel);
}

From source file:com.act.lcms.v2.TraceIndexExtractor.java

public Iterator<Pair<Double, List<XZ>>> getIteratorOverTraces(File index) throws IOException, RocksDBException {
    RocksDBAndHandles<COLUMN_FAMILIES> dbAndHandles = DBUtil.openExistingRocksDB(index,
            COLUMN_FAMILIES.values());//from  w  w w .  ja  v a  2s  .  co m
    final RocksDBAndHandles.RocksDBIterator rangesIterator = dbAndHandles
            .newIterator(COLUMN_FAMILIES.TARGET_TO_WINDOW);

    rangesIterator.reset();

    final List<Double> times;
    try {
        byte[] timeBytes = dbAndHandles.get(COLUMN_FAMILIES.TIMEPOINTS, TIMEPOINTS_KEY);
        times = deserializeDoubleList(timeBytes);
    } catch (RocksDBException e) {
        LOGGER.error("Caught RocksDBException when trying to fetch times: %s", e.getMessage());
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOGGER.error("Caught IOException when trying to fetch timese %s", e.getMessage());
        throw new UncheckedIOException(e);
    }

    return new Iterator<Pair<Double, List<XZ>>>() {
        int windowNum = 0;

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

        @Override
        public Pair<Double, List<XZ>> next() {
            byte[] valBytes = rangesIterator.value();
            MZWindow window;
            windowNum++;
            try {
                window = deserializeObject(valBytes);
            } catch (IOException e) {
                LOGGER.error("Caught IOException when iterating over mz windows (%d): %s", windowNum,
                        e.getMessage());
                throw new UncheckedIOException(e);
            } catch (ClassNotFoundException e) {
                LOGGER.error("Caught ClassNotFoundException when iterating over mz windows (%d): %s", windowNum,
                        e.getMessage());
                throw new RuntimeException(e);
            }

            byte[] traceKeyBytes;
            try {
                traceKeyBytes = serializeObject(window.getIndex());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }

            List<Double> trace;
            try {
                byte[] traceBytes = dbAndHandles.get(COLUMN_FAMILIES.ID_TO_TRACE, traceKeyBytes);
                if (traceBytes == null) {
                    String msg = String.format("Got null byte array back for trace key %d (target: %.6f)",
                            window.getIndex(), window.getTargetMZ());
                    LOGGER.error(msg);
                    throw new RuntimeException(msg);
                }
                trace = deserializeDoubleList(traceBytes);
            } catch (RocksDBException e) {
                LOGGER.error("Caught RocksDBException when trying to extract trace %d (%.6f): %s",
                        window.getIndex(), window.getTargetMZ(), e.getMessage());
                throw new RuntimeException(e);
            } catch (IOException e) {
                LOGGER.error("Caught IOException when trying to extract trace %d (%.6f): %s", window.getIndex(),
                        window.getTargetMZ(), e.getMessage());
                throw new UncheckedIOException(e);
            }

            if (trace.size() != times.size()) {
                LOGGER.error("Found mismatching trace and times size (%d vs. %d), continuing anyway",
                        trace.size(), times.size());
            }

            List<XZ> xzs = new ArrayList<>(times.size());
            for (int i = 0; i < trace.size() && i < times.size(); i++) {
                xzs.add(new XZ(times.get(i), trace.get(i)));
            }

            /* The Rocks iterator pattern is a bit backwards from the Java model, as we don't need an initial next() call
             * to prime the iterator, and `isValid` indicates whether we've gone past the end of the iterator.  We thus
             * advance only after we've read the current value, which means the next hasNext call after we've walked off the
             * edge will return false. */
            rangesIterator.next();
            return Pair.of(window.getTargetMZ(), xzs);
        }
    };
}

From source file:com.datumbox.framework.common.dataobjects.Dataframe.java

/**
 * Returns a read-only Iterable on the keys and Records of the Dataframe.
 * /*from w w  w  . ja v  a  2s.  co m*/
 * @return 
 */
public Iterable<Map.Entry<Integer, Record>> entries() {
    return () -> new Iterator<Map.Entry<Integer, Record>>() {
        private final Iterator<Map.Entry<Integer, Record>> it = records.entrySet().iterator();

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

        /** {@inheritDoc} */
        @Override
        public Map.Entry<Integer, Record> next() {
            return it.next();
        }

        /** {@inheritDoc} */
        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "This is a read-only iterator, remove operation is not supported.");
        }
    };
}

From source file:jetbrains.exodus.env.Reflect.java

public void spaceInfoFromUtilization() {
    spaceInfo(new Iterable<Map.Entry<Long, Long>>() {
        private final long[] addresses = env.getLog().getAllFileAddresses();

        @Override//from ww w . j  ava 2  s.  com
        public Iterator<Map.Entry<Long, Long>> iterator() {
            return new Iterator<Map.Entry<Long, Long>>() {
                private int i = addresses.length - 1;

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

                @Override
                public Map.Entry<Long, Long> next() {
                    final long key = addresses[i--];
                    final long fileFreeBytes = env.getGC().getFileFreeBytes(key);
                    final Long value = fileFreeBytes == Long.MAX_VALUE ? null
                            : log.getFileSize(key) - fileFreeBytes;
                    return new Map.Entry<Long, Long>() {
                        @Override
                        public Long getKey() {
                            return key;
                        }

                        @Override
                        public Long getValue() {
                            return value;
                        }

                        @Override
                        public Long setValue(Long value) {
                            throw new UnsupportedOperationException();
                        }
                    };
                }

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

From source file:com.github.jsonj.JsonArray.java

/**
 * Convenience method to prevent casting JsonElement to Long when iterating in the common case that you have
 * an array of longs.// w  w  w.  j av  a2s .c o m
 *
 * @return iterable that iterates over Longs instead of JsonElements.
 */
public Iterable<Long> longs() {
    final JsonArray parent = this;
    return new Iterable<Long>() {

        @Override
        public Iterator<Long> iterator() {
            final Iterator<JsonElement> iterator = parent.iterator();
            return new Iterator<Long>() {

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

                @Override
                public Long next() {
                    return iterator.next().asLong();
                }

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

From source file:org.briljantframework.array.AbstractComplexArray.java

@Override
public Iterator<Complex> iterator() {
    return new Iterator<Complex>() {
        private int current = 0;

        @Override/*from   www . jav  a2  s . c o  m*/
        public boolean hasNext() {
            return current < size();
        }

        @Override
        public Complex next() {
            return get(current++);
        }
    };
}