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[] rotateNV21(@NonNull final byte[] yuv, final int width, final int height,
        final int rotation, final boolean flipHorizontal) throws IOException {
    if (rotation == 0)
        return yuv;
    if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
        throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
    } else if ((width * height * 3) / 2 != yuv.length) {
        throw new IOException("provided width and height don't jive with the data length (" + yuv.length
                + "). Width: " + width + " height: " + height + " = data length: " + (width * height * 3) / 2);
    }/*w  w w.ja  v  a2  s .com*/

    final byte[] output = new byte[yuv.length];
    final int frameSize = width * height;
    final boolean swap = rotation % 180 != 0;
    final boolean xflip = flipHorizontal ? rotation % 270 == 0 : rotation % 270 != 0;
    final boolean yflip = rotation >= 180;

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            final int yIn = j * width + i;
            final int uIn = frameSize + (j >> 1) * width + (i & ~1);
            final int vIn = uIn + 1;

            final int wOut = swap ? height : width;
            final int hOut = swap ? width : height;
            final int iSwapped = swap ? j : i;
            final int jSwapped = swap ? i : j;
            final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped;
            final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped;

            final int yOut = jOut * wOut + iOut;
            final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
            final int vOut = uOut + 1;

            output[yOut] = (byte) (0xff & yuv[yIn]);
            output[uOut] = (byte) (0xff & yuv[uIn]);
            output[vOut] = (byte) (0xff & yuv[vIn]);
        }
    }
    return output;
}

From source file:Main.java

