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 final void checkBuffer(final ByteBuffer buffer) {
    if (buffer == null) {
        throw new IllegalArgumentException("buffer == null");
    }/* w ww  . j a  v a  2s  .  c  o  m*/
    if (!buffer.isDirect()) {
        throw new IllegalArgumentException("must use DirectByteBuffer");
    }
}

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);//from www.  j a  v a2s.c  o  m
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

/**
 * @param hexChars A String containing an EVEN number of hex
 *                      characters./*w  ww. j ava  2 s  .c om*/
 */
public static String hexCharsToOctalOctets(String hexChars) {
    int chars = hexChars.length();
    if (chars != (chars / 2) * 2) {
        throw new IllegalArgumentException(
                "Hex character lists contains " + "an odd number of characters: " + chars);
    }
    StringBuffer sb = new StringBuffer();
    char c;
    int octet;
    for (int i = 0; i < chars; i++) {
        octet = 0;
        c = hexChars.charAt(i);
        if (c >= 'a' && c <= 'f') {
            octet += 10 + c - 'a';
        } else if (c >= 'A' && c <= 'F') {
            octet += 10 + c - 'A';
        } else if (c >= '0' && c <= '9') {
            octet += c - '0';
        } else {
            throw new IllegalArgumentException("Non-hex character in input at offset " + i + ": " + c);
        }
        octet = octet << 4;
        c = hexChars.charAt(++i);
        if (c >= 'a' && c <= 'f') {
            octet += 10 + c - 'a';
        } else if (c >= 'A' && c <= 'F') {
            octet += 10 + c - 'A';
        } else if (c >= '0' && c <= '9') {
            octet += c - '0';
        } else {
            throw new IllegalArgumentException("Non-hex character in input at offset " + i + ": " + c);
        }

        sb.append('\\');
        sb.append((char) ('0' + (octet >> 6)));
        sb.append((char) ('0' + ((octet >> 3) & 7)));
        sb.append((char) ('0' + (octet & 7)));
    }
    return sb.toString();
}

From source file:Main.java

private static byte[] transStreamToBytes(InputStream is, int buffSize) {
    if (is == null) {
        return null;
    }/*from   w  w w  .  j  a v a  2 s.c  o m*/
    if (buffSize <= 0) {
        throw new IllegalArgumentException("buffSize can not less than zero.....");
    }
    byte[] data = null;
    byte[] buffer = new byte[buffSize];
    int actualSize = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((actualSize = is.read(buffer)) != -1) {
            baos.write(buffer, 0, actualSize);
        }
        data = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return data;
}

From source file:Main.java

/**
 * Creates a Map out of an array with Strings.
 * //from w w w.  j  a v a 2s  .  co m
 * @param strings input strings, key-value alternating
 * @return a parameter map
 */
public static Map<String, String> map(final String... strings) {
    if (strings.length % 2 != 0) {
        throw new IllegalArgumentException("strings.length % 2 != 0");
    }
    final Map<String, String> sMap = new HashMap<String, String>();
    for (int i = 0; i < strings.length; i += 2) {
        sMap.put(strings[i], strings[i + 1]);
    }
    return sMap;
}

From source file:Main.java

public static Method[] convertStringsToMethods(Class clazz, String[] methodNames) {
    Method[] methods = new Method[methodNames.length];
    for (int i = 0; i < methods.length; i++) {
        Method m = getMethod(clazz, methodNames[i]);
        if (m == null) {
            throw new IllegalArgumentException("Not found method. methodName: " + methodNames[i]);
        }//from w w w  . j a va 2 s. c  o m
        methods[i] = m;
    }
    return methods;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Map<T, Object> asMap(T key, Object... values) {
    Map<T, Object> result = new LinkedHashMap<T, Object>();

    if (values == null || values.length % 2 == 0) {
        throw new IllegalArgumentException("value[] must be not null and an odd length");
    }/*from  w ww. j ava 2s  .  c  om*/

    result.put(key, values[0]);
    for (int i = 1; i < values.length; i += 2) {
        result.put((T) values[i], values[i + 1]);
    }

    return result;
}

From source file:Main.java

public static int getMeasuredWidth(View view) {
    if (view == null) {
        throw new IllegalArgumentException("view is null");
    }//from  w w  w  .  ja va  2s.c  o m

    view.measure(0, 0);
    return view.getMeasuredWidth();
}

From source file:Main.java

public static Color getRandColor(int threashold) {
    if (threashold < 0 || threashold > 255)
        throw new IllegalArgumentException("Threashold is not between 0 and 255");
    int secondOperand = 255 - threashold;
    return new Color((int) (Math.random() * secondOperand) + threashold,
            (int) (Math.random() * secondOperand) + threashold,
            (int) (Math.random() * secondOperand) + threashold);
}

From source file:Main.java

public static boolean hasNewVersion(Context context, String pkgName, int versionCodeNew) {
    if (null == context) {
        throw new IllegalArgumentException("context may not be null.");
    }//from  w  w w  .j  a  va2  s . c  o  m
    try {
        int versionCode = getVersionCode(context, pkgName);
        if (versionCode < versionCodeNew) {
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}