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

private static String getSafePrintChars(byte[] byteArray, int startPos, int length) {
    if (byteArray == null) {
        // return "" instead?
        throw new IllegalArgumentException("Argument 'byteArray' cannot be null");
    }/*www  .  j av a2s . co  m*/
    if (byteArray.length < startPos + length) {
        throw new IllegalArgumentException("startPos(" + startPos + ")+length(" + length
                + ") > byteArray.length(" + byteArray.length + ")");
    }
    StringBuilder buf = new StringBuilder();
    for (int i = startPos; i < length; i++) {
        if (byteArray[i] >= (byte) 0x20 && byteArray[i] < (byte) 0x7F) {
            buf.append((char) byteArray[i]);
        } else {
            buf.append(".");
        }
    }
    return buf.toString();
}

From source file:Main.java

public static byte ulongToByte(long value) throws IllegalArgumentException {
    if (value <= MAX_UNSIGNED_BYTE_VALUE) {
        if (value >= MAX_UNSIGNED_BYTE_VALUE / 2) {
            long originalComplementValue = (~(MAX_UNSIGNED_BYTE_VALUE - value)) + 1;
            return (byte) originalComplementValue;
        } else {//from   ww w  .  ja v  a 2 s .c  om
            return (byte) value;
        }
    } else {
        throw new IllegalArgumentException("Value out of range for a byte");
    }
}

From source file:Main.java

/**
 * Validates and processes the given Url
 * @param    url The given Url to process
 * @return   Pre-process Url as string */
public static String cleanUrl(StringBuilder url) {
    //ensure that the urls are absolute
    Pattern pattern = Pattern.compile("^(https?://[^/]+)");
    Matcher matcher = pattern.matcher(url);
    if (!matcher.find())
        throw new IllegalArgumentException("Invalid Url format.");

    //get the http protocol match
    String protocol = matcher.group(1);

    //remove redundant forward slashes
    String query = url.substring(protocol.length());
    query = query.replaceAll("//+", "/");

    //return process url
    return protocol.concat(query);
}

From source file:Main.java

/**
 * Remove the XPath special characters.// w  w w.  ja va 2  s .co m
 * 
 * @param input
 *            - the return value of XPath.
 * @return a String that doesn't contain any XPath characters.
 * @throws IllegalArgumentException
 *             if the input string is empty or null or equals [] (an empty
 *             mods tag).
 */
public static String removeCtrlChars(final String input) {
    if (input == null || input.length() == 0 || input.equals("[]")) {
        throw new IllegalArgumentException(
                "The value for the parameter input in removeCtrlChars mustn't be empty.");
    }
    // Ex. value: []
    String ret = input.replaceFirst("[\\[]+(\\w)+: ((\\w)+=\")?", "");
    int lastPos = input.lastIndexOf("]");
    if (lastPos == input.length() - 1) {
        ret = ret.substring(0, ret.length() - 1);
        if (ret.length() > 1 && ret.lastIndexOf("\"]") == ret.length() - 2) {
            ret = ret.substring(0, ret.length() - 2);
        }
    }
    return ret;
    // return input;
}

From source file:Main.java

public static void start(final ContentHandler handler, final String... elementAndAttributes)
        throws SAXException {
    if (elementAndAttributes == null || elementAndAttributes.length == 0
            || elementAndAttributes.length % 2 != 1) {
        throw new IllegalArgumentException(
                "elementAndAttributes must contains element name and 0..n pais of attr name-value");
    }//  w  w w.j a  v  a 2 s. c  om
    final String elementName = elementAndAttributes[0];
    AttributesImpl attributes = EMPTY_ATTRIBUTES;
    if (elementAndAttributes.length > 1) {
        attributes = new AttributesImpl();
        for (int i = 1; i + 1 < elementAndAttributes.length;) {
            String attributeName = elementAndAttributes[i++];
            String attributeValue = elementAndAttributes[i++];
            attributes.addAttribute("", attributeName, attributeName, NULL_TYPE, attributeValue);
        }
    }
    start(handler, elementName, attributes);
}

From source file:Main.java

static String zzb(String... strArr) {
    Builder builder = new Builder();
    int length = strArr.length;
    int i = 0;// w w  w  . j a  v  a2 s  .  c om
    while (i < length) {
        String str = strArr[i];
        try {
            URL url = new URL(str);
            builder.appendQueryParameter("url", url.getProtocol() + "://" + url.getHost());
            i++;
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid URL: " + str);
        }
    }
    return "weblogin:" + builder.build().getQuery();
}

From source file:Main.java

/** Adds a click listener to the given view which invokes the method named by methodName on the given target.
 * The method must be public and take no arguments.
 *//*  w w  w  .j  a va2 s  . c  om*/
public static void bindOnClickListener(final Object target, View view, String methodName) {
    final Method method;
    try {
        method = target.getClass().getMethod(methodName);
    } catch (Exception ex) {
        throw new IllegalArgumentException(ex);
    }
    view.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                method.invoke(target);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:Main.java

@Deprecated
public static NdefRecord createExternal(String domain, String type, byte[] data) {
    if (domain == null)
        throw new NullPointerException("domain is null");
    if (type == null)
        throw new NullPointerException("type is null");

    domain = domain.trim().toLowerCase(Locale.US);
    type = type.trim().toLowerCase(Locale.US);

    if (domain.length() == 0)
        throw new IllegalArgumentException("domain is empty");
    if (type.length() == 0)
        throw new IllegalArgumentException("type is empty");

    byte[] byteDomain = domain.getBytes(Charset.forName("UTF_8"));
    byte[] byteType = type.getBytes(Charset.forName("UTF_8"));
    byte[] b = new byte[byteDomain.length + 1 + byteType.length];
    System.arraycopy(byteDomain, 0, b, 0, byteDomain.length);
    b[byteDomain.length] = ':';
    System.arraycopy(byteType, 0, b, byteDomain.length + 1, byteType.length);

    return new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, b, new byte[0], data);
}

From source file:Main.java

/**
 * Finds element in DOM tree//from  ww w .  jav a2  s  . c  o m
 * @param topElm Top element
 * @param nodeName Node name
 * @return returns found node
 */
public static List<Node> findNodesByType(Element topElm, int type) {
    List<Node> retvals = new ArrayList<Node>();
    if (topElm == null)
        throw new IllegalArgumentException("topElm cannot be null");
    synchronized (topElm.getOwnerDocument()) {
        Stack<Node> stack = new Stack<Node>();
        stack.push(topElm);
        while (!stack.isEmpty()) {
            Node curElm = stack.pop();
            if (curElm.getNodeType() == type) {
                retvals.add(curElm);
            }
            List<Node> nodesToProcess = new ArrayList<Node>();

            NodeList childNodes = curElm.getChildNodes();
            for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
                Node item = childNodes.item(i);
                //stack.push((Element) item);
                nodesToProcess.add(item);
            }
            Collections.reverse(nodesToProcess);
            for (Node node : nodesToProcess) {
                stack.push(node);
            }
        }
        return retvals;
    }
}

From source file:Main.java

/**
 * Appends the given set of parameters to the given query string
 * @param   queryBuilder   The query url string to append the parameters
 * @param   parameters   The parameters to append
 * @return   The modified query url string */
public static void appendUrlWithQueryParameters(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;//from  w w w . j  a  v  a 2s  .  c  om

    //does the query string already has parameters
    boolean hasParams = (queryBuilder.indexOf("?") > 0);

    //iterate and append parameters
    for (Map.Entry<String, Object> pair : parameters.entrySet()) {
        //ignore null values
        if (null == pair.getValue())
            continue;
        //if already has parameters, use the &amp; to append new parameters
        char separator = (hasParams) ? '&' : '?';

        queryBuilder.append(separator + pair.getKey() + "=" + pair.getValue());

        //indicate that now the query has some params
        hasParams = true;
    }
}