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 byte[] hexStringToByteArray(String encoded) {
    if ((encoded.length() % 2) != 0)
        throw new IllegalArgumentException("Input string must contain an even number of characters");

    final byte result[] = new byte[encoded.length() / 2];
    final char enc[] = encoded.toCharArray();
    for (int i = 0; i < enc.length; i += 2) {
        StringBuilder curr = new StringBuilder(2);
        curr.append(enc[i]).append(enc[i + 1]);
        result[i / 2] = (byte) Integer.parseInt(curr.toString(), 16);
    }/*from  w  ww  .j av  a 2 s .  c o m*/
    return result;
}

From source file:Main.java

public static int expectedNumberOfSets(int setSize, int subSetSize) {
    if (setSize < subSetSize) {
        // Not enough sources
        throw new IllegalArgumentException(
                "Cannot create subsets of size " + subSetSize + " out of set of size " + setSize);
    }/*from  w  w  w . j  a  v  a 2 s.c o m*/
    int expectedNumberOfSets = 1;
    int divider = 1;
    for (int i = 0; i < (setSize - subSetSize); i++) {
        expectedNumberOfSets *= setSize - i;
        divider *= i + 1;
    }
    return expectedNumberOfSets / divider;
}

From source file:Main.java

public static int numberOfBytesToEncodeLength(long length) {
    if (length < 0)
        throw new IllegalArgumentException("Length is negative: " + length);
    if (length < (1 << 7 - 1))
        return 1;
    if (length < (1 << 14 - 1))
        return 2;
    if (length < (1 << 21 - 1))
        return 3;
    if (length < (1 << 28 - 1))
        return 4;
    if (length < (1 << 35 - 1))
        return 5;
    if (length < (1 << 42 - 1))
        return 6;
    if (length < (1 << 49 - 1))
        return 7;
    if (length < (1 << 56 - 1))
        return 8;
    throw new IllegalArgumentException("Length exceeds maximum: " + length);
}

From source file:Main.java

public static void hideView(View view) {
    if (view != null) {
        view.setVisibility(View.GONE);
    } else {//from w ww.  j av a2s  .  c  om
        throw new IllegalArgumentException("A view is null in your parameter.");
    }
}

From source file:Main.java

public static byte[] getBytes(final String data, String charset) {

    if (data == null) {
        throw new IllegalArgumentException("data may not be null");
    }//from  ww w . j a v a 2  s. co  m

    if (charset == null || charset.length() == 0) {
        throw new IllegalArgumentException("charset may not be null or empty");
    }

    try {
        return data.getBytes(charset);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(String.format("Unsupported encoding: %s", charset));
    }
}

From source file:Main.java

public static <T> T checkNullArgument(T reference, @Nullable Object errorMessage) {
    if (reference == null) {
        throw new IllegalArgumentException(String.valueOf(errorMessage));
    }/*from w w  w .  j av a2 s .com*/
    return reference;
}

From source file:Main.java

public synchronized static void sortTwoArrays(Double[] queryList, Double[] secondList) {
    if (queryList.length != secondList.length) {
        throw new IllegalArgumentException("The lists have to be of equal size!");
    }/*from  w  w w.  j  av  a  2 s  . c o m*/
    int n = queryList.length;
    for (int i = 0; i < n; i++) {
        for (int j = n - 1; j > i; j--) {
            double one = queryList[j - 1].doubleValue();
            double two = queryList[j].doubleValue();
            if (one > two) {
                queryList[j - 1] = two;
                queryList[j] = one;

                Double temp = secondList[j - 1];
                secondList[j - 1] = secondList[j];
                secondList[j] = temp;
            }
        }
    }
}

From source file:Main.java

public static int index(int bar, int beat) {
    if (beat == 1) {
        return bar * 2;
    } else if (beat == 5) {
        return bar * 2 + 1;
    }//from  w ww  .j av a 2  s . c  om
    throw new IllegalArgumentException("only beats 1 and 5 allowed, but was " + beat);
}

From source file:Main.java

public static <E> List<E> limitResults(List<E> list, int maxIndex) {
    if (maxIndex < 0) {
        throw new IllegalArgumentException("Expected non negative max index, received " + maxIndex); //$NON-NLS-1$
    }//  w w  w  . j  a v a  2 s .c  o m
    if (maxIndex > 0 && maxIndex <= list.size()) {
        return list.subList(0, maxIndex);
    }
    return list;
}

From source file:Main.java

public static void removeActivityBackgroundCompat(Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("Activity is null");
    }/*from   w  ww  .j a  va 2s  . c om*/

    if (Build.VERSION.SDK_INT >= 16) {
        activity.getWindow().getDecorView().setBackground(null);
    } else {
        activity.getWindow().getDecorView().setBackgroundDrawable(null);
    }
}