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

/**
 * Checks to see if a specific port is available.
 * /* w  ww. j a  v a2  s. c  o  m*/
 * @Author From apache Mina project
 * 
 * @param port
 *            the port to check for availability
 */
public static boolean portAvailable(final int port) {
    if (port < 1 || port > 30000) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    java.net.ServerSocket ss = null;
    java.net.DatagramSocket ds = null;
    try {
        ss = new java.net.ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new java.net.DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (final java.io.IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (final java.io.IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:Main.java

public static void checkRecyclerView(ViewGroup recyclerView) {
    checkRecyclerViewOnClassPath();/*w  w  w .  j a v  a2s  .c o  m*/

    Class clazz = recyclerView.getClass();
    while (clazz != null) {
        if (clazz.getName().equals(RECYCLERVIEW_CLASS_NAME)) {
            return;
        }
        clazz = clazz.getSuperclass();
    }

    throw new IllegalArgumentException("ViewGroup " + recyclerView.getClass().getName() + " not supported");

}

From source file:Main.java

/**
 * Makes a texture from any bitmap. // ww  w .  ja  v a 2  s.c  o  m
 * (Texture should have size that is power of 2) 
 * @param bmp bitmap
 * @return BitmapDrawable that has size that is power of 2. Bitmap is not stretched, free space 
 * is filled with default color.
 */
public static Bitmap makeTexture(Resources res, Bitmap bmp) {
    if (bmp == null) {
        throw new IllegalArgumentException("Bitmap can't be null");
    }

    int height = (int) roundPower2(bmp.getHeight());
    int width = (int) roundPower2(bmp.getWidth());

    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bmp, 0, 0, null);

    return result;
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }//  ww w. j av  a  2 s.c o m

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

From source file:Main.java

/**
 * Drops enough strings from the first array to fill the second one
 * beginning with the last object.//from   w w  w.j  a  v a 2 s. com
 * 
 * @param from the array to take the object from
 * @param to the array to write the objects to
 * @throws IllegalArgumentException If the second array is not shorter than the first one.
 * 
 * @author TheMrMilchmann
 * @since DerpieLang v1.0.0
 */
public static final <T> T[] dropFromEnd(T[] from, T[] to) {
    if (from.length <= to.length)
        throw new IllegalArgumentException("First array must be longer than the second one!");

    for (int i = 0; i < to.length; i++) {
        to[i] = from[i];
    }

    return to;
}

From source file:Main.java

/**
 * Replaces template parameters in the given url
 * @param   queryBuilder    The query string builder to replace the template parameters
 * @param   parameters   The parameters to replace in the url */
public static void appendUrlWithTemplateParameters(StringBuilder queryBuilder, Map<String, Object> parameters) {
    //perform parameter validation
    if (null == queryBuilder)
        throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid.");

    if (null == parameters)
        return;/* w  ww . java2 s.c o m*/

    //iterate and append parameters
    for (Map.Entry<String, Object> pair : parameters.entrySet()) {
        String replaceValue = "";

        //load parameter value
        if (null != pair.getValue())
            replaceValue = pair.getValue().toString();

        //find the template parameter and replace it with its value
        replaceAll(queryBuilder, "{" + pair.getKey() + "}", replaceValue);
    }
}

From source file:edu.usc.goffish.gopher.impl.client.GopherClient.java

/**
 * Simple Commandline version for Gopher client
 * @param args  gopherConfig gofsConfig graphId jarFile clzz data iterations
 *//*  w w w  . ja va 2 s . c  om*/
public static void main(String[] args) {

    GopherClient client = new GopherClient();

    if (args.length == 6 || args.length == 7) {
        client.initialize(args[0], args[1], args[2], args[3], args[4]);

        if (!"NILL".equals(args[5])) {
            SubGraphMessage message = new SubGraphMessage(args[5].getBytes());
            client.sendData(message);
        }

        if (args.length == 6) {
            client.start();
        } else {
            client.start(Integer.parseInt(args[6]));
        }

    } else {
        System.out.println(
                "Invalid Arguments, Useage : " + "java -cp ... edu.usc.goffish.gopher.impl.client.GopherClient "
                        + "gopherConfig gofsConfig graphId jarFile clzz data (optional)iterations ");
        throw new IllegalArgumentException("\"Invalid Arguments , Usage \" +\n"
                + "                    \"java -cp ... edu.usc.goffish.gopher.impl.client.GopherClient \" +\n"
                + "                    \"gopherConfig gofsConfig graphId jarFile clzz data (optional)iterations \"");
    }

}

From source file:com.aestheticsw.jobkeywords.service.termextractor.support.SearchUtils.java

public static Locale lookupLocaleByCountry(String country) {
    List<Locale> localeList = LocaleUtils.languagesByCountry(country);
    if (localeList == null || localeList.size() == 0) {
        throw new IllegalArgumentException("Invalid country code: " + country);
    }/*ww w  . j av a  2  s. c o  m*/
    Locale locale = localeList.get(0);

    // if English isn't the language of the first Locale, then try to find English.
    if (!locale.getLanguage().equals("en")) {
        for (Locale option : localeList) {
            if (option.getLanguage().equals("en")) {
                return option;
            }
        }
    }
    return locale;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T extends Fragment> T attach(Activity activity, int containerId, Class<T> fragmentType,
        String tag) {/*w  w w.ja  v  a 2  s . c  o  m*/
    if (activity == null) {
        throw new IllegalArgumentException("activity is null");
    }
    if (fragmentType == null) {
        throw new IllegalArgumentException("fragmentType is null");
    }
    if (tag == null) {
        throw new IllegalArgumentException("tag is null");
    }

    T result;

    FragmentManager manager = activity.getFragmentManager();
    Fragment fragment = manager.findFragmentByTag(tag);

    if (fragment == null) {
        try {
            result = fragmentType.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(
                    "fragmentType cannot be instantiated (default constructor is not visible)", e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(
                    "fragmentType cannot be instantiated (instance could not be created)", e);
        }

        manager.beginTransaction().add(containerId, result, tag).commit();
    } else {
        if (!fragmentType.isInstance(fragment)) {
            throw new IllegalArgumentException("Different fragmentType for tag");
        }

        result = (T) fragment;
    }

    return result;
}

From source file:Main.java

public static long byteArrayToLong(byte[] b, int offset, int length) {
    if (b.length < offset + length) {
        throw new IllegalArgumentException("offset + length must be less than or equal to b.length");
    }/*  www.ja va 2s  . c  om*/

    long value = 0;
    for (int i = 0; i < length; i++) {
        int shift = (length - 1 - i) * 8;
        value += (b[i + offset] & 0x000000FF) << shift;
    }
    return value;
}