Example usage for java.lang Short valueOf

List of usage examples for java.lang Short valueOf

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static Short valueOf(short s) 

Source Link

Document

Returns a Short instance representing the specified short value.

Usage

From source file:com.tibbo.linkserver.plugin.device.file.item.NumericItem.java

/**
 *
 * @param data/*from w  w w  .j a  v  a 2s .c om*/
 * @param offset
 * @return
 */
@Override
public Object bytesToValueRealOffset(byte data[], int offset) {
    offset *= 2;
    if (dataType == 2) {
        return Integer.valueOf((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 3) {
        return Short.valueOf((short) ((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 16) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Short.valueOf(Short.parseShort(sb.toString()));
    }
    if (dataType == 4) {
        return Long.valueOf((long) (data[offset] & 0xff) << 24 | (long) (data[offset + 1] & 0xff) << 16
                | (long) (data[offset + 2] & 0xff) << 8 | (long) (data[offset + 3] & 0xff));
    }
    if (dataType == 5) {
        return Integer.valueOf((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff);
    }
    if (dataType == 6) {
        return Long.valueOf((long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 7) {
        return Integer.valueOf((data[offset + 2] & 0xff) << 24 | (data[offset + 3] & 0xff) << 16
                | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 8) {
        return Float.valueOf(Float.intBitsToFloat((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff));
    }
    if (dataType == 9) {
        return Float.valueOf(Float.intBitsToFloat((data[offset + 2] & 0xff) << 24
                | (data[offset + 3] & 0xff) << 16 | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 17) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Integer.valueOf(Integer.parseInt(sb.toString()));
    }
    if (dataType == 10) {
        byte b9[] = new byte[9];
        System.arraycopy(data, offset, b9, 1, 8);
        return new BigInteger(b9);
    }
    if (dataType == 11) {
        return Long.valueOf((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff));
    }
    if (dataType == 12) {
        byte b9[] = new byte[9];
        b9[1] = data[offset + 6];
        b9[2] = data[offset + 7];
        b9[3] = data[offset + 4];
        b9[4] = data[offset + 5];
        b9[5] = data[offset + 2];
        b9[6] = data[offset + 3];
        b9[7] = data[offset];
        b9[8] = data[offset + 1];
        return new BigInteger(b9);
    }
    if (dataType == 13) {
        return Long.valueOf((long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 14) {
        return Double.valueOf(Double
                .longBitsToDouble((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                        | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                        | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                        | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff)));
    }
    if (dataType == 15) {
        return Double.valueOf(Double.longBitsToDouble(
                (long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                        | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                        | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                        | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff)));
    } else {
        throw new RuntimeException(
                (new StringBuilder()).append("Unsupported data type: ").append(dataType).toString());
    }
}

From source file:org.hyperic.hq.product.jmx.MBeanUtil.java

private static void initConverters() {
    addConverter(Object.class, new Converter() {
        public Object convert(String param) {
            return param;
        }/*from  w w  w.  j  a  va  2 s .c o m*/
    });

    addConverter(Short.class, new Converter() {
        public Object convert(String param) {
            return Short.valueOf(param);
        }
    });

    addConverter(Integer.class, new Converter() {
        public Object convert(String param) {
            return Integer.valueOf(param);
        }
    });

    addConverter(Long.class, new Converter() {
        public Object convert(String param) {
            return Long.valueOf(param);
        }
    });

    addConverter(Double.class, new Converter() {
        public Object convert(String param) {
            return Double.valueOf(param);
        }
    });

    addConverter(Boolean.class, new Converter() {
        public Object convert(String param) {
            return Boolean.valueOf(param);
        }
    });

    addConverter(File.class, new Converter() {
        public Object convert(String param) {
            return new File(param);
        }
    });

    addConverter(URL.class, new Converter() {
        public Object convert(String param) {
            try {
                return new URL(param);
            } catch (MalformedURLException e) {
                throw invalid(param, e);
            }
        }
    });

    addConverter(ObjectName.class, new Converter() {
        public Object convert(String param) {
            try {
                return new ObjectName(param);
            } catch (MalformedObjectNameException e) {
                throw invalid(param, e);
            }
        }
    });

    addConverter(List.class, new ListConverter() {
        public Object convert(String[] params) {
            return Arrays.asList(params);
        }
    });

    addConverter(String[].class, new ListConverter() {
        public Object convert(String[] params) {
            return params;
        }
    });

    addConverter(Long[].class, new ListConverter() {
        public Object convert(String[] params) {
            Long[] args = new Long[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Long.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Integer[].class, new ListConverter() {
        public Object convert(String[] params) {
            Integer[] args = new Integer[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Integer.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Double[].class, new ListConverter() {
        public Object convert(String[] params) {
            Double[] args = new Double[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Double.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Short[].class, new ListConverter() {
        public Object convert(String[] params) {
            Short[] args = new Short[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Short.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Boolean[].class, new ListConverter() {
        public Object convert(String[] params) {
            Boolean[] args = new Boolean[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Boolean.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(long[].class, new ListConverter() {
        public Object convert(String[] params) {
            long[] args = new long[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Long.parseLong(params[i]);
            }
            return args;
        }
    });

    addConverter(int[].class, new ListConverter() {
        public Object convert(String[] params) {
            int[] args = new int[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Integer.parseInt(params[i]);
            }
            return args;
        }
    });

    addConverter(double[].class, new ListConverter() {
        public Object convert(String[] params) {
            double[] args = new double[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Double.parseDouble(params[i]);
            }
            return args;
        }
    });

    addConverter(short[].class, new ListConverter() {
        public Object convert(String[] params) {
            short[] args = new short[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Short.parseShort(params[i]);
            }
            return args;
        }
    });

    addConverter(boolean[].class, new ListConverter() {
        public Object convert(String[] params) {
            boolean[] args = new boolean[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = params[i].equals("true") ? true : false;
            }
            return args;
        }
    });

    Class[][] aliases = { { String.class, Object.class }, { Short.TYPE, Short.class },
            { Integer.TYPE, Integer.class }, { Long.TYPE, Long.class }, { Double.TYPE, Double.class },
            { Boolean.TYPE, Boolean.class }, };

    for (int i = 0; i < aliases.length; i++) {
        addConverter(aliases[i][0], aliases[i][1]);
    }
}

From source file:com.alibaba.dubboadmin.web.mvc.RouterController.java

private Object convertPrimitive(Class<?> cls, String value) {
    if (cls == boolean.class || cls == Boolean.class) {
        return value == null || value.length() == 0 ? false : Boolean.valueOf(value);
    } else if (cls == byte.class || cls == Byte.class) {
        return value == null || value.length() == 0 ? 0 : Byte.valueOf(value);
    } else if (cls == char.class || cls == Character.class) {
        return value == null || value.length() == 0 ? '\0' : value.charAt(0);
    } else if (cls == short.class || cls == Short.class) {
        return value == null || value.length() == 0 ? 0 : Short.valueOf(value);
    } else if (cls == int.class || cls == Integer.class) {
        return value == null || value.length() == 0 ? 0 : Integer.valueOf(value);
    } else if (cls == long.class || cls == Long.class) {
        return value == null || value.length() == 0 ? 0 : Long.valueOf(value);
    } else if (cls == float.class || cls == Float.class) {
        return value == null || value.length() == 0 ? 0 : Float.valueOf(value);
    } else if (cls == double.class || cls == Double.class) {
        return value == null || value.length() == 0 ? 0 : Double.valueOf(value);
    }//from  w ww.  java  2  s .  co m
    return value;
}

From source file:com.igorbaiborodine.example.mybatis.customer.CustomerServiceImpl.java

@Override
@Transactional(readOnly = true)/*from w ww .  j a  v a2s  . c  o m*/
public Customer findCustomer(short customerId_) throws ServiceException {

    Customer customer = null;
    try {
        customer = _customerMapper.selectByPrimaryKey(Short.valueOf(customerId_));
    } catch (Throwable t) {
        String msg = String.format("Cannot find customer with id [%d]", customerId_);
        throw new ServiceException(msg, t);
    }
    return customer;
}

From source file:org.apache.beam.sdk.extensions.sql.impl.schema.BeamTableUtils.java

public static Object autoCastField(int fieldType, Object rawObj) {
    if (rawObj == null) {
        return null;
    }/*w ww . j  av a  2  s .  c  om*/

    SqlTypeName columnType = CalciteUtils.toCalciteType(fieldType);
    // auto-casting for numberics
    if ((rawObj instanceof String && SqlTypeName.NUMERIC_TYPES.contains(columnType))
            || (rawObj instanceof BigDecimal && columnType != SqlTypeName.DECIMAL)) {
        String raw = rawObj.toString();
        switch (columnType) {
        case TINYINT:
            return Byte.valueOf(raw);
        case SMALLINT:
            return Short.valueOf(raw);
        case INTEGER:
            return Integer.valueOf(raw);
        case BIGINT:
            return Long.valueOf(raw);
        case FLOAT:
            return Float.valueOf(raw);
        case DOUBLE:
            return Double.valueOf(raw);
        default:
            throw new UnsupportedOperationException(
                    String.format("Column type %s is not supported yet!", columnType));
        }
    } else if (SqlTypeName.CHAR_TYPES.contains(columnType)) {
        // convert NlsString to String
        if (rawObj instanceof NlsString) {
            return ((NlsString) rawObj).getValue();
        } else {
            return rawObj;
        }
    } else {
        return rawObj;
    }
}

From source file:org.hydracache.server.IdentityXmlMarshaller.java

public Identity readObject(String xml) throws IOException {
    try {//  w  ww.jav  a  2  s  . c  om
        if (StringUtils.isBlank(xml))
            return Identity.NULL_IDENTITY;

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(new StringReader(xml));

        Element idElement = doc.getRootElement();

        String addressAttributeValue = idElement.getChildText(ADDRESS_ATTRIBUTE_NAME);
        String portAttributeValue = idElement.getChildText(PORT_ATTRIBUTE_NAME);

        if (StringUtils.isBlank(addressAttributeValue) || StringUtils.isBlank(portAttributeValue))
            return Identity.NULL_IDENTITY;

        byte[] address = Base64.decodeBase64(addressAttributeValue);
        short port = Short.valueOf(portAttributeValue);

        return new Identity(Inet4Address.getByAddress(address), port);
    } catch (Exception jdex) {
        log.error("Failed to parse input xml", jdex);
        return Identity.NULL_IDENTITY;
    }
}

From source file:org.lable.oss.dynamicconfig.core.commonsconfiguration.ConcurrentConfigurationTest.java

@Test
public void testMethodWrappers() {
    CombinedConfiguration mockConfiguration = mock(CombinedConfiguration.class);
    Configuration concurrentConfiguration = new ConcurrentConfiguration(mockConfiguration, null);

    concurrentConfiguration.subset("subset");
    concurrentConfiguration.isEmpty();//from  w  w  w. j av  a  2s  .c  o m
    concurrentConfiguration.containsKey("key");
    concurrentConfiguration.getProperty("getprop");
    concurrentConfiguration.getKeys("getkeys");
    concurrentConfiguration.getKeys();
    concurrentConfiguration.getProperties("getprops");
    concurrentConfiguration.getBoolean("getboolean1");
    concurrentConfiguration.getBoolean("getboolean2", true);
    concurrentConfiguration.getBoolean("getboolean3", Boolean.FALSE);
    concurrentConfiguration.getByte("getbyte1");
    concurrentConfiguration.getByte("getbyte2", (byte) 0);
    concurrentConfiguration.getByte("getbyte3", Byte.valueOf((byte) 0));
    concurrentConfiguration.getDouble("getdouble1");
    concurrentConfiguration.getDouble("getdouble2", 0.2);
    concurrentConfiguration.getDouble("getdouble3", Double.valueOf(0.2));
    concurrentConfiguration.getFloat("getfloat1");
    concurrentConfiguration.getFloat("getfloat2", 0f);
    concurrentConfiguration.getFloat("getfloat3", Float.valueOf(0f));
    concurrentConfiguration.getInt("getint1");
    concurrentConfiguration.getInt("getint2", 0);
    concurrentConfiguration.getInteger("getint3", 0);
    concurrentConfiguration.getLong("getlong1");
    concurrentConfiguration.getLong("getlong2", 0L);
    concurrentConfiguration.getLong("getlong3", Long.valueOf(0L));
    concurrentConfiguration.getShort("getshort1");
    concurrentConfiguration.getShort("getshort2", (short) 0);
    concurrentConfiguration.getShort("getshort3", Short.valueOf((short) 0));
    concurrentConfiguration.getBigDecimal("getbigd1");
    concurrentConfiguration.getBigDecimal("getbigd2", BigDecimal.valueOf(0.4));
    concurrentConfiguration.getBigInteger("getbigi1");
    concurrentConfiguration.getBigInteger("getbigi2", BigInteger.valueOf(2L));
    concurrentConfiguration.getString("getstring1");
    concurrentConfiguration.getString("getstring2", "def");
    concurrentConfiguration.getStringArray("stringarray");
    concurrentConfiguration.getList("getlist1");
    concurrentConfiguration.getList("getlist2", Arrays.asList("a", "b"));

    verify(mockConfiguration, times(1)).subset("subset");
    verify(mockConfiguration, times(1)).isEmpty();
    verify(mockConfiguration, times(1)).containsKey("key");
    verify(mockConfiguration, times(1)).getProperty("getprop");
    verify(mockConfiguration, times(1)).getKeys("getkeys");
    verify(mockConfiguration, times(1)).getKeys();
    verify(mockConfiguration, times(1)).getProperties("getprops");
    verify(mockConfiguration, times(1)).getBoolean("getboolean1");
    verify(mockConfiguration, times(1)).getBoolean("getboolean2", true);
    verify(mockConfiguration, times(1)).getBoolean("getboolean3", Boolean.FALSE);
    verify(mockConfiguration, times(1)).getByte("getbyte1");
    verify(mockConfiguration, times(1)).getByte("getbyte2", (byte) 0);
    verify(mockConfiguration, times(1)).getByte("getbyte3", Byte.valueOf((byte) 0));
    verify(mockConfiguration, times(1)).getDouble("getdouble1");
    verify(mockConfiguration, times(1)).getDouble("getdouble2", 0.2);
    verify(mockConfiguration, times(1)).getDouble("getdouble3", Double.valueOf(0.2));
    verify(mockConfiguration, times(1)).getFloat("getfloat1");
    verify(mockConfiguration, times(1)).getFloat("getfloat2", 0f);
    verify(mockConfiguration, times(1)).getFloat("getfloat3", Float.valueOf(0f));
    verify(mockConfiguration, times(1)).getInt("getint1");
    verify(mockConfiguration, times(1)).getInt("getint2", 0);
    verify(mockConfiguration, times(1)).getInteger("getint3", Integer.valueOf(0));
    verify(mockConfiguration, times(1)).getLong("getlong1");
    verify(mockConfiguration, times(1)).getLong("getlong2", 0L);
    verify(mockConfiguration, times(1)).getLong("getlong3", Long.valueOf(0L));
    verify(mockConfiguration, times(1)).getShort("getshort1");
    verify(mockConfiguration, times(1)).getShort("getshort2", (short) 0);
    verify(mockConfiguration, times(1)).getShort("getshort3", Short.valueOf((short) 0));
    verify(mockConfiguration, times(1)).getBigDecimal("getbigd1");
    verify(mockConfiguration, times(1)).getBigDecimal("getbigd2", BigDecimal.valueOf(0.4));
    verify(mockConfiguration, times(1)).getBigInteger("getbigi1");
    verify(mockConfiguration, times(1)).getBigInteger("getbigi2", BigInteger.valueOf(2L));
    verify(mockConfiguration, times(1)).getString("getstring1");
    verify(mockConfiguration, times(1)).getString("getstring2", "def");
    verify(mockConfiguration, times(1)).getStringArray("stringarray");
    verify(mockConfiguration, times(1)).getList("getlist1");
    verify(mockConfiguration, times(1)).getList("getlist2", Arrays.asList("a", "b"));
}

From source file:org.executequery.databaseobjects.impl.TableColumnIndex.java

public void setMetaData(Map<String, String> metaData) {
    this.metaData = metaData;
    for (String key : this.metaData.keySet()) {

        if (StringUtils.equalsIgnoreCase("TYPE", key)) {

            Short value = Short.valueOf(this.metaData.get(key));
            this.metaData.put(key, translateType(value));
        }/*from   w ww .  ja  v  a2 s  .  com*/

    }
}

From source file:com.p5solutions.core.jpa.orm.ConversionUtilityImpl.java

/**
 * Convert string.//from   w  w  w .  j a  va2s .  c  om
 * 
 * @param value
 *          the value
 * @param clazz
 *          the clazz
 * @return the object
 */
protected Object convertString(String value, Class<?> clazz) {
    if (ReflectionUtility.isBooleanClass(clazz)) {
        return Boolean.valueOf(value);
    } else if (ReflectionUtility.isShortClass(clazz)) {
        return Short.valueOf(value);
    } else if (ReflectionUtility.isIntegerClass(clazz)) {
        return Integer.valueOf(value);
    } else if (ReflectionUtility.isLongClass(clazz)) {
        return Long.valueOf(value);
    } else if (ReflectionUtility.isFloatClass(clazz)) {
        return Float.valueOf(value);
    } else if (ReflectionUtility.isDoubleClass(clazz)) {
        return Double.valueOf(value);
    } else if (ReflectionUtility.isBigDecimalClass(clazz)) {
        return new BigDecimal(value);
    } else if (ReflectionUtility.isBigIntegerClass(clazz)) {
        return new BigInteger(value);
    } else if (ReflectionUtility.isByteClass(clazz)) {
        return Byte.valueOf(value);
    } else if (ReflectionUtility.isDateOrTimestamp(clazz)) {
        try {
            return DATE_FORMAT.parse(value);
        } catch (ParseException e) {
            logger.error("Unable to parse " + value + " using date formatter " + DATE_FORMAT_STRING);
            return value;
        }
    } else {
        logger.warn("Unable to identify appropriate conversion strategy for value " + value
                + " using class type " + clazz);
    }

    return value;
}

From source file:com.workday.autoparse.xml.demo.XmlParserTest.java

@Test
public void testParse() throws UnknownElementException, ParseException, UnexpectedChildException {

    XmlStreamParser parser = XmlStreamParserFactory.newXmlStreamParser();

    InputStream in = getInputStreamOf("input.xml");
    DemoModel model = (DemoModel) parser.parseStream(in);

    assertTrue(model.myBoxedBoolean);/*  w  w  w.  j  a  v a2s .  c  o  m*/
    assertTrue(model.myPrimitiveBoolean);
    assertEquals(new BigDecimal(0.5), model.myBigDecimal);
    assertEquals(BigInteger.ONE, model.myBigInteger);
    assertEquals(2, model.myPrimitiveByte);
    assertEquals(Byte.valueOf((byte) 4), model.myBoxedByte);
    assertEquals('a', model.myPrimitiveChar);
    assertEquals(Character.valueOf('b'), model.myBoxedChar);
    assertEquals(5.5, model.myPrimitiveDouble, DOUBLE_E);
    assertEquals(Double.valueOf(6.5), model.myBoxedDouble);
    assertEquals(7.5f, model.myPrimitiveFloat, FLOAT_E);
    assertEquals(Float.valueOf(8.5f), model.myBoxedFloat);
    assertEquals(9, model.myPrimitiveInt);
    assertEquals(Integer.valueOf(10), model.myBoxedInt);
    assertEquals(11, model.myPrimitiveLong);
    assertEquals(Long.valueOf(12), model.myBoxedLong);
    assertEquals(13, model.myPrimitiveShort);
    assertEquals(Short.valueOf((short) 15), model.myBoxedShort);
    assertEquals("Bob", model.myString);
    assertTrue(model.myChildModel != null);

    assertEquals("My_String_Value", model.myChildModel.myString);
    assertEquals(5, model.myChildModel.myInt);

    assertEquals(3, model.repeatedChildModels.size());

    assertEquals("a", model.repeatedChildModels.get(0).value);
    assertEquals("I am some text.", model.repeatedChildModels.get(0).textContent);

    assertEquals("b", model.repeatedChildModels.get(1).value);
    assertNull(model.repeatedChildModels.get(1).textContent);

    assertEquals("c", model.repeatedChildModels.get(2).value);
}