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.datumbox.framework.common.dataobjects.Dataframe.java

/**
 * Returns a read-only Iterable on the keys of the Dataframe.
 * //from   ww  w  .jav a 2  s  .  co m
 * @return 
 */
public Iterable<Integer> index() {
    return () -> new Iterator<Integer>() {
        private final Iterator<Integer> it = records.keySet().iterator();

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

        /** {@inheritDoc} */
        @Override
        public Integer 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:gobblin.couchbase.writer.CouchbaseWriterTest.java

@Test
public void testMultiTupleDocumentWriteWithAsyncWriter()
        throws IOException, DataConversionException, ExecutionException, InterruptedException {
    final Schema dataRecordSchema = SchemaBuilder.record("Data").fields().name("data").type().bytesType()
            .noDefault().name("flags").type().intType().noDefault().endRecord();

    final Schema schema = SchemaBuilder.record("TestRecord").fields().name("key").type().stringType()
            .noDefault().name("data").type(dataRecordSchema).noDefault().endRecord();

    final int numRecords = 1000;

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

        @Override//from www  . ja v  a  2 s.  c o  m
        public void remove() {
        }

        @Override
        public boolean hasNext() {
            return (currentIndex < numRecords);
        }

        @Override
        public GenericRecord next() {
            GenericData.Record testRecord = new GenericData.Record(schema);

            String testContent = "hello world" + currentIndex;
            GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema);
            dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8"))));
            dataRecord.put("flags", 0);

            testRecord.put("key", "hello" + currentIndex);
            testRecord.put("data", dataRecord);
            currentIndex++;
            return testRecord;
        }
    };
    writeRecordsWithAsyncWriter(new TupleDocumentIterator(recordIterator));
}

From source file:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Decodes a comma seperated list of quoted strings.
 * Quotes are encoded by doubling them./*from   www  .j  a v  a  2 s.  c o  m*/
 * @param list
 * @return
 */
public static Iterator decodeDoublequotedList(final String list) {
    return new Iterator() {
        String next = null;
        int cursor = 0;

        private void cacheNext() {
            StringBuffer sb = new StringBuffer();
            char next = list.charAt(cursor);
            if (next == '"') {
                // in a quoted thing
                while (cursor < list.length()) {
                    next = list.charAt(cursor);
                    if (next == '"') {
                        cursor++;
                        next = list.charAt(cursor);
                        if (next == '"') {
                            sb.append('"');
                            cursor++;
                            continue;
                        } else {
                            cursor += 2;
                            break;
                        }
                    }
                    sb.append(next);
                    cursor++;
                }
            } else {
                // in a bare string
                while (cursor < list.length()) {
                    next = list.charAt(cursor);
                    if (next == ',') {
                        cursor++;
                        break;
                    }
                    sb.append(next);
                    cursor++;
                }
            }
            this.next = sb.toString();
        }

        public boolean hasNext() {
            if (next == null) {
                cacheNext();
            }
            return cursor < list.length();
        }

        public Object next() {
            if (!hasNext()) {
                throw new IndexOutOfBoundsException();
            }
            String r = next;
            next = null;
            return r;
        }

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

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

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

        @Override//from ww w . java2  s  .c om
        public boolean hasNext() {
            return index < size();
        }

        @Override
        public Long next() {
            return get(index++);
        }
    };
}

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 v  a 2s .co  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:com.datumbox.framework.common.dataobjects.Dataframe.java

/**
 * Returns a read-only Iterable on the values of the Dataframe.
 * /*w w  w.j a  v a2 s.c om*/
 * @return 
 */
public Iterable<Record> values() {
    return () -> new Iterator<Record>() {
        private final Iterator<Record> it = records.values().iterator();

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

        /** {@inheritDoc} */
        @Override
        public 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:org.apache.lens.cube.parse.join.AutoJoinContext.java

/**
 * There can be multiple join paths between a dimension and the target. Set of all possible join clauses is the
 * cartesian product of join paths of all dimensions
 *///w w  w  .ja va 2 s . com
private Iterator<JoinClause> getJoinClausesForAllPaths(final StorageCandidate sc, final Set<Dimension> qDims,
        final CubeQueryContext cubeql) throws LensException {
    Map<Aliased<Dimension>, List<JoinPath>> allPaths;
    // if fact is passed only look at paths possible from fact to dims
    if (sc != null) {
        allPaths = pruneFactPaths(cubeql.getCube(), sc);
    } else {
        allPaths = new LinkedHashMap<>(this.allPaths);
    }
    // prune allPaths with qdims
    pruneAllPathsWithQueriedDims(allPaths, qDims);

    // Number of paths in each path set
    final int[] groupSizes = new int[allPaths.values().size()];
    // Total number of elements in the cartesian product
    int numSamples = 1;
    // All path sets
    final List<List<JoinPath>> pathSets = new ArrayList<>();
    // Dimension corresponding to the path sets
    final List<Aliased<Dimension>> dimensions = new ArrayList<>(groupSizes.length);

    int i = 0;
    for (Map.Entry<Aliased<Dimension>, List<JoinPath>> entry : allPaths.entrySet()) {
        dimensions.add(entry.getKey());
        List<JoinPath> group = entry.getValue();
        pathSets.add(group);
        groupSizes[i] = group.size();
        numSamples *= groupSizes[i];
        i++;
    }

    final int[] selection = new int[groupSizes.length];
    final int MAX_SAMPLE_COUNT = numSamples;

    // Return a lazy iterator over all possible join chains
    return new Iterator<JoinClause>() {
        int sample = 0;

        @Override
        public boolean hasNext() {
            return sample < MAX_SAMPLE_COUNT;
        }

        @Override
        public JoinClause next() {
            Map<Aliased<Dimension>, List<TableRelationship>> chain = new LinkedHashMap<>();
            //generate next permutation.
            for (int i = groupSizes.length - 1, base = sample; i >= 0; base /= groupSizes[i], i--) {
                selection[i] = base % groupSizes[i];
            }
            for (int i = 0; i < selection.length; i++) {
                int selectedPath = selection[i];
                List<TableRelationship> path = pathSets.get(i).get(selectedPath).getEdges();
                chain.put(dimensions.get(i), path);
            }

            Set<Dimension> dimsOnPath = getDimsOnPath(chain, qDims);

            sample++;
            // Cost of join = number of tables joined in the clause
            return new JoinClause(cubeql, chain, dimsOnPath);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Cannot remove elements!");
        }
    };
}

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  w  w w.  j  a  v  a2s  . 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:org.lwes.ArrayEvent.java

@Override
public Iterator<FieldAccessor> iterator() {
    return new Iterator<FieldAccessor>() {
        private final ArrayEventFieldAccessor accessor = new ArrayEventFieldAccessor();

        public boolean hasNext() {
            return accessor.nextFieldIndex < length;
        }/*from   www  .j a  v  a2 s  . c o  m*/

        public FieldAccessor next() {
            accessor.advance();
            return accessor;
        }

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

From source file:org.openstreetmap.josm.tools.Utils.java

/**
 * Transforms the collection {@code c} into an unmodifiable collection and
 * applies the {@link Function} {@code f} on each element upon access.
 * @param <A> class of input collection
 * @param <B> class of transformed collection
 * @param c a collection// www.j  ava 2  s  .c  om
 * @param f a function that transforms objects of {@code A} to objects of {@code B}
 * @return the transformed unmodifiable collection
 */
public static <A, B> Collection<B> transform(final Collection<? extends A> c, final Function<A, B> f) {
    return new AbstractCollection<B>() {

        @Override
        public int size() {
            return c.size();
        }

        @Override
        public Iterator<B> iterator() {
            return new Iterator<B>() {

                private final Iterator<? extends A> it = c.iterator();

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

                @Override
                public B next() {
                    return f.apply(it.next());
                }

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