public static void startActivityForResult(Fragment fragment, Intent intent, int requestCode, Bundle options) {
    if (Build.VERSION.SDK_INT >= 19) {
        if ((requestCode & 0xffff0000) != 0) {
            throw new IllegalArgumentException("Can only use lower 16 bits" + " for requestCode");
        }//  w w  w .j  a  va2 s. c o  m
        if (requestCode != -1) {
            try {
                Field mIndex = Fragment.class.getDeclaredField("mIndex");
                mIndex.setAccessible(true);
                requestCode = ((mIndex.getInt(fragment) + 1) << 16) + (requestCode & 0xffff);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        ActivityCompat.startActivityForResult(fragment.getActivity(), intent, requestCode, options);
    } else {
        fragment.getActivity().startActivityFromFragment(fragment, intent, requestCode);
    }
}

From source file:Main.java

/**
 * Swaps 2 items/*  w  ww.ja  va 2s  .c  o  m*/
 * 
 * @param data
 * @param from
 * @param to
 */
public static <T> void swapItems(final List<T> data, int from, int to) {
    if (from == to)
        return;
    if (!hasElements(data))
        return;
    if (from < 0 || from >= data.size()) {
        throw new IllegalArgumentException("'from' must be within 0 to n-1");
    }
    if (to < 0 || to >= data.size()) {
        throw new IllegalArgumentException("'to' must be within 0 to n-1");
    }

    T temp = data.get(from);
    data.set(from, data.get(to));
    data.set(to, temp);
}

From source file:Main.java

public static Double div(Double v1, Double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/*from w ww .  j a  va2  s . c  o  m*/
    BigDecimal b1 = new BigDecimal(v1.toString());
    BigDecimal b2 = new BigDecimal(v2.toString());
    return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue());
}

From source file:Main.java

public static byte ushortToByte(short value) {
    if (value <= MAX_UNSIGNED_SHORT_VALUE) {
        if (value >= MAX_UNSIGNED_BYTE_VALUE / 2) {
            return (byte) ((~(MAX_UNSIGNED_BYTE_VALUE - value)) + 1);
        } else {/*w  ww  .  j a  va 2 s  .  c o m*/
            return (byte) value;
        }
    } else {
        throw new IllegalArgumentException("Value out of range for a byte");
    }
}

From source file:Main.java

private static <T> Class<T> findClass(String className, Class<?>[] declaredClasses) {
    for (Class<?> klass : declaredClasses) {
        if (klass.getSimpleName().matches(className)) {
            return (Class<T>) klass;
        }/*  www  .  j  a  va 2s  .  com*/
    }
    throw new IllegalArgumentException(String.format("Class name %s not found ", className));
}

From source file:Main.java

/**
 * Converts the byte array into a String based on the specified charset. The charset cannot be null.
 *
 * @param content bytes to convert to a String
 * @param charset the character set of the content
 * @return String containing the converted content
 * @throws IllegalArgumentException if charset is null
 *//*from ww w.  jav  a2 s  .c  om*/
public static String getContentAsString(byte[] content, Charset charset) {
    if (charset == null) {
        throw new IllegalArgumentException("Charset cannot be null");
    }

    return new String(content, charset);
}

From source file:Main.java

/**
 * Takes a date value as used in CPLC Date fields (represented by 2 bytes)
 * /*  www . ja  v a 2  s  .  c  om*/
 * @param paramByte1
 * @param paramByte2
 * @throws IllegalArgumentException
 * @return
 */
public static Date calculateCplcDate(byte[] dateBytes) throws IllegalArgumentException {
    if (dateBytes == null || dateBytes.length != 2) {
        throw new IllegalArgumentException("Error! CLCP Date values consist always of exactly 2 bytes");
    }
    // current time
    Calendar now = Calendar.getInstance();

    int year = now.get(Calendar.YEAR);
    int startYearOfCurrentDecade = year - (year % 10);

    int days = 100 * (dateBytes[0] & 0xF) + 10 * (0xF & dateBytes[1] >>> 4) + (dateBytes[1] & 0xF);

    if (days > 366) {
        throw new IllegalArgumentException("Invalid date (or are we parsing it wrong??)");
    }

    Calendar calculatedDate = Calendar.getInstance();
    calculatedDate.clear();
    calculatedDate.set(Calendar.YEAR, startYearOfCurrentDecade + (0xF & dateBytes[0] >>> 4));
    calculatedDate.set(Calendar.DAY_OF_YEAR, days);
    while (calculatedDate.after(now)) {
        calculatedDate.add(Calendar.YEAR, -10);
    }
    return calculatedDate.getTime();
}

From source file:Main.java

/**
 * get month between minTime and maxTime, including head and tail
 *
 * @param minTime//from  w  w w .j  a va2 s . co  m
 * @param maxTime
 * @return
 */
public static int getMonthNum(long minTime, long maxTime) {
    Calendar min = Calendar.getInstance();
    min.setTimeInMillis(minTime);
    Calendar max = Calendar.getInstance();
    max.setTimeInMillis(maxTime);
    if (max.before(min)) {
        throw new IllegalArgumentException("max date is before min date");
    }
    int minMonth = min.get(Calendar.MONTH) + 1;
    int maxMonth = max.get(Calendar.MONTH) + 1;
    return (max.get(Calendar.YEAR) - min.get(Calendar.YEAR)) * 12 + maxMonth - minMonth + 1;
}

From source file:Main.java

public static long getTimeAsMillisecs(final Object iSize) {
    if (iSize == null)
        throw new IllegalArgumentException("Time is null");

    if (iSize instanceof Number)
        // MILLISECS
        return ((Number) iSize).longValue();

    String time = iSize.toString();

    boolean number = true;
    for (int i = time.length() - 1; i >= 0; --i) {
        if (!Character.isDigit(time.charAt(i))) {
            number = false;//from   w  ww  .j a  v  a  2  s  .c  o  m
            break;
        }
    }

    if (number)
        // MILLISECS
        return Long.parseLong(time);
    else {
        time = time.toUpperCase(Locale.ENGLISH);

        int pos = time.indexOf("MS");
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos));

        pos = time.indexOf("S");
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * SECOND;

        pos = time.indexOf("M");
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * MINUTE;

        pos = time.indexOf("H");
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * HOUR;

        pos = time.indexOf("D");
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * DAY;

        pos = time.indexOf('W');
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * WEEK;

        pos = time.indexOf('Y');
        if (pos > -1)
            return Long.parseLong(time.substring(0, pos)) * YEAR;

        // RE-THROW THE EXCEPTION
        throw new IllegalArgumentException("Time '" + time + "' has a unrecognizable format");
    }
}