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

/**
 * Extracts child elements from node whith type <code>ELEMENT_NODE</code>.
 *
 * @param node root node of XML document for search.
 * @return iterator with proper node childs.
 *///from   ww  w  .  j  av  a 2s. c  om
public static Iterator<Element> getChildElements(final Node node) {
    //        node.normalize();
    return new Iterator<Element>() {
        private final NodeList nodes = node.getChildNodes();
        private int nextPos = 0;
        private Element nextElement = seekNext();

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

        public Element next() {
            if (nextElement == null)
                throw new NoSuchElementException();
            final Element result = nextElement;
            nextElement = seekNext();
            return result;
        }

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

        private Element seekNext() {
            for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                final Node childNode = nodes.item(i);
                if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                    nextPos = i + 1;
                    return (Element) childNode;
                }
            }
            return null;
        }
    };
}

From source file:Main.java

public static <E extends Comparable<E>> E getSmallestNotNull(final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallestNotNull((List<? extends E>) c);
    }/*from   w w  w  . j  a v  a  2 s .c om*/

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        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

public static <E extends Comparable<E>> E getGreatestNotNull(final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatestNotNull((List<? extends E>) c);
    }//from  w ww  .j  a v a  2  s.c  o m

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        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

/**
 * INTERNAL: Gets the first object in the collection. If the
 * collection does not contain any elements NoSuchElementException
 * is thrown.<p>/*w  w w.  j av a  2 s  .  c om*/
 *
 * @since 1.3.4
 */
public static <T> T getFirstElement(Collection<T> coll) {
    if ((coll == null) || coll.isEmpty())
        throw new NoSuchElementException();
    return coll.iterator().next();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Iterable<T> concat(final Iterable<T>... collections) {
    return new Iterable<T>() {
        @Override//from w ww .j a v a2 s  .  c o  m
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                final Iterable<T>[] iterables = collections;
                int iteratorIdx = 0;
                Iterator<T> it = collections[0].iterator();

                private synchronized void advanceCursor() {
                    while (!it.hasNext() && iteratorIdx < iterables.length) {
                        iteratorIdx += 1;
                        if (iteratorIdx == iterables.length) {
                            it = new Iterator<T>() {
                                @Override
                                public boolean hasNext() {
                                    return false;
                                }

                                @Override
                                public T next() {
                                    throw new NoSuchElementException();
                                }

                                @Override
                                public void remove() {
                                    throw new RuntimeException();
                                }
                            };
                        } else {
                            it = iterables[iteratorIdx].iterator();
                        }
                    }
                }

                @Override
                public synchronized boolean hasNext() {
                    advanceCursor();
                    return iteratorIdx != iterables.length && it.hasNext();
                }

                @Override
                public synchronized T next() {
                    advanceCursor();
                    if (iteratorIdx == iterables.length)
                        throw new NoSuchElementException();
                    return it.next();
                }

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

From source file:Main.java

public static <T> Iterator<T> singletonIterator(final T item) {
    return new Iterator<T>() {
        private boolean gotItem = false;

        public boolean hasNext() {
            return !this.gotItem;
        }/* ww  w .j  ava2  s  .  c o m*/

        public T next() {
            if (this.gotItem) {
                throw new NoSuchElementException();
            }
            this.gotItem = true;
            return item;
        }

        public void remove() {
            if (!this.gotItem) {
                this.gotItem = true;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
}

From source file:EmptyIterator.java

/**
 * @return null
 */
public Object next() {
    throw new NoSuchElementException();
}

From source file:IterableString.java

public Character next() {
    if (count == str.length())
        throw new NoSuchElementException();

    count++;//from  w ww .j  av  a 2 s .  c o  m
    return str.charAt(count - 1);
}

From source file:Main.java

public static <E> E getSmallest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/* w w  w .  jav  a2s  . c  o m*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallest(comparator, (List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (comparator.compare(element, result) < 0) {
            result = element;
        }
    }

    return result;
}

From source file:Main.java

public static <E> E getGreatest(final Comparator<? super E> comparator, final Collection<? extends E> c) {
    if (c.isEmpty()) {
        throw new NoSuchElementException();
    }/*from  ww w  . j a v a2 s .com*/

    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatest(comparator, (List<? extends E>) c);
    }

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (comparator.compare(element, result) > 0) {
            result = element;
        }
    }

    return result;
}