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:HashMapComponentGraph.java

@Override
public final Iterator<V> getComponents() {
    // wrapping iterator
    return new Iterator<V>() {
        private final Iterator<Component> iter = componentEdges.keySet().iterator();

        public boolean hasNext() {
            return iter.hasNext();
        }/*from  ww  w  . j a  va2s.co  m*/

        @Override
        public V next() {
            return iter.next().element;
        }

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

From source file:org.apache.hama.graph.GraphJobRunner.java

public Iterable<Writable> getIterableMessages(final byte[] valuesBytes, final int numOfValues) {

    return new Iterable<Writable>() {
        DataInputStream dis;/*  ww  w .ja  va  2 s.co m*/

        @Override
        public Iterator<Writable> iterator() {
            if (!conf.getBoolean("hama.use.unsafeserialization", false)) {
                dis = new DataInputStream(new ByteArrayInputStream(valuesBytes));
            } else {
                dis = new DataInputStream(new UnsafeByteArrayInputStream(valuesBytes));
            }

            return new Iterator<Writable>() {
                int index = 0;

                @Override
                public boolean hasNext() {
                    return (index < numOfValues) ? true : false;
                }

                @Override
                public Writable next() {
                    Writable v = createVertexValue();
                    try {
                        v.readFields(dis);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    index++;
                    return v;
                }

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

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

/**
 * Returns a read-only Iterable on the keys of the Dataframe.
 *
 * @return/* ww w  .j  a  v  a2 s.c  o  m*/
 */
public Iterable<Integer> index() {
    return () -> new Iterator<Integer>() {
        private final Iterator<Integer> it = data.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:HashMapComponentGraph.java

@Override
public final Iterator<U> getEdgesInComponent(V c) {
    // get edges from component
    final Set<Pair<T>> edges = componentEdges.get(new Component(c));

    // abort if the component doesn't exist
    if (edges == null)
        return null;

    // get the edges
    final Iterator<Pair<T>> i = edges.iterator();

    // create an iterator that wraps the process of picking out the
    // edge data types from edgeData
    return new Iterator<U>() {
        @Override//  w  w w  .j av a  2 s .  com
        public boolean hasNext() {
            return i.hasNext();
        }

        @Override
        public U next() {
            if (i.hasNext()) {
                Pair<T> p = i.next();
                // return the edge data
                return edgeData.get(p);
            }
            // no element available
            return null;
        }

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

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

/**
 * Returns a read-only Iterable on the values of the Dataframe.
 *
 * @return// w  w  w . j a  v  a  2  s .c om
 */
public Iterable<Record> values() {
    return () -> new Iterator<Record>() {
        private final Iterator<Record> it = data.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:HashMapComponentGraph.java

@Override
public Iterator<T> getNodesInComponent(V c1) {
    // get edges from component
    final Set<Node> nodes = componentNodes.get(new Component(c1));

    // abort if the component doesn't exist
    if (nodes == null)
        return null;

    // get the edges
    final Iterator<Node> i = nodes.iterator();

    // create an iterator iterates the nodes, but return the T element value
    return new Iterator<T>() {
        @Override/*from  w ww  .java2s .co m*/
        public boolean hasNext() {
            return i.hasNext();
        }

        @Override
        public T next() {
            if (i.hasNext()) {
                Node p = i.next();
                // return the node data
                return p.element;
            }
            // no element available
            return null;
        }

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

}

From source file:com.cloudbees.plugins.credentials.CredentialsProvider.java

/**
 * Returns a lazy {@link Iterable} of all the {@link CredentialsStore} instances contributing credentials to the
 * supplied object.//from  w w  w.  ja v  a2  s.c om
 *
 * @param context the {@link Item} or {@link ItemGroup} or {@link User} to get the {@link CredentialsStore}s of.
 * @return a lazy {@link Iterable} of all {@link CredentialsStore} instances.
 * @since 1.8
 */
public static Iterable<CredentialsStore> lookupStores(final ModelObject context) {
    final ExtensionList<CredentialsProvider> providers = all();
    return new Iterable<CredentialsStore>() {
        public Iterator<CredentialsStore> iterator() {
            return new Iterator<CredentialsStore>() {
                private ModelObject current = context;
                private Iterator<CredentialsProvider> iterator = providers.iterator();
                private CredentialsStore next;

                public boolean hasNext() {
                    if (next != null) {
                        return true;
                    }
                    while (current != null) {
                        while (iterator.hasNext()) {
                            CredentialsProvider p = iterator.next();
                            if (!p.isEnabled(context)) {
                                continue;
                            }
                            next = p.getStore(current);
                            if (next != null) {
                                return true;
                            }
                        }
                        // now walk up the model object tree
                        // TODO make this an extension point perhaps ContextResolver could help
                        if (current instanceof Item) {
                            current = ((Item) current).getParent();
                            iterator = providers.iterator();
                        } else if (current instanceof User) {
                            Jenkins jenkins = Jenkins.getActiveInstance();
                            Authentication a;
                            if (jenkins.hasPermission(USE_ITEM) && current == User.current()) {
                                // this is the fast path for the 99% of cases
                                a = Jenkins.getAuthentication();
                            } else {
                                try {
                                    a = ((User) current).impersonate();
                                } catch (UsernameNotFoundException e) {
                                    a = null;
                                }
                            }
                            if (current == User.current() && jenkins.getACL().hasPermission(a, USE_ITEM)) {
                                current = jenkins;
                                iterator = providers.iterator();
                            } else {
                                current = null;
                            }
                        } else if (current instanceof Jenkins) {
                            // escape
                            current = null;
                        } else if (current instanceof ComputerSet) {
                            current = Jenkins.getActiveInstance();
                            iterator = providers.iterator();
                        } else if (current instanceof Computer) {
                            current = Jenkins.getActiveInstance();
                            iterator = providers.iterator();
                        } else if (current instanceof Node) {
                            current = Jenkins.getActiveInstance();
                            iterator = providers.iterator();
                        } else {
                            // fall back to Jenkins as the ultimate parent of everything else
                            current = Jenkins.getActiveInstance();
                            iterator = providers.iterator();
                        }
                    }
                    return false;
                }

                public CredentialsStore next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    try {
                        return next;
                    } finally {
                        next = null;
                    }
                }

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

From source file:org.kie.commons.java.nio.fs.jgit.JGitFileSystemProvider.java

@Override
public DirectoryStream<Path> newDirectoryStream(final Path path, final DirectoryStream.Filter<Path> pfilter)
        throws NotDirectoryException, IOException, SecurityException {
    checkNotNull("path", path);
    final DirectoryStream.Filter<Path> filter;
    if (pfilter == null) {
        filter = new DirectoryStream.Filter<Path>() {
            @Override// w w  w .  j av a 2  s .com
            public boolean accept(final Path entry) throws IOException {
                return true;
            }
        };
    } else {
        filter = pfilter;
    }

    final JGitPathImpl gPath = toPathImpl(path);

    final Pair<PathType, ObjectId> result = checkPath(gPath.getFileSystem().gitRepo(), gPath.getRefTree(),
            gPath.getPath());

    if (!result.getK1().equals(PathType.DIRECTORY)) {
        throw new NotDirectoryException(path.toString());
    }

    final List<JGitPathInfo> pathContent = listPathContent(gPath.getFileSystem().gitRepo(), gPath.getRefTree(),
            gPath.getPath());

    return new DirectoryStream<Path>() {
        boolean isClosed = false;

        @Override
        public void close() throws IOException {
            if (isClosed) {
                throw new IOException();
            }
            isClosed = true;
        }

        @Override
        public Iterator<Path> iterator() {
            if (isClosed) {
                throw new IOException();
            }
            return new Iterator<Path>() {
                private int i = -1;
                private Path nextEntry = null;
                public boolean atEof = false;

                @Override
                public boolean hasNext() {
                    if (nextEntry == null && !atEof) {
                        nextEntry = readNextEntry();
                    }
                    return nextEntry != null;
                }

                @Override
                public Path next() {
                    final Path result;
                    if (nextEntry == null && !atEof) {
                        result = readNextEntry();
                    } else {
                        result = nextEntry;
                        nextEntry = null;
                    }
                    if (result == null) {
                        throw new NoSuchElementException();
                    }
                    return result;
                }

                private Path readNextEntry() {
                    if (atEof) {
                        return null;
                    }

                    Path result = null;
                    while (true) {
                        i++;
                        if (i >= pathContent.size()) {
                            atEof = true;
                            break;
                        }

                        final JGitPathInfo content = pathContent.get(i);
                        final Path path = JGitPathImpl.create(gPath.getFileSystem(), "/" + content.getPath(),
                                gPath.getHost(), content.getObjectId(), gPath.isRealPath());
                        if (filter.accept(path)) {
                            result = path;
                            break;
                        }
                    }

                    return result;
                }

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

From source file:nl.inl.blacklab.search.HitsImpl.java

/**
 * Return an iterator over these hits that produces the
 * hits in their original order.// ww  w . ja v a 2s .  com
 *
 * @param originalOrder if true, returns hits in original order. If false,
 *   returns them in sorted order (if any)
 * @return the iterator
 */
@Override
public Iterator<Hit> getIterator(final boolean originalOrder) {
    // Construct a custom iterator that iterates over the hits in the hits
    // list, but can also take into account the Spans object that may not have
    // been fully read. This ensures we don't instantiate Hit objects for all hits
    // if we just want to display the first few.
    return new Iterator<Hit>() {

        int index = -1;

        @Override
        public boolean hasNext() {
            // Do we still have hits in the hits list?
            try {
                ensureHitsRead(index + 2);
            } catch (InterruptedException e) {
                // Thread was interrupted. Don't finish reading hits and accept possibly wrong
                // answer.
                // Client must detect the interruption and stop the thread.
                Thread.currentThread().interrupt();
            }
            return hits.size() >= index + 2;
        }

        @Override
        public Hit next() {
            // Check if there is a next, taking unread hits from Spans into account
            if (hasNext()) {
                index++;
                return hits.get((originalOrder || sortOrder == null) ? index : sortOrder[index]);
            }
            throw new NoSuchElementException();
        }

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

    };
}

From source file:org.nuclos.common.collection.CollectionUtils.java

/**
 * Wraps an Enumeration (often used in older libraries) in an iterable.
 * (Probably every library nowadays has such a wrapper...)
 *
 * @param e the enum//from   w w  w .  j  a v  a2  s.  c om
 * @return an iterable of the same type
 */
public static <T> Iterable<T> iterableEnum(final Enumeration<T> e) {
    return new Iterable<T>() {
        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                @Override
                public boolean hasNext() {
                    return e.hasMoreElements();
                }

                @Override
                public T next() {
                    return e.nextElement();
                }

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