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

/**
 * Get URI string from resourceId./*from   w  w  w  .j a  v  a  2 s .  c o  m*/
 *
 * @param context The context.
 * @param resourceId The resource id.
 * @return The URI string.
 */
public static String getUriString(final Context context, final int resourceId) {
    if (context == null) {
        throw new IllegalArgumentException("context == null");
    }
    return new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE).authority(context.getPackageName())
            .appendPath(Integer.toString(resourceId)).toString();
}

From source file:Main.java

/**
 * invoke a setter method//  w  ww. j a v  a2 s . co m
 * 
 * @param setter
 * @param object
 * @param propValue
 */
public static void invokeSetter(Method setter, Object object, Object propValue) {
    if (setter == null) {
        throw new IllegalArgumentException("The setter method cannot be null");
    }

    if (object == null) {
        throw new IllegalArgumentException("The object cannot be null");
    }

    try {
        setter.setAccessible(true);
        setter.invoke(object, new Object[] { propValue });
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static int convertT9CharToIndex(char c) {
    if ((c >= '0') && (c <= '9')) {
        return c - '0';
    }/*  ww w .  j  a v a 2  s . c  om*/

    switch (c) {
    case '+':
        return 10;
    case ',':
        return 11;
    case '*':
        return 12;
    case '#':
        return 13;
    default:
        throw new IllegalArgumentException("INVALID T9 SEARCH CHARACTER");
    }
}

From source file:Main.java

static public JMenuItem createJMenuItem(JMenu pMenu, String pText, char pMnemonic, KeyStroke pAccelerator,
        ActionListener pActionListener) throws IllegalArgumentException {
    if (pMenu == null)
        throw new IllegalArgumentException("Menu is missing.");
    if (pText == null)
        throw new IllegalArgumentException("Missing text for menu item.");

    final JMenuItem menuItem = new JMenuItem(pText, pMnemonic);

    if (pAccelerator != null)
        menuItem.setAccelerator(pAccelerator);
    if (pActionListener != null)
        menuItem.addActionListener(pActionListener);
    if (defaultFont != null)
        menuItem.setFont(defaultFont);/* w  w w .  j  a  v  a  2 s .c  o m*/

    pMenu.add(menuItem);

    return menuItem;
}

From source file:Main.java

/**
 * Convert a millisecond duration to a string format
 *
 * @param millis A duration to convert to a string form
 * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
 *///from  w ww .java  2 s.c om
public static String getDurationBreakdown(long millis) {
    if (millis < 0) {
        throw new IllegalArgumentException("Duration must be greater than zero!");
    }

    long days = TimeUnit.MILLISECONDS.toDays(millis);
    millis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    return (String.valueOf(days) + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}

From source file:Main.java

public static String getYoutubeVideoId(String url) {
    Pattern pattern = Pattern.compile("^https?://.*(?:youtu.be/|v/|u/\\w/|embed/|watch?v=)([^#&?]*).*$",
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(url);
    if (!matcher.matches())
        throw new IllegalArgumentException("Regex fail. not matched. Url: " + url);

    return matcher.group(1);
}

From source file:Main.java

/**
 * For a given string that should contain an integer value, return either
 * the <code>int</code> value or a zero if empty.
 * //from w w w.j  av  a2s.c  o m
 * @param irodsValue
 * @return <code>int</code> equivilent of irods value
 */
public static int getIntOrZeroFromIRODSValue(final String irodsValue) {

    int result = 0;

    if (irodsValue == null) {
        throw new IllegalArgumentException("null irodsValue");
    }

    if (irodsValue.isEmpty()) {
        // nothing
    } else {
        try {
            result = Integer.parseInt(irodsValue);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("cannot format number:" + irodsValue, nfe);
        }
    }

    return result;

}

From source file:MD5.java

public static String crypt(String str) {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }/*from   w ww  .ja v  a2 s.  c  o m*/

    digester.update(str.getBytes());
    byte[] hash = digester.digest();
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < hash.length; i++) {
        if ((0xff & hash[i]) < 0x10) {
            hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
        } else {
            hexString.append(Integer.toHexString(0xFF & hash[i]));
        }
    }
    return hexString.toString();
}

From source file:Main.java

/**
 * Convert a millisecond duration to a string format
 *
 * @param millis A duration to convert to a string form
 * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
 *///from  w  ww  .  j  av a  2 s  . c om
public static String getDurationBreakdownNoDays(long millis) {
    if (millis < 0) {
        throw new IllegalArgumentException("Duration must be greater than zero!");
    }

    long days = TimeUnit.MILLISECONDS.toDays(millis);
    millis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    return (hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}

From source file:Main.java

/**
 * For a given string that should contain an long value, return either the
 * <code>long</code> value or a zero if empty.
 * //from w ww  .j  a v a 2 s  . c  o m
 * @param irodsValue
 * @return <code>long</code> equivilent of irods value
 */
public static long getLongOrZeroFromIRODSValue(final String irodsValue) {

    long result = 0L;

    if (irodsValue == null) {
        throw new IllegalArgumentException("null irodsValue");
    }

    if (irodsValue.isEmpty()) {
        // nothing
    } else {
        try {
            result = Long.parseLong(irodsValue);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("cannot format number:" + irodsValue, nfe);
        }
    }

    return result;

}