Example usage for java.util NoSuchElementException NoSuchElementException

List of usage examples for java.util NoSuchElementException NoSuchElementException

Introduction

In this page you can find the example usage for java.util NoSuchElementException NoSuchElementException.

Prototype

public NoSuchElementException() 

Source Link

Document

Constructs a NoSuchElementException with null as its error message string.

Usage

From source file:Main.java

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
    final JavaSourceFromString jsfs = new JavaSourceFromString("code", code);
    return new Iterable<JavaSourceFromString>() {
        public Iterator<JavaSourceFromString> iterator() {
            return new Iterator<JavaSourceFromString>() {
                boolean isNext = true;

                public boolean hasNext() {
                    return isNext;
                }// ww w. ja v  a 2s . c  o m

                public JavaSourceFromString next() {
                    if (!isNext)
                        throw new NoSuchElementException();
                    isNext = false;
                    return jsfs;
                }

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

From source file:Main.java

private static <E extends Comparable<E>> E getSmallestNotNull(final List<? extends E> randomAccessList) {
    E result = null;/*from   w  w  w  . ja v a 2  s  .  c  o m*/
    E element = randomAccessList.get(0);

    for (int i = 1; i < randomAccessList.size(); i++) {
        element = randomAccessList.get(i);
        if (element == null) {
            continue;
        }
        if ((result == null) || (element.compareTo(result) < 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

private static <E extends Comparable<E>> E getGreatestNotNull(final List<? extends E> randomAccessList) {
    E result = null;//from   www. ja v  a2s.c  om
    E element = randomAccessList.get(0);

    for (int i = 1; i < randomAccessList.size(); i++) {
        element = randomAccessList.get(i);
        if (element == null) {
            continue;
        }
        if ((result == null) || (element.compareTo(result) > 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:CompileString.java

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
    final JavaSourceFromString jsfs;
    jsfs = new JavaSourceFromString("code", code);
    return new Iterable<JavaSourceFromString>() {
        public Iterator<JavaSourceFromString> iterator() {
            return new Iterator<JavaSourceFromString>() {
                boolean isNext = true;

                public boolean hasNext() {
                    return isNext;
                }/* w  w  w . ja v  a2  s.co m*/

                public JavaSourceFromString next() {
                    if (!isNext)
                        throw new NoSuchElementException();
                    isNext = false;
                    return jsfs;
                }

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

From source file:IteratorUtils.java

public static Iterator oneElementUnmodifiableIterator(final Object elem) {
    return new Iterator() {
        boolean shot = false;

        public boolean hasNext() {
            return (!shot);
        }//from   ww w  .  ja v  a 2  s  .co  m

        public Object next() {
            if (shot)
                throw new NoSuchElementException();
            else {
                shot = true;
                return elem;
            }
        }

        public void remove() {
            throw new UnsupportedOperationException("remove() not supported.");
        }
    };
}

From source file:Main.java

public static Collection<Node> createNodeCollection(final NodeList nodeList) {
    // http://www.java2s.com/Code/Java/XML/WrapNodeListtoCollection.htm
    // Written by Tomer Gabel under the Apache License Version 2.0
    return new Collection<Node>() {
        @Override//w w  w. j  a  v  a  2  s .  c o m
        public int size() {
            return nodeList.getLength();
        }

        @Override
        public boolean isEmpty() {
            return nodeList.getLength() > 0;
        }

        @Override
        public boolean contains(final Object o) {
            if (o == null || !(o instanceof Node))
                return false;
            for (int i = 0; i < nodeList.getLength(); ++i)
                if (o == nodeList.item(i))
                    return true;
            return false;
        }

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

                @Override
                public boolean hasNext() {
                    return nodeList.getLength() > this.index;
                }

                @Override
                public Node next() {
                    if (this.index >= nodeList.getLength())
                        throw new NoSuchElementException();
                    return nodeList.item(this.index++);
                }

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

        @Override
        public Object[] toArray() {
            final Node[] array = new Node[nodeList.getLength()];
            for (int i = 0; i < array.length; ++i)
                array[i] = nodeList.item(i);
            return array;
        }

        @Override
        @SuppressWarnings({ "unchecked" })
        public <T> T[] toArray(final T[] a) throws ArrayStoreException {
            if (!a.getClass().getComponentType().isAssignableFrom(Node.class))
                throw new ArrayStoreException(
                        a.getClass().getComponentType().getName() + " is not the same or a supertype of Node");

            if (a.length >= nodeList.getLength()) {
                for (int i = 0; i < nodeList.getLength(); ++i)
                    a[i] = (T) nodeList.item(i);
                if (a.length > nodeList.getLength())
                    a[nodeList.getLength()] = null;
                return a;
            }

            return (T[]) toArray();
        }

        @Override
        public boolean add(final Node node) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(final Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean containsAll(final Collection<?> c) {
            for (final Object o : c)
                if (!this.contains(o))
                    return false;
            return true;
        }

        @Override
        public boolean addAll(final Collection<? extends Node> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

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

From source file:Main.java

public static <T> Iterator<T> iteratorUnion(final Iterator<T>[] iterators) {
    return new Iterator<T>() {
        private int iteratorIndex = 0;
        private Iterator<T> current = iterators.length > 0 ? iterators[0] : null;

        public boolean hasNext() {
            for (;;) {
                if (current == null) {
                    return false;
                }/*from  w ww . j  a v  a2  s.  com*/
                if (current.hasNext()) {
                    return true;
                }
                iteratorIndex++;
                current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex];
            }
        }

        public T next() {
            for (;;) {
                if (this.current == null) {
                    throw new NoSuchElementException();
                }
                try {
                    return this.current.next();
                } catch (final NoSuchElementException nse) {
                    this.iteratorIndex++;
                    this.current = this.iteratorIndex >= iterators.length ? null
                            : iterators[this.iteratorIndex];
                }
            }
        }

        public void remove() {
            if (this.current == null) {
                throw new NoSuchElementException();
            }
            this.current.remove();
        }
    };
}

From source file:NullIterator.java

public Object next() {
    throw new NoSuchElementException();
}

From source file:Main.java

public static <T> Iterable<T> concat(final List<Iterable<? extends T>> iterables) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                Iterator<? extends T> iter;
                int pos = 0;

                public boolean hasNext() {
                    while (pos < iterables.size()) {
                        if (iter == null)
                            iter = iterables.get(pos).iterator();
                        if (iter.hasNext())
                            return true;
                        iter = null;/*from w w w  . j  a  v  a 2  s.  c  o  m*/
                        pos++;
                    }
                    return false;
                }

                public T next() {
                    while (pos < iterables.size()) {
                        if (iter == null)
                            iter = iterables.get(pos).iterator();
                        if (iter.hasNext())
                            return iter.next();
                        iter = null;
                        pos++;
                    }
                    throw new NoSuchElementException();
                }

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

From source file:Main.java

/**
 * Returns an iterator that returns up to max number of elements from the
 * Collection//w ww . ja v a 2s . c o  m
 */
private static <T> Iterator<T> iterator(final Collection<T> c, final int count) {
    return new Iterator<T>() {

        private final Iterator<T> it = c.iterator();

        private int item = 0;

        public boolean hasNext() {
            if (item >= count) {
                return false;
            }
            return it.hasNext();
        }

        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            item++;
            return it.next();
        }

        public void remove() {
            it.remove();
            item--;
        }
    };
}