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:com.hp.alm.ali.idea.translate.expr.Lexer.java

Lexeme next() {
    if (next == null) {
        while (pos < str.length() && Character.isWhitespace(str.charAt(pos))) {
            ++pos;//from  w  w w.jav a  2 s .co  m
        }
        if (pos > str.length()) {
            throw new NoSuchElementException();
        }
        if (pos == str.length()) {
            next = new Lexeme(pos, pos, "", Type.END);
            return next;
        }
        int startPos = pos;
        if (str.charAt(pos) == '\'' || str.charAt(pos) == '"') {
            int end = pos;
            do {
                ++end;
            } while (end < str.length() && str.charAt(end) != str.charAt(pos));
            if (end == str.length() || str.charAt(end) != str.charAt(pos)) {
                throw new ParserException("Unterminated literal", pos);
            }
            pos = end + 1;
            next = new Lexeme(startPos, end, str.substring(startPos + 1, end), Type.VALUE);
            return next;
        }
        for (Type type : Type.values()) {
            if (type.getRepr() == null) {
                continue;
            }

            if (str.substring(pos).toUpperCase().startsWith(type.getRepr())) {
                int len = type.getRepr().length();
                if (CharUtils.isAsciiAlphanumeric(type.getRepr().charAt(len - 1)) && str.length() > pos + len
                        && CharUtils.isAsciiAlphanumeric(str.charAt(pos + len))) {
                    // don't split words to match lexeme
                    continue;
                }
                next = new Lexeme(startPos, startPos + len, str.substring(startPos, startPos + len), type);
                pos += len;
                return next;
            }
        }
        do {
            ++pos;
        } while (pos < str.length() && allowedInLiteral(str.charAt(pos)));
        next = new Lexeme(startPos, pos, str.substring(startPos, pos), Type.VALUE);
    }
    return next;
}

From source file:CollectionUtilities.java

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

        public boolean hasNext() {
            for (;;) {
                if (current == null) {
                    return false;
                }/*ww  w .  ja v  a2s . co m*/
                if (current.hasNext()) {
                    return true;
                }
                iteratorIndex++;
                current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex];
            }
        }

        public Object next() {
            for (;;) {
                if (this.current == null) {
                    throw new NoSuchElementException();
                }
                try {
                    return this.current.next();
                } catch (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:com.evolveum.midpoint.repo.sql.util.ScrollableResultsIterator.java

public T next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }//from   www. j  av a  2 s  .  co  m

    hasNext = null;
    return (T) results.get(0);
}

From source file:Main.java

public static <E> E getSmallestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallestNotNull(comparator, (List<? extends E>) c);
    }/*  w  w  w  . j av a2 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) || (comparator.compare(element, result) < 0)) {
            result = element;
        }
    }

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

    return result;
}

From source file:Main.java

public static <E> E getGreatestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatestNotNull(comparator, (List<? extends E>) c);
    }//from  ww w .  ja va 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) || (comparator.compare(element, result) > 0)) {
            result = element;
        }
    }

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

    return result;
}

From source file:Main.java

/**
 * Extracts child elements with given name from node whith type <code>ELEMENT_NODE</code>.
 *
 * @param node        root node of XML document for search.
 * @param elementName name of elements that should be returned.
 * @return iterator with proper node childs.
 *///ww w  .  j a  v a2s  . c  om
public static Iterator<Element> getChildElements(final Node node, final String elementName) {
    //        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
                        && childNode.getNodeName().equals(elementName)) {
                    nextPos = i + 1;
                    return (Element) childNode;
                }
            }
            return null;
        }
    };
}

From source file:XmlUtils.java

public static Collection<Node> wrapNodeList(final NodeList nodeList) throws IllegalArgumentException {
    if (nodeList == null)
        throw new IllegalArgumentException("Cannot wrap null NodeList");

    return new Collection<Node>() {
        @Override/*w w w . ja v a 2  s  . c  om*/
        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:com.wavemaker.commons.io.FilteredIterator.java

@Override
public E next() {
    try {//from   w  w w. ja  v a2  s .c  o  m
        ensureNextHasBeenFetched();
        if (this.next == null) {
            throw new NoSuchElementException();
        }
        return this.next;
    } finally {
        this.next = null;
    }
}

From source file:SingleItemEnumeration.java

/**
 * Returns the sole item or throws a <code>NoSuchElementException</code>.
 *///from w ww . ja v  a2s.c om

public Object nextElement() {
    if (!hasMoreElements())
        throw new NoSuchElementException();

    done = true;

    Object item = this.item;
    this.item = null; // We don't want to hold a reference to it any longer than we need.
    return item;
}

From source file:com.mewmew.fairy.v1.json.SimpleJsonIterator.java

public Map<String, Object> next() {
    Map<String, Object> map = null;
    do {//from  www. j  a  va  2s  .  co m
        String line = delegate.nextLine();
        try {
            map = mapper.readValue(line, Map.class);
            return map;
        } catch (IOException e) {
        }
    } while (map == null && delegate.hasNext());
    throw new NoSuchElementException();
}