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

/**
 * Gets one element from the provided Collection. If no element is present,
 * an Exception is thrown. No guarantee is made as to which element is
 * returned from time to time.//from  w w  w  .j  ava  2  s  .  com
 *
 * @param <T> Collection type
 * @param collection to use
 * @return one element from the provided Collection
 * @throws NoSuchElementException if no elements are present in the
 * Collection
 */
public static <T> T getAnyFrom(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    }
    throw new NoSuchElementException("No value present");
}

From source file:com.liferay.blade.cli.util.Prompter.java

public static boolean confirm(String question, InputStream in, PrintStream out,
        Optional<Boolean> defaultAnswer) {
    String questionWithPrompt = _buildQuestionWithPrompt(question, defaultAnswer);

    Optional<Boolean> answer = _getBooleanAnswer(questionWithPrompt, in, out, defaultAnswer);

    if (answer.isPresent()) {
        return answer.get();
    } else {/* w  w  w.  j a  v  a  2  s. co  m*/
        throw new NoSuchElementException("Unable to acquire an answer");
    }
}

From source file:Main.java

/**
 * This method does the same as {@link #moveLeft(int, Object, List)} but it moves the specified element
 * to the right instead of the left./*from ww w. j  av a  2 s . co m*/
 */
public static <T> List<T> moveRight(int offset, T element, List<T> list) {
    final int elementIndex = list.indexOf(element);

    if (elementIndex == -1) {
        throw new NoSuchElementException("Element not found in provided list.");
    }

    if (offset == 0) {
        return list;
    } else {
        int newElementIndex = elementIndex + offset;

        // Ensure that the element will not move off the end of the list.
        if (newElementIndex >= list.size()) {
            newElementIndex = list.size() - 1;
        } else if (newElementIndex < 0) {
            newElementIndex = 0;
        }

        List<T> result = new ArrayList<>(list);
        result.remove(element);

        result.add(newElementIndex, element);

        return result;
    }
}

From source file:Utils.java

/** Reads for modifiers and creates integer with required mask.
* @param s string with modifiers/*from w  w  w.  j  ava  2  s .c  om*/
* @return integer with mask
* @exception NoSuchElementException if some letter is not modifier
*/
private static int readModifiers(String s) throws NoSuchElementException {
    int m = 0;

    for (int i = 0; i < s.length(); i++) {
        switch (s.charAt(i)) {
        case 'C':
            m |= KeyEvent.CTRL_MASK;
            break;

        case 'A':
            m |= KeyEvent.ALT_MASK;
            break;

        case 'M':
            m |= KeyEvent.META_MASK;
            break;

        case 'S':
            m |= KeyEvent.SHIFT_MASK;
            break;

        case 'D':
            m |= CTRL_WILDCARD_MASK;
            break;

        case 'O':
            m |= ALT_WILDCARD_MASK;
            break;

        default:
            throw new NoSuchElementException(s);
        }
    }

    return m;
}

From source file:it.reply.orchestrator.utils.EnumUtils.java

/**
 * <p>/*from  w w  w.  j av a 2 s  . co  m*/
 * Retrieve a enum value from its name and its class.
 * </p>
 * 
 * <p>
 * The enum must extends {@link Named}, and the name value should be the one that would be
 * returned from {@link Named#getName()}.
 * </p>
 * 
 * <p>
 * If no enum is found a {@link NoSuchElementException} will be thrown.
 * </p>
 * 
 * @param enumClass
 *          the class of the enum
 * @param name
 *          the enum name
 * @return the enum value, cannot be null
 */
public static <T extends Enum<T> & Named> T fromNameOrThrow(Class<T> enumClass, String name) {
    @Nullable
    T foundEnum = fromName(enumClass, name);
    if (foundEnum != null) {
        return foundEnum;
    } else {
        throw new NoSuchElementException("No enum found with name [" + name + "] in class [" + enumClass + "]");
    }
}

From source file:org.apache.brooklyn.test.http.TestHttpRecordingRequestInterceptor.java

public HttpRequest getLastRequest() {
    synchronized (requests) {
        if (requests.isEmpty())
            throw new NoSuchElementException("No http-requests received");
        return requests.get(requests.size() - 1);
    }//from w w  w. j  ava  2s . c o  m
}

From source file:org.obiba.mica.web.rest.user.UserProfileResource.java

@GET
@Path("/application/{application}")
@RequiresRoles(Roles.MICA_DAO)//  w w w .ja  v a2 s .  c  o m
public Mica.UserProfileDto getProfileByApplication(@PathParam("username") String username,
        @PathParam("application") String application, @QueryParam("group") String group) {
    ObibaRealm.Subject subject = userProfileService.getProfileByApplication(username, application, group);
    if (subject == null) {
        throw new NoSuchElementException(
                "User '" + username + "' with application '" + application + "' was not found.");
    }

    return dtos.asDto(subject);
}

From source file:de.tud.inf.db.sparqlytics.repository.AbstractRepository.java

@Override
public Cube findCube(final String name) {
    for (Cube cube : getCubes()) {
        if (cube.getName().equals(name)) {
            return cube;
        }//from   w  w w .j  ava 2s . c  o  m
    }
    throw new NoSuchElementException(name);
}

From source file:com.eviware.soapui.utils.ContainerWalker.java

public AbstractButton findButtonWithIcon(String iconFile) {
    AbstractButton returnedButton = (AbstractButton) Iterables.find(containedComponents,
            new ButtonWithIconPredicate(iconFile));
    if (returnedButton == null) {
        throw new NoSuchElementException("No button found with icon file " + iconFile);
    }/*ww w.  j  a v  a2 s . c  o m*/
    return returnedButton;
}

From source file:fr.cls.atoll.motu.library.misc.utils.PropertiesUtilities.java

/**
 * Replace in a string all variables (declared by <tt>${var}<tt>) with their corresponding values.
 * <p>/*from  w w  w  . jav  a  2s  . c  o  m*/
 * 
 * @param value string for which declared variables are replaced
 * @return the substituted string.
 */

static public String replaceSystemVariable(String value) {
    StringBuffer substValue = new StringBuffer();
    int idx = 0;
    int startIdx;
    int endIdx;
    String varName;
    String varValue;
    while ((startIdx = value.indexOf("${", idx)) != -1) {
        endIdx = value.indexOf("}", startIdx + 2);
        if (endIdx != -1) {
            varName = value.substring(startIdx + 2, endIdx);
            varValue = System.getProperty(varName);
            if (varValue == null) {
                throw new NoSuchElementException("no system property found for " + varName + " in " + value);
            }
            if (startIdx > 0) {
                substValue.append(value.substring(idx, startIdx));
            }
            // Treat correctly \
            varValue = StringUtils.replace(varValue, "\\", "\\\\");
            substValue.append(varValue);
            idx = endIdx + 1;
        } else {
            substValue.append(value.substring(idx));
            idx = value.length();
        }
    }
    substValue.append(value.substring(idx));
    return substValue.toString();
}