Example usage for java.lang IllegalArgumentException IllegalArgumentException

List of usage examples for java.lang IllegalArgumentException IllegalArgumentException

Introduction

In this page you can find the example usage for java.lang IllegalArgumentException IllegalArgumentException.

Prototype

public IllegalArgumentException(Throwable cause) 

Source Link

Document

Constructs a new exception with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }//from w w w . jav  a 2s .  c  o m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static Short byteArrayToShortLE(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }/*w w w  .jav a 2 s  .  c o m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static BigInteger byteArrayToBigInteger(byte[] b, int offset, int length) {
    if (b.length < offset + length) {
        throw new IllegalArgumentException("offset + length must be less than or equal to b.length");
    }/* www . j  a  v  a2s  . c om*/

    BigInteger value = BigInteger.valueOf(0);
    for (int i = 0; i < length; i++) {
        value = value.shiftLeft(8);
        value = value.add(BigInteger.valueOf(b[i + offset] & 0x000000ff));
    }
    return value;
}

From source file:Main.java

public static String getIdNameFromId(String pId) {
    if (pId.startsWith("@+id/")) {
        return pId.substring(5);
    } else if (pId.startsWith("@id/")) {
        return pId.substring(4);
    }//from   www  .j  a  v a2 s.co m
    throw new IllegalArgumentException("Wrong id structure: " + pId);
}

From source file:Main.java

public static <T> T findLastItemAtPosition(SortedSet<T> queryResult, int itemPosition) {
    if (queryResult == null || queryResult.isEmpty())
        throw new IllegalArgumentException("Provided result set is 'null' or empty.");

    Iterator<T> resultSetIterator = queryResult.iterator();
    int nbItemInList = 0;
    T lastItem = null;//from w  ww. j av  a 2  s  .  c  o m
    while (resultSetIterator.hasNext() && nbItemInList++ < itemPosition)
        lastItem = resultSetIterator.next();
    return lastItem;
}

From source file:Main.java

public static <E> E getSingleElement(List<E> list) {
    if (list == null) {
        return null;
    }/*from w  ww .j  av  a  2  s . co  m*/

    int listSize = list.size();
    if (listSize > 1) {
        throw new IllegalArgumentException("Expected only one entry in the list.");
    }

    return listSize == 1 ? list.get(0) : null;
}

From source file:Main.java

public static byte[] hexStringToBytes(String hexString) {
    if (hexString.length() % 2 != 0)
        throw new IllegalArgumentException("Hex string length must be even: " + hexString);
    byte[] bytes = new byte[hexString.length() / 2];
    char[] ucChars = hexString.toUpperCase().toCharArray();
    for (int i = 0; i < bytes.length; i++) {
        int index1 = HEX_CHARS_STRING.indexOf(ucChars[2 * i]);
        int index2 = HEX_CHARS_STRING.indexOf(ucChars[2 * i + 1]);
        if (index1 == -1 || index2 == -1)
            throw new IllegalArgumentException("Non-hex character in string: " + hexString);
        bytes[i] = (byte) ((index1 << 4) + index2);
    }/*  w  ww.j  a v  a 2  s .c  o m*/
    return bytes;
}

From source file:Main.java

public static Document file2Document(File file) throws Exception {
    if (file == null || !file.exists()) {
        throw new IllegalArgumentException("File must exist![" + file == null ? "NULL"
                : ("Could not be found: " + file.getAbsolutePath()) + "]");
    }//ww  w  .j a  v  a2  s  . c  o m
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    return dbFactory.newDocumentBuilder().parse(new FileInputStream(file));
}

From source file:Main.java

public static void hideKeyboard(Context context, View view) {
    if (context == null) {
        throw new IllegalArgumentException("context cannot be null");
    }//www. j  av a2 s .co  m
    if (view == null) {
        throw new IllegalArgumentException("view cannot be null");
    }
    InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:Main.java

public static int base32CharToValue(char base32Char) {
    int value = BASE32_CHARS.indexOf(base32Char);
    if (value == -1) {
        throw new IllegalArgumentException("Not a valid base32 char: " + base32Char);
    } else {//from  w w w.  j  av  a  2s  .c om
        return value;
    }
}