Example usage for java.lang Short MIN_VALUE

List of usage examples for java.lang Short MIN_VALUE

Introduction

In this page you can find the example usage for java.lang Short MIN_VALUE.

Prototype

short MIN_VALUE

To view the source code for java.lang Short MIN_VALUE.

Click Source Link

Document

A constant holding the minimum value a short can have, -215.

Usage

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a short primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)vv//w  w  w . ja  v a2s  .  com
 *@return the converted Short value.
 */
public static Short formatShort(String value, Locale locale) {
    Short result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) {
                result = new Short(num.shortValue());
            }
        }
    }

    return result;
}

From source file:edu.stanford.slac.archiverappliance.PB.data.StatusSeverityTest.java

private DBR getJCASampleValue(ArchDBRTypes type, int value, int severity, int status) {
    switch (type) {
    case DBR_SCALAR_STRING:
        DBR_TIME_String retvalss = new DBR_TIME_String(new String[] { Integer.toString(value) });
        retvalss.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalss.setSeverity(severity);//from   w  ww. j  ava2 s  .co  m
        retvalss.setStatus(status);
        return retvalss;
    case DBR_SCALAR_SHORT:
        DBR_TIME_Short retvalsh;
        if (0 <= value && value < 1000) {
            // Check for some numbers around the minimum value
            retvalsh = new DBR_TIME_Short(new short[] { (short) (Short.MIN_VALUE + value) });
        } else if (1000 <= value && value < 2000) {
            // Check for some numbers around the maximum value
            retvalsh = new DBR_TIME_Short(new short[] { (short) (Short.MAX_VALUE - (value - 1000)) });
        } else {
            // Check for some numbers around 0
            retvalsh = new DBR_TIME_Short(new short[] { (short) (value - 2000) });
        }
        retvalsh.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalsh.setSeverity(severity);
        retvalsh.setStatus(status);
        return retvalsh;
    case DBR_SCALAR_FLOAT:
        DBR_TIME_Float retvalfl;
        if (0 <= value && value < 1000) {
            // Check for some numbers around the minimum value
            retvalfl = new DBR_TIME_Float(new float[] { Float.MIN_VALUE + value });
        } else if (1000 <= value && value < 2000) {
            // Check for some numbers around the maximum value
            retvalfl = new DBR_TIME_Float(new float[] { Float.MAX_VALUE - (value - 1000) });
        } else {
            // Check for some numbers around 0. Divide by a large number to make sure we cater to the number of precision digits
            retvalfl = new DBR_TIME_Float(new float[] { (value - 2000.0f) / value });
        }
        retvalfl.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalfl.setSeverity(severity);
        retvalfl.setStatus(status);
        return retvalfl;
    case DBR_SCALAR_ENUM:
        DBR_TIME_Enum retvalen;
        retvalen = new DBR_TIME_Enum(new short[] { (short) (value) });
        retvalen.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalen.setSeverity(severity);
        retvalen.setStatus(status);
        return retvalen;
    case DBR_SCALAR_BYTE:
        DBR_TIME_Byte retvalby;
        retvalby = new DBR_TIME_Byte(new byte[] { ((byte) (value % 255)) });
        retvalby.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalby.setSeverity(severity);
        retvalby.setStatus(status);
        return retvalby;
    case DBR_SCALAR_INT:
        DBR_TIME_Int retvalint;
        if (0 <= value && value < 1000) {
            // Check for some numbers around the minimum value
            retvalint = new DBR_TIME_Int(new int[] { Integer.MIN_VALUE + value });
        } else if (1000 <= value && value < 2000) {
            // Check for some numbers around the maximum value
            retvalint = new DBR_TIME_Int(new int[] { Integer.MAX_VALUE - (value - 1000) });
        } else {
            // Check for some numbers around 0
            retvalint = new DBR_TIME_Int(new int[] { (value - 2000) });
        }
        retvalint.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvalint.setSeverity(severity);
        retvalint.setStatus(status);
        return retvalint;
    case DBR_SCALAR_DOUBLE:
        DBR_TIME_Double retvaldb;
        if (0 <= value && value < 1000) {
            // Check for some numbers around the minimum value
            retvaldb = new DBR_TIME_Double(new double[] { (Double.MIN_VALUE + value) });
        } else if (1000 <= value && value < 2000) {
            // Check for some numbers around the maximum value
            retvaldb = new DBR_TIME_Double(new double[] { (Double.MAX_VALUE - (value - 1000)) });
        } else {
            // Check for some numbers around 0. Divide by a large number to make sure we cater to the number of precision digits
            retvaldb = new DBR_TIME_Double(new double[] { ((value - 2000.0) / (value * 1000000)) });
        }
        retvaldb.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvaldb.setSeverity(severity);
        retvaldb.setStatus(status);
        return retvaldb;
    case DBR_WAVEFORM_STRING:
        DBR_TIME_String retvst;
        // Varying number of copies of a typical value
        retvst = new DBR_TIME_String(
                Collections.nCopies(value, Integer.toString(value)).toArray(new String[0]));
        retvst.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvst.setSeverity(severity);
        retvst.setStatus(status);
        return retvst;
    case DBR_WAVEFORM_SHORT:
        DBR_TIME_Short retvsh;
        retvsh = new DBR_TIME_Short(
                ArrayUtils.toPrimitive(Collections.nCopies(1, (short) value).toArray(new Short[0])));
        retvsh.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvsh.setSeverity(severity);
        retvsh.setStatus(status);
        return retvsh;
    case DBR_WAVEFORM_FLOAT:
        DBR_TIME_Float retvf;
        // Varying number of copies of a typical value
        retvf = new DBR_TIME_Float(ArrayUtils.toPrimitive(
                Collections.nCopies(value, (float) Math.cos(value * Math.PI / 3600)).toArray(new Float[0])));
        retvf.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvf.setSeverity(severity);
        retvf.setStatus(status);
        return retvf;
    case DBR_WAVEFORM_ENUM:
        DBR_TIME_Enum retven;
        retven = new DBR_TIME_Enum(
                ArrayUtils.toPrimitive(Collections.nCopies(1024, (short) value).toArray(new Short[0])));
        retven.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retven.setSeverity(severity);
        retven.setStatus(status);
        return retven;
    case DBR_WAVEFORM_BYTE:
        DBR_TIME_Byte retvb;
        // Large number of elements in the array
        retvb = new DBR_TIME_Byte(ArrayUtils
                .toPrimitive(Collections.nCopies(65536 * value, ((byte) (value % 255))).toArray(new Byte[0])));
        retvb.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvb.setSeverity(severity);
        retvb.setStatus(status);
        return retvb;
    case DBR_WAVEFORM_INT:
        DBR_TIME_Int retvint;
        // Varying number of copies of a typical value
        retvint = new DBR_TIME_Int(
                ArrayUtils.toPrimitive(Collections.nCopies(value, value * value).toArray(new Integer[0])));
        retvint.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvint.setSeverity(severity);
        retvint.setStatus(status);
        return retvint;
    case DBR_WAVEFORM_DOUBLE:
        DBR_TIME_Double retvd;
        // Varying number of copies of a typical value
        retvd = new DBR_TIME_Double(ArrayUtils.toPrimitive(
                Collections.nCopies(value, Math.sin(value * Math.PI / 3600)).toArray(new Double[0])));
        retvd.setTimeStamp(convertSecondsIntoYear2JCATimeStamp(value));
        retvd.setSeverity(severity);
        retvd.setStatus(status);
        return retvd;
    case DBR_V4_GENERIC_BYTES:
        throw new RuntimeException("Currently don't support " + type + " when generating sample data");
    default:
        throw new RuntimeException("We seemed to have missed a DBR type when generating sample data");
    }
}

From source file:org.apache.hadoop.hive.accumulo.AccumuloTestSetup.java

protected void createAccumuloTable(Connector conn)
        throws TableExistsException, TableNotFoundException, AccumuloException, AccumuloSecurityException {
    TableOperations tops = conn.tableOperations();
    if (tops.exists(TABLE_NAME)) {
        tops.delete(TABLE_NAME);/*  w  w  w. ja v  a2 s .c o  m*/
    }

    tops.create(TABLE_NAME);

    boolean[] booleans = new boolean[] { true, false, true };
    byte[] bytes = new byte[] { Byte.MIN_VALUE, -1, Byte.MAX_VALUE };
    short[] shorts = new short[] { Short.MIN_VALUE, -1, Short.MAX_VALUE };
    int[] ints = new int[] { Integer.MIN_VALUE, -1, Integer.MAX_VALUE };
    long[] longs = new long[] { Long.MIN_VALUE, -1, Long.MAX_VALUE };
    String[] strings = new String[] { "Hadoop, Accumulo", "Hive", "Test Strings" };
    float[] floats = new float[] { Float.MIN_VALUE, -1.0F, Float.MAX_VALUE };
    double[] doubles = new double[] { Double.MIN_VALUE, -1.0, Double.MAX_VALUE };
    HiveDecimal[] decimals = new HiveDecimal[] { HiveDecimal.create("3.14159"), HiveDecimal.create("2.71828"),
            HiveDecimal.create("0.57721") };
    Date[] dates = new Date[] { Date.valueOf("2014-01-01"), Date.valueOf("2014-03-01"),
            Date.valueOf("2014-05-01") };
    Timestamp[] timestamps = new Timestamp[] { new Timestamp(50), new Timestamp(100), new Timestamp(150) };

    BatchWriter bw = conn.createBatchWriter(TABLE_NAME, new BatchWriterConfig());
    final String cf = "cf";
    try {
        for (int i = 0; i < 3; i++) {
            Mutation m = new Mutation("key-" + i);
            m.put(cf, "cq-boolean", Boolean.toString(booleans[i]));
            m.put(cf.getBytes(), "cq-byte".getBytes(), new byte[] { bytes[i] });
            m.put(cf, "cq-short", Short.toString(shorts[i]));
            m.put(cf, "cq-int", Integer.toString(ints[i]));
            m.put(cf, "cq-long", Long.toString(longs[i]));
            m.put(cf, "cq-string", strings[i]);
            m.put(cf, "cq-float", Float.toString(floats[i]));
            m.put(cf, "cq-double", Double.toString(doubles[i]));
            m.put(cf, "cq-decimal", decimals[i].toString());
            m.put(cf, "cq-date", dates[i].toString());
            m.put(cf, "cq-timestamp", timestamps[i].toString());

            bw.addMutation(m);
        }
    } finally {
        bw.close();
    }
}

From source file:org.jts.eclipse.conversion.cjsidl.ConversionUtil.java

/**
 * converts a range of values to a JSIDL type.  This is used where the CJSIDL
 * grammar does not include type in the definition of a range.
 * @param lowerLim - the lower limit of the range
 * @param upperLim - the upper limit of the range
 * @return - a string representing a JSIDL data type that can hold the upper and 
 * lower limits.//from  w w  w.  ja  v a  2 s .c  o  m
 */
public static String rangeToJSIDLType(long lowerLim, long upperLim) {
    long range = upperLim - lowerLim;
    String type = "unsigned byte";
    if (range >= Byte.MIN_VALUE && range <= Byte.MAX_VALUE) {
        type = "unsigned byte";
    } else if (range >= Short.MIN_VALUE && range <= Short.MAX_VALUE) {
        type = "unsigned short integer";
    } else if (range >= Integer.MIN_VALUE && range <= Integer.MAX_VALUE) {
        type = "unsigned integer";
    } else {
        type = "unsigned long integer";
    }

    return type;
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n//from   ww w . j a  v a  2 s.com
 * @return
 */
public static short shortValueExact(Number n) {
    if (n instanceof Short || n instanceof Byte)
        return n.shortValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE)
            return (short) l;
    }
    throw new NumberFormatException();
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a short primitive.
 * /*from www. j  a  v a 2  s  .c o  m*/
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)vv
 * @return the converted Short value.
 */
public static Short formatShort(String value, Locale locale) {
    Short result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) {
                result = new Short(num.shortValue());
            }
        }
    }

    return result;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a short primitive.
 * //  w w w. ja  v a  2  s  .  com
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if
 *            null)vv
 * @return the converted Short value.
 */
public static Short formatShort(String value, Locale locale) {
    Short result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) {
                result = Short.valueOf(num.shortValue());
            }
        }
    }

    return result;
}

From source file:ddf.catalog.data.dynamic.impl.DynamicMetacardImplTest.java

@Test
public void testSetAttribute() throws Exception {
    Attribute attribute = new AttributeImpl(STRING, "abc");
    metacard.setAttribute(attribute);//ww w . jav  a  2 s .c  om
    assertEquals("abc", baseBean.get(STRING));

    attribute = new AttributeImpl(BOOLEAN, true);
    metacard.setAttribute(attribute);
    assertEquals(true, baseBean.get(BOOLEAN));

    Date d = new Date(System.currentTimeMillis());
    attribute = new AttributeImpl(DATE, d);
    metacard.setAttribute(attribute);
    assertEquals(d, baseBean.get(DATE));

    attribute = new AttributeImpl(SHORT, Short.MIN_VALUE);
    metacard.setAttribute(attribute);
    assertEquals(Short.MIN_VALUE, baseBean.get(SHORT));

    attribute = new AttributeImpl(INTEGER, Integer.MAX_VALUE);
    metacard.setAttribute(attribute);
    assertEquals(Integer.MAX_VALUE, baseBean.get(INTEGER));

    attribute = new AttributeImpl(LONG, Long.MAX_VALUE);
    metacard.setAttribute(attribute);
    assertEquals(Long.MAX_VALUE, baseBean.get(LONG));

    attribute = new AttributeImpl(FLOAT, Float.MAX_VALUE);
    metacard.setAttribute(attribute);
    assertEquals(Float.MAX_VALUE, baseBean.get(FLOAT));

    attribute = new AttributeImpl(DOUBLE, Double.MAX_VALUE);
    metacard.setAttribute(attribute);
    assertEquals(Double.MAX_VALUE, baseBean.get(DOUBLE));

    Byte[] bytes = new Byte[] { 0x00, 0x01, 0x02, 0x03 };
    attribute = new AttributeImpl(BINARY, bytes);
    metacard.setAttribute(attribute);
    assertEquals(bytes, baseBean.get(BINARY));

    attribute = new AttributeImpl(XML, XML_STRING);
    metacard.setAttribute(attribute);
    assertEquals(XML_STRING, baseBean.get(XML));

    attribute = new AttributeImpl(OBJECT, XML_STRING);
    metacard.setAttribute(attribute);
    assertEquals(XML_STRING, baseBean.get(OBJECT));

    List<Serializable> list = new ArrayList<>();
    list.add("123");
    list.add("234");
    list.add("345");
    attribute = new AttributeImpl(STRING_LIST, list);
    metacard.setAttribute(attribute);
    assertEquals(list, baseBean.get(STRING_LIST));

}

From source file:org.mule.util.NumberUtils.java

@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }//from   www . j ava 2s .  c  o m
        return (T) new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return (T) new Long(number.longValue());
    } else if (targetClass.equals(BigInteger.class)) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (targetClass.equals(Float.class)) {
        return (T) new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return (T) new Double(number.doubleValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // always use BigDecimal(String) here to avoid unpredictability of
        // BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * <p>Returns a bitmap that visualizes the given waveform (byte array),
 * i.e. a sequence of 16-bit integers.</p>
 *
 * TODO: show to high/low points in other color
 * TODO: show end pause data with another color
 *///from  ww w. j a  va 2s.c om
public static Bitmap drawWaveform(byte[] waveBuffer, int w, int h, int start, int end) {
    final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    final Canvas c = new Canvas(b);
    final Paint paint = new Paint();
    paint.setColor(0xFFFFFFFF); // 0xRRGGBBAA
    paint.setAntiAlias(true);
    paint.setStrokeWidth(0);

    final Paint redPaint = new Paint();
    redPaint.setColor(0xFF000080);
    redPaint.setAntiAlias(true);
    redPaint.setStrokeWidth(0);

    final ShortBuffer buf = ByteBuffer.wrap(waveBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    buf.position(0);

    final int numSamples = waveBuffer.length / 2;
    //final int delay = (SAMPLING_RATE * 100 / 1000);
    final int delay = 0;
    int endIndex = end / 2 + delay;
    if (end == 0 || endIndex >= numSamples) {
        endIndex = numSamples;
    }
    int index = start / 2 - delay;
    if (index < 0) {
        index = 0;
    }
    final int size = endIndex - index;
    int numSamplePerPixel = 32;
    int delta = size / (numSamplePerPixel * w);
    if (delta == 0) {
        numSamplePerPixel = size / w;
        delta = 1;
    }

    final float scale = 3.5f / 65536.0f;
    // do one less column to make sure we won't read past
    // the buffer.
    try {
        for (int i = 0; i < w - 1; i++) {
            final float x = i;
            for (int j = 0; j < numSamplePerPixel; j++) {
                final short s = buf.get(index);
                final float y = (h / 2) - (s * h * scale);
                if (s > Short.MAX_VALUE - 10 || s < Short.MIN_VALUE + 10) {
                    // TODO: make it work
                    c.drawPoint(x, y, redPaint);
                } else {
                    c.drawPoint(x, y, paint);
                }
                index += delta;
            }
        }
    } catch (IndexOutOfBoundsException e) {
        // this can happen, but we don't care
    }

    return b;
}