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 String utf8Encoding(String value, String sourceCharsetName) {
    try {/*from   w  ww.  jav a  2  s . c o m*/
        return new String(value.getBytes(sourceCharsetName), UTF_8);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

@SafeVarargs
public static <T> Map<T, T> createMap(T... array) {
    if (array == null || array.length == 0) {
        return null;
    }/*from   w  w w  .  j  av  a2  s .  c om*/
    if (array.length % 2 != 0) {
        throw new IllegalArgumentException("Array size must be even number.");
    }
    Map<T, T> map = new LinkedHashMap<T, T>();
    for (int i = 0; i < array.length; i += 2) {
        map.put(array[i], array[i + 1]);
    }
    return map;
}

From source file:Main.java

/**
 * Compute edit distance between two strings = Levenshtein distance *
 *//*from   w w w .ja  v  a 2s.co  m*/
public static int getLevenshteinDistance(final String s, final String t) {
    if (s == null || t == null) {
        throw new IllegalArgumentException("Strings must not be null");
    }

    final int n = s.length(); // length of s
    final int m = t.length(); // length of t

    if (n == 0) {
        return m;
    } else if (m == 0) {
        return n;
    }

    int p[] = new int[n + 1]; //'previous' cost array, horizontally
    int d[] = new int[n + 1]; // cost array, horizontally
    int _d[]; //placeholder to assist in swapping p and d

    // indexes into strings s and t
    int i; // iterates through s
    int j; // iterates through t
    char t_j; // jth character of t
    int cost; // cost
    for (i = 0; i <= n; i++) {
        p[i] = i;
    }
    for (j = 1; j <= m; j++) {
        t_j = t.charAt(j - 1);
        d[0] = j;
        for (i = 1; i <= n; i++) {
            cost = s.charAt(i - 1) == t_j ? 0 : 1;
            // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
            d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
        }
        // copy current distance counts to 'previous row' distance counts
        _d = p;
        p = d;
        d = _d;
    }

    // our last action in the above loop was to switch d and p, so p now
    // actually has the most recent cost counts
    return p[n];
}

From source file:Main.java

public static void assertAllDifferent(List<?> entities) {

    Set<?> set = new LinkedHashSet<>(entities);

    if (set.size() != entities.size()) {
        throw new IllegalArgumentException("All entities must be different: " + entities + ", " + set);
    }//from  w w w .  j  av a2s .  c om

}

From source file:Main.java

public static String truncate(String str, int byteLength) {
    if (str == null) {
        return null;
    }/* w w w.j a va 2  s .  c o m*/
    if (str.length() == 0) {
        return str;
    }
    if (byteLength < 0) {
        throw new IllegalArgumentException("Parameter byteLength must be great than 0");
    }
    int i = 0;
    int len = 0;
    int leng = 0;
    char[] chs = str.toCharArray();
    try {
        leng = str.getBytes("gbk").length;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    if (leng <= byteLength)
        return str;
    while ((len < byteLength) && (i < leng)) {
        len = (chs[i++] > 0xff) ? (len + 2) : (len + 1);
    }

    if (len > byteLength) {
        i--;
    }
    return new String(chs, 0, i) + "...";
}

From source file:Main.java

public static long toLong(byte[] array) {
    if (array.length < 8)
        throw new IllegalArgumentException("Byte array requires 4 bytes.");
    long nonce = array[7];
    for (int i = 6; i >= 0; i--) {
        nonce = nonce << 8 | array[i] & 0xFF;
    }/*from   w ww  .j a  v a2  s . co  m*/

    return nonce;
}

From source file:Main.java

public static Collection asTargetTypeCollection(Collection c, Class targetCollectionClass) {
    if (targetCollectionClass == null)
        throw new IllegalArgumentException("'targetCollectionClass' must be not null");
    if (c == null)
        return null;
    if (targetCollectionClass.isInstance(c))
        return c;

    Collection result = null;//from   w w  w . j  a v a2  s.  co  m

    try {
        result = (Collection) targetCollectionClass.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "targetCollectionClass=" + targetCollectionClass.getName() + " is not correct!", e);
    }

    result.addAll(c);
    return result;
}

From source file:Main.java

/**
 * Get the value of color with specified alpha.
 *
 * @param color/*from  w w  w.  j a v a2  s  . co m*/
 * @param alpha between 0 to 255.
 * @return Return the color with specified alpha.
 */
public static int getColorAtAlpha(int color, int alpha) {
    if (alpha < 0 || alpha > 255) {
        throw new IllegalArgumentException("The alpha should be 0 - 255.");
    }
    return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
}

From source file:Main.java

public static String getResourceString(String name, Context context) {
    int nameResourceID = context.getResources().getIdentifier(name, "string",
            context.getApplicationInfo().packageName);
    if (nameResourceID == 0) {
        throw new IllegalArgumentException("No resource string found with name " + name);
    } else {/*w w  w .jav  a  2  s  . c  om*/
        return context.getString(nameResourceID);
    }
}

From source file:Main.java

public static double divide(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }//from   w w  w  .ja  va 2s . co  m
    BigDecimal b = new BigDecimal(Double.toString(v1));
    BigDecimal one = new BigDecimal(v2);
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}