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 <T> void removeWithoutUsingRemoveMethod(List<T> list, int index) {
    if (index < 0 || index >= list.size()) {
        throw new IllegalArgumentException("index out of range");
    }// w  ww .j a  v  a  2 s  .  c  o  m
    List<T> part1 = new ArrayList<T>(list.subList(0, index));
    List<T> part2 = new ArrayList<T>(list.subList(index + 1, list.size()));
    list.clear();
    list.addAll(part1);
    list.addAll(part2);
}

From source file:Main.java

public static <E extends View> E findViewById(View view, @IdRes int resId) {
    if (null == view)
        throw new IllegalArgumentException("The argument view can not be null,please check your argument!");
    //noinspection unchecked
    return (E) view.findViewById(resId);
}

From source file:Main.java

public static short uintToShort(int value) {
    if (value <= MAX_UNSIGNED_SHORT_VALUE) {
        if (value >= MAX_UNSIGNED_SHORT_VALUE / 2) {
            return (short) (~(MAX_UNSIGNED_SHORT_VALUE - value) + 1);
        } else {//from  w  w  w.  j a  va2 s  .  c  o  m
            return (short) value;
        }
    } else {
        throw new IllegalArgumentException("Value out of range for a short");
    }
}

From source file:Main.java

/**
 * Set user data to account//from w w w.j  a  va  2 s.c om
 * @param context context
 * @param account account
 * @param key key
 * @param value value
 */
public static void setUserData(Context context, Account account, String key, String value) {
    if (account == null)
        throw new IllegalArgumentException("account cannot be null");
    AccountManager accountManager = AccountManager.get(context);
    accountManager.setUserData(account, key, value);
}

From source file:Main.java

public static String getFilePathByContentResolver(Context context, Uri uri) {
    if (null == uri) {
        return null;
    }/*w  w  w  .  jav  a 2  s .c  o  m*/
    Cursor c = context.getContentResolver().query(uri, null, null, null, null);
    String filePath = null;
    if (null == c) {
        throw new IllegalArgumentException("Query on " + uri + " returns null result.");
    }
    try {
        if ((c.getCount() != 1) || !c.moveToFirst()) {
        } else {
            filePath = c.getString(c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
        }
    } finally {
        c.close();
    }
    return filePath;
}

From source file:Main.java

public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    }/*from w  w  w  .java2s  .c om*/
    if (list.getLength() == 0) {
        return null;
    }
    throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
            + parent.getNodeName() + ", but found " + list.getLength());
}

From source file:Main.java

private static String decode(final String content, final String encoding) {
    try {//from   ww w.ja  v a  2  s  . co  m
        return URLDecoder.decode(content, encoding != null ? encoding : DEFAULT_CONTENT_CHARSET);
    } catch (UnsupportedEncodingException problem) {
        throw new IllegalArgumentException(problem);
    }
}

From source file:Main.java

/**
 * Convert Base64 string to byte array./*www .  j  a  v  a 2  s.com*/
 * 
 * @param input
 *            Base64 string.
 * @return Converted byte array.
 */
public static byte[] fromBase64(final String input) {
    String tmp = input.replace("\r\n", "");
    if (tmp.length() % 4 != 0) {
        throw new IllegalArgumentException("Invalid base64 input");
    }
    int len = (tmp.length() * 3) / 4;
    int pos = tmp.indexOf('=');
    if (pos > 0) {
        len -= tmp.length() - pos;
    }
    byte[] decoded = new byte[len];
    char[] inChars = new char[4];
    int j = 0;
    int[] b = new int[4];
    for (pos = 0; pos != tmp.length(); pos += 4) {
        tmp.getChars(pos, pos + 4, inChars, 0);
        b[0] = getIndex(inChars[0]);
        b[1] = getIndex(inChars[1]);
        b[2] = getIndex(inChars[2]);
        b[3] = getIndex(inChars[3]);
        decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));
        if (b[2] < 64) {
            decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));
            if (b[3] < 64) {
                decoded[j++] = (byte) ((b[2] << 6) | b[3]);
            }
        }
    }
    return decoded;
}

From source file:Main.java

public static boolean isApplicationInstalled(final Context context, final String pkgName) {
    if (TextUtils.isEmpty(pkgName)) {
        return false;
    }/*from   w  w  w  . ja  va2  s.co  m*/
    if (null == context) {
        throw new IllegalArgumentException("context may not be null.");
    }
    try {
        context.getPackageManager().getPackageInfo(pkgName, 0);
        return true;
    } catch (final NameNotFoundException e) {
        // Application not installed.
    }
    return false;
}

From source file:Main.java

/**
 * Validate given string argument isn't null or empty string.
 *
 * @param arg argument to validate// w w  w.j ava  2 s  .  co m
 * @param argName name of the argument to show in error message
 * @throws IllegalArgumentException
 */
public static void notNullOrEmpty(String arg, String argName) {
    if (arg == null || arg.length() < 1) {
        throw new IllegalArgumentException("argument is null: " + argName);
    }
}