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(String s) 

Source Link

Document

Constructs a NoSuchElementException , saving a reference to the error message string s for later retrieval by the getMessage method.

Usage

From source file:Main.java

public static <T> T third(List<T> list) {
    if (list.size() < 3) {
        throw new NoSuchElementException("Collection does not have a enough elements");
    }//w ww  . j  ava2 s  .  c  om
    return list.get(2);
}

From source file:Main.java

public static <T> T fifth(List<T> list) {
    if (list.size() < 5) {
        throw new NoSuchElementException("Collection does not have a enough elements");
    }// w  ww . j  a  va 2  s  . co m
    return list.get(4);
}

From source file:Main.java

public static <T> T fourth(List<T> list) {
    if (list.size() < 4) {
        throw new NoSuchElementException("Collection does not have a enough elements");
    }//from w ww .  j a v a2 s .  c om
    return list.get(3);
}

From source file:Main.java

public static <T> T only(Collection<T> coll) {
    if (coll.size() != 1) {
        throw new NoSuchElementException("Collection has none or more than one elements");
    }/*from w ww  .  j  a v a  2s  . c  o m*/
    return coll.iterator().next();
}

From source file:Main.java

/**
 * Returns the single element contained in a collection or a null
 * if the collection is null or empty./*w  w  w  . j a  v a  2  s .  c  o  m*/
 * 
 * @param c the collection containing a single element
 * @return Object the single element contained in the collection
 * @throws NoSuchElementException If the collection contains more than one element.
 */
public static Object getUniqueElement(Collection c) throws NoSuchElementException {
    // Return null if list is null or empty
    if (c == null || c.isEmpty())
        return null;

    // Throw an exception if collection has more than one element
    if (c.size() > 1) {
        NoSuchElementException exc = new NoSuchElementException(
                "CollectionHelper.getUniqueElement() called with a collection containing "
                        + "more than one element.");
        throw exc;
    }

    return c.iterator().next();
}

From source file:Main.java

public static Element firstChildElement(Element element, String childNameSpace, String childName)
        throws NoSuchElementException {
    NodeList childCandidateList;//w  w w  .j a va 2 s .  c o  m
    if (childNameSpace == null || childNameSpace.isEmpty()) {
        childCandidateList = element.getElementsByTagName(childName);
    } else {
        /*childCandidateList = element.getElementsByTagNameNS(childNameSpace,
          childName);*/
        childCandidateList = element.getElementsByTagNameNS(childNameSpace, childName);
    }
    if (childCandidateList.getLength() > 0) {
        Element firstChild = (Element) childCandidateList.item(0);
        return firstChild;
    } else {
        throw new NoSuchElementException("No child candidate found in this element");
    }
}

From source file:Main.java

public static int indexOf(Object[] array, Object obj) {
    for (int i = 0; i < array.length; ++i) {
        if (obj == array[i]) {
            return i;
        }//w ww. ja  v  a2 s .  co  m
    }
    throw new NoSuchElementException("" + obj);
}

From source file:Main.java

public static String getAttribute(Element element, String name) {
    String value = element.getAttribute(name);
    if (value != null && !value.isEmpty()) {
        return value;
    }//w  w  w .j  a va2 s .com
    throw new NoSuchElementException(String.format("Element <%s> has no \"%s\" attribute", element, name));
}

From source file:Main.java

public static <T> Enumeration<T> getEmptyEnumeration() {
    return new Enumeration<T>() {
        public boolean hasMoreElements() {
            return false;
        }/*w w w  .j a  v a2 s . c o  m*/

        public T nextElement() {
            throw new NoSuchElementException("Trying to get element of an empty enumeration");
        }
    };
}

From source file:Main.java

/**
 * The function getFirstChildWithTagName() returns the first child node from
 * a node, identified by its node name./*from   ww w  .j ava 2s.c o m*/
 *
 * @param data
 *            Document or Element whose children shall be examined
 * @param tagName
 *            name of the node to find
 * @return first child node with that node name
 * @throws NoSuchElementException
 *             if no child node with that name can be found
 */
public static Element getFirstChildWithTagName(Node data, String tagName) {
    for (Node element = data.getFirstChild(); element != null; element = element.getNextSibling()) {
        if (!(element instanceof Element)) {
            continue;
        }
        if (element.getNodeName().equals(tagName)) {
            return (Element) element;
        }
    }
    throw new NoSuchElementException(tagName);
}