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.pig.backend.hadoop.executionengine.HJob.java

@Override
public Iterator<Tuple> getResults() throws ExecException {
    final LoadFunc p;

    try {//w ww . j  a v  a2 s  .c  o m
        LoadFunc originalLoadFunc = (LoadFunc) PigContext.instantiateFuncFromSpec(outFileSpec.getFuncSpec());

        p = (LoadFunc) new ReadToEndLoader(originalLoadFunc,
                ConfigurationUtil.toConfiguration(pigContext.getProperties()), outFileSpec.getFileName(), 0);

    } catch (Exception e) {
        int errCode = 2088;
        String msg = "Unable to get results for: " + outFileSpec;
        throw new ExecException(msg, errCode, PigException.BUG, e);
    }

    return new Iterator<Tuple>() {
        Tuple t;
        boolean atEnd;

        @Override
        public boolean hasNext() {
            if (atEnd)
                return false;
            try {
                if (t == null)
                    t = p.getNext();
                if (t == null)
                    atEnd = true;
            } catch (Exception e) {
                log.error(e);
                t = null;
                atEnd = true;
                throw new Error(e);
            }
            return !atEnd;
        }

        @Override
        public Tuple next() {
            Tuple next = t;
            if (next != null) {
                t = null;
                return next;
            }
            try {
                next = p.getNext();
            } catch (Exception e) {
                log.error(e);
            }
            if (next == null)
                atEnd = true;
            return next;
        }

        @Override
        public void remove() {
            throw new RuntimeException("Removal not supported");
        }

    };
}

From source file:hudson.matrix.AxisList.java

/**
 * List up all the possible combinations of this list.
 *//*from   w  w  w  .  j a  v  a 2 s . c  om*/
public Iterable<Combination> list() {
    final int[] base = new int[size()];
    if (base.length == 0)
        return Collections.<Combination>emptyList();

    int b = 1;
    for (int i = size() - 1; i >= 0; i--) {
        base[i] = b;
        b *= get(i).size();
    }

    final int total = b; // number of total combinations

    return new Iterable<Combination>() {
        public Iterator<Combination> iterator() {
            return new Iterator<Combination>() {
                private int counter = 0;

                public boolean hasNext() {
                    return counter < total;
                }

                public Combination next() {
                    String[] data = new String[size()];
                    int x = counter++;
                    for (int i = 0; i < data.length; i++) {
                        data[i] = get(i).value(x / base[i]);
                        x %= base[i];
                    }
                    assert x == 0;
                    return new Combination(AxisList.this, data);
                }

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

From source file:hudson.model.RunMap.java

/**
 * Walks through builds, newer ones first.
 *///from w w  w  .  ja v a 2s. c  o  m
public Iterator<R> iterator() {
    return new Iterator<R>() {
        R last = null;
        R next = newestBuild();

        public boolean hasNext() {
            return next != null;
        }

        public R next() {
            last = next;
            if (last != null)
                next = last.getPreviousBuild();
            else
                throw new NoSuchElementException();
            return last;
        }

        public void remove() {
            if (last == null)
                throw new UnsupportedOperationException();
            removeValue(last);
        }
    };
}

From source file:Iterators.java

/**
 * Returns an unmodifiable view of the specified iterator.
 * Calls to {@code next()} and {@code hasNext()} are delegated
 * to the specified iterator as made.  Calls to {@code remove()}
 * throw unsupported operation exceptions.
 *
 * @param it Iterator to view./*ww  w  .  ja va  2  s .  c  om*/
 * @return Unmodifiable view of specified iterator.
 */
public static <E> Iterator<E> unmodifiable(Iterator<E> it) {
    final Iterator<E> mIt = it;
    return new Iterator<E>() {
        public boolean hasNext() {
            return mIt.hasNext();
        }

        public E next() {
            return mIt.next();
        }

        public void remove() {
            String msg = "Cannot remove from an unmodifiable iterator.";
            throw new UnsupportedOperationException(msg);
        }
    };
}

From source file:hudson.util.CopyOnWriteList.java

/**
 * Returns an iterator./* w  ww  . j  a va2s  . co m*/
 */
public Iterator<E> iterator() {
    final Iterator<? extends E> itr = core.iterator();
    return new Iterator<E>() {
        private E last;

        public boolean hasNext() {
            return itr.hasNext();
        }

        public E next() {
            return last = itr.next();
        }

        public void remove() {
            CopyOnWriteList.this.remove(last);
        }
    };
}

From source file:com.clxcommunications.xms.PagedFetcher.java

/**
 * Returns an iterable object that traverses all fetched elements across all
 * associated pages. This is done by iterating over fetched pages and, when
 * necessary, fetching new pages.//from   ww  w .  ja v  a2 s .  c  o m
 * <p>
 * Since multiple fetches may be necessary to iterate over all batches it is
 * possible that concurrent changes on the server will cause the same batch
 * to be iterated over twice.
 * <p>
 * ALso, since the returned iterator will perform asynchronous network
 * traffic it is possible that the {@link Iterator#hasNext()} and
 * {@link Iterator#next()} methods throws {@link RuntimeException} having as
 * cause an {@link ExecutionException}.
 * 
 * @return a non-null iterable
 * @throws RuntimeException
 *             if the background page fetching failed
 */
@Nonnull
public Iterable<T> elements() {

    return new Iterable<T>() {

        @Override
        public Iterator<T> iterator() {

            final Iterator<Page<T>> pageIt = pages().iterator();

            return new Iterator<T>() {

                Iterator<T> pageElemIt = pageIt.next().iterator();

                @Override
                public boolean hasNext() {
                    if (!pageElemIt.hasNext()) {
                        if (!pageIt.hasNext()) {
                            return false;
                        } else {
                            pageElemIt = pageIt.next().iterator();
                            return pageElemIt.hasNext();
                        }
                    } else {
                        return true;
                    }
                }

                @Override
                public T next() {
                    if (!pageElemIt.hasNext()) {
                        pageElemIt = pageIt.next().iterator();
                    }

                    return pageElemIt.next();
                }

            };

        }

    };
}

From source file:com.conwet.silbops.model.Advertise.java

/**
 * @throws UnsupportedOperationException since there is no real need to
 * duplicate elements.//from  www  . j ava2s. c  om
 */
@Override
public Set<Entry<Attribute, Attribute>> entries() {

    return new AbstractSet<Entry<Attribute, Attribute>>() {

        @Override
        public Iterator<Entry<Attribute, Attribute>> iterator() {

            final Iterator<Attribute> iterator = attributes.iterator();

            return new Iterator<Entry<Attribute, Attribute>>() {

                @Override
                public boolean hasNext() {

                    return iterator.hasNext();
                }

                @Override
                public Entry<Attribute, Attribute> next() {

                    Attribute attribute = iterator.next();

                    return new SimpleImmutableEntry<>(attribute, attribute);
                }

                @Override
                public void remove() {

                    throw new UnsupportedOperationException("Read-only iterator.");
                }
            };
        }

        @Override
        public int size() {

            return attributes.size();
        }
    };
}

From source file:de.tudarmstadt.ukp.dkpro.core.performance.PerformanceTestUtil.java

public static <T> Iterable<T> repeat(final T aObject, final int aCount) {
    return new Iterable<T>() {
        @Override/*from   ww  w . j a v a2s.c om*/
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                private int i = 0;

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

                @Override
                public T next() {
                    i++;
                    return aObject;
                }

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

From source file:MultiMap.java

public Set entrySet() {
    int size = 0;
    Iterator iterKeys = map.entrySet().iterator();
    while (iterKeys.hasNext()) {
        Map.Entry entry = (Map.Entry) iterKeys.next();
        Collection values = (Collection) entry.getValue();
        Iterator iterValues = values.iterator();
        while (iterValues.hasNext()) {
            size++;/*from  w ww  .  ja  v a 2 s . c o m*/
            iterValues.next();
        }
    }

    final int finalSize = size;

    final Iterator entries = map.entrySet().iterator();

    return new AbstractSet() {
        int pos = 0;
        Map.Entry entry;
        Iterator values;

        public Iterator iterator() {
            return new Iterator() {
                public void remove() {
                    throw new UnsupportedOperationException();
                }

                public boolean hasNext() {
                    return pos != finalSize;
                }

                public Object next() {
                    while (true) {
                        if (entry == null) {
                            entry = (Map.Entry) entries.next();
                            values = ((Collection) entry.getValue()).iterator();
                        }
                        Object key = entry.getKey();
                        if (values.hasNext()) {
                            Object value = values.next();
                            pos++;
                            return new Entry(key, value);
                        } else {
                            entry = null;
                        }
                    }
                }
            };
        }

        public int size() {
            return finalSize;
        }
    };
}

From source file:de.codesourcery.jasm16.ast.ASTUtils.java

public static Iterator<ASTNode> createDepthFirst(ASTNode node) {
    final Stack<ASTNode> stack = new Stack<ASTNode>();
    if (node != null) {
        stack.push(node);//from   w  w  w .j a va2  s  .co m
    }
    return new Iterator<ASTNode>() {

        @Override
        public boolean hasNext() {
            return !stack.isEmpty();
        }

        @Override
        public ASTNode next() {
            ASTNode n = stack.peek();
            for (ASTNode child : n.getChildren()) {
                stack.push(child);
            }
            return stack.pop();
        }

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