Example usage for java.lang Iterable Iterable

List of usage examples for java.lang Iterable Iterable

Introduction

In this page you can find the example usage for java.lang Iterable Iterable.

Prototype

Iterable

Source Link

Usage

From source file:org.apache.jackrabbit.oak.plugins.document.util.Utils.java

private static Iterable<NodeDocument> internalGetSelectedDocuments(final DocumentStore store,
        final String indexedProperty, final long startValue) {
    return new Iterable<NodeDocument>() {
        @Override/*from w ww  .j  a v a 2  s. co  m*/
        public Iterator<NodeDocument> iterator() {
            return new AbstractIterator<NodeDocument>() {

                private static final int BATCH_SIZE = 100;
                private String startId = NodeDocument.MIN_ID_VALUE;

                private Iterator<NodeDocument> batch = nextBatch();

                @Override
                protected NodeDocument computeNext() {
                    // read next batch if necessary
                    if (!batch.hasNext()) {
                        batch = nextBatch();
                    }

                    NodeDocument doc;
                    if (batch.hasNext()) {
                        doc = batch.next();
                        // remember current id
                        startId = doc.getId();
                    } else {
                        doc = endOfData();
                    }
                    return doc;
                }

                private Iterator<NodeDocument> nextBatch() {
                    List<NodeDocument> result = indexedProperty == null
                            ? store.query(Collection.NODES, startId, NodeDocument.MAX_ID_VALUE, BATCH_SIZE)
                            : store.query(Collection.NODES, startId, NodeDocument.MAX_ID_VALUE, indexedProperty,
                                    startValue, BATCH_SIZE);
                    return result.iterator();
                }
            };
        }
    };
}

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.//ww  w . j av  a 2s.c o  m
 *
 * @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.healthmarketscience.jackcess.Cursor.java

/**
 * Returns an Iterable whose iterator() method returns the result of a call
 * to {@link #columnMatchIterator(Collection,Column,Object)}
 * @throws IllegalStateException if an IOException is thrown by one of the
 *         operations, the actual exception will be contained within
 *//*from w w  w  .j  a v a 2s .  c  o m*/
public Iterable<Map<String, Object>> columnMatchIterable(final Collection<String> columnNames,
        final Column columnPattern, final Object valuePattern) {
    return new Iterable<Map<String, Object>>() {
        public Iterator<Map<String, Object>> iterator() {
            return Cursor.this.columnMatchIterator(columnNames, columnPattern, valuePattern);
        }
    };
}

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  .ja  v a2 s.  c  o m
        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./*ww  w . j  av a 2  s.  c  om*/
 *
 * @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.apache.kylin.engine.spark.SparkCubing.java

private void writeToHFile2(final JavaPairRDD<byte[], byte[]> javaPairRDD, final String[] dataTypes,
        final int measureSize, final MeasureAggregators aggs, final byte[][] splitKeys,
        final Configuration conf, final String hFileLocation) {
    javaPairRDD.repartitionAndSortWithinPartitions(new Partitioner() {
        @Override/*from   w  ww.jav a2s.c  o  m*/
        public int numPartitions() {
            return splitKeys.length + 1;
        }

        @Override
        public int getPartition(Object key) {
            Preconditions.checkArgument(key instanceof byte[]);
            for (int i = 0, n = splitKeys.length; i < n; ++i) {
                if (UnsignedBytes.lexicographicalComparator().compare((byte[]) key, splitKeys[i]) < 0) {
                    return i;
                }
            }
            return splitKeys.length;
        }
    }, UnsignedBytes.lexicographicalComparator())
            .mapPartitions(new FlatMapFunction<Iterator<Tuple2<byte[], byte[]>>, Tuple2<byte[], byte[]>>() {
                @Override
                public Iterable<Tuple2<byte[], byte[]>> call(
                        final Iterator<Tuple2<byte[], byte[]>> tuple2Iterator) throws Exception {
                    return new Iterable<Tuple2<byte[], byte[]>>() {
                        final BufferedMeasureCodec codec = new BufferedMeasureCodec(dataTypes);
                        final Object[] input = new Object[measureSize];
                        final Object[] result = new Object[measureSize];

                        @Override
                        public Iterator<Tuple2<byte[], byte[]>> iterator() {
                            return IteratorUtils.merge(tuple2Iterator,
                                    UnsignedBytes.lexicographicalComparator(),
                                    new Function<Iterable<byte[]>, byte[]>() {
                                        @Override
                                        public byte[] call(Iterable<byte[]> v1) throws Exception {
                                            final LinkedList<byte[]> list = Lists.newLinkedList(v1);
                                            if (list.size() == 1) {
                                                return list.get(0);
                                            }
                                            aggs.reset();
                                            for (byte[] v : list) {
                                                codec.decode(ByteBuffer.wrap(v), input);
                                                aggs.aggregate(input);
                                            }
                                            aggs.collectStates(result);
                                            ByteBuffer buffer = codec.encode(result);
                                            byte[] bytes = new byte[buffer.position()];
                                            System.arraycopy(buffer.array(), buffer.arrayOffset(), bytes, 0,
                                                    buffer.position());
                                            return bytes;
                                        }
                                    });
                        }
                    };
                }
            }, true).mapToPair(new PairFunction<Tuple2<byte[], byte[]>, ImmutableBytesWritable, KeyValue>() {
                @Override
                public Tuple2<ImmutableBytesWritable, KeyValue> call(Tuple2<byte[], byte[]> tuple2)
                        throws Exception {
                    ImmutableBytesWritable key = new ImmutableBytesWritable(tuple2._1());
                    KeyValue value = new KeyValue(tuple2._1(), "F1".getBytes(), "M".getBytes(), tuple2._2());
                    return new Tuple2(key, value);
                }
            }).saveAsNewAPIHadoopFile(hFileLocation, ImmutableBytesWritable.class, KeyValue.class,
                    HFileOutputFormat.class, conf);
}

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./*  ww  w. ja  va  2 s  .c o m*/
 *
 * @return iterable that iterates over Longs instead of JsonElements.
 */
public Iterable<Integer> ints() {
    final JsonArray parent = this;
    return new Iterable<Integer>() {

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

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

                @Override
                public Integer next() {
                    return iterator.next().asInt();
                }

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

From source file:org.apache.flink.runtime.executiongraph.ExecutionGraph.java

@Override
public Iterable<ExecutionJobVertex> getVerticesTopologically() {
    // we return a specific iterator that does not fail with concurrent modifications
    // the list is append only, so it is safe for that
    final int numElements = this.verticesInCreationOrder.size();

    return new Iterable<ExecutionJobVertex>() {
        @Override//from ww w  . j av  a2 s.c o m
        public Iterator<ExecutionJobVertex> iterator() {
            return new Iterator<ExecutionJobVertex>() {
                private int pos = 0;

                @Override
                public boolean hasNext() {
                    return pos < numElements;
                }

                @Override
                public ExecutionJobVertex next() {
                    if (hasNext()) {
                        return verticesInCreationOrder.get(pos++);
                    } else {
                        throw new NoSuchElementException();
                    }
                }

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

From source file:com.healthmarketscience.jackcess.Cursor.java

/**
 * Returns an Iterable whose iterator() method returns the result of a call
 * to {@link #rowMatchIterator(Collection,Map)}
 * @throws IllegalStateException if an IOException is thrown by one of the
 *         operations, the actual exception will be contained within
 *//*from ww  w .  j av  a 2  s  .  co  m*/
public Iterable<Map<String, Object>> rowMatchIterable(final Collection<String> columnNames,
        final Map<String, ?> rowPattern) {
    return new Iterable<Map<String, Object>>() {
        public Iterator<Map<String, Object>> iterator() {
            return Cursor.this.rowMatchIterator(columnNames, rowPattern);
        }
    };
}

From source file:net.sf.maltcms.chromaui.project.spi.descriptors.CachingChromatogram2D.java

@Override
public Iterable<IScan2D> subsetByMsLevel(final short msLevel) {
    Iterable<IScan2D> iterable = new Iterable<IScan2D>() {
        @Override//from  w  w w  .j a va 2 s . com
        public Iterator<IScan2D> iterator() {
            return new Scan2DIterator(msLevel);
        }
    };
    return iterable;
}