Example usage for java.lang Short decode

List of usage examples for java.lang Short decode

Introduction

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

Prototype

public static Short decode(String nm) throws NumberFormatException 

Source Link

Document

Decodes a String into a Short .

Usage

From source file:Main.java

public static void main(String[] args) {

    System.out.println("Decimal 10:" + Short.decode("10"));
    System.out.println("Octal 10:" + Short.decode("010"));
    System.out.println("Hex F:" + Short.decode("0XF"));

    System.out.println("Negative Hex F:" + Short.decode("-0XF"));
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 * @param text the text to convert/*from w ww .  j  a  va  2  s  .c o m*/
 * @param targetClass the target class to parse into
 * @return the parsed number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class<?> targetClass) {
    //   Assert.notNull(text, "Text must not be null");
    //Assert.notNull(targetClass, "Target class must not be null");

    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:org.onosproject.drivers.microsemi.yang.utils.CeVlanMapUtils.java

/**
 * Calculate the ceVlanMap in to a Set of values.
 *
 * From Yang description// w ww  .  ja  v  a  2s. c o  m
 *   "This object indicates the CE-VLANs associated with the specific
 *   EVC on a UNI. CE-VLAN IDs have value of 0 to 4095. The CE-VLAN ID
 *   list can be a single value or multiple values separated by a delimiter.
 *   Some valid values are: '100', '1:10', '10,20,30', '1:4095'. In the
 *   first example only CE-VLAN ID 100 is associated with the VLAN map.
 *   In the second example the CE-VLAN map includes CE-VLAN IDs 1 through
 *   10 (range of values). The third example indicates three separate values
 *   that make up the CE-VLAN map. The last example indicates all CE-VLAN IDs
 *   are included in the map (range of values). ";
 *  reference
 *   "[MEF 6.1] 6.1; [MEF 7.2] 6.2.1.3";
 * @param ceVlanMap A list of vlan id's in the format described above
 * @return A set of vlan ids
 */
public static Short[] getVlanSet(String ceVlanMap) {
    if (ceVlanMap == null || ceVlanMap.isEmpty()) {
        return new Short[0];
    }
    Set<Short> ceVlanSet = new TreeSet<Short>();

    String[] ceVlanMapCommas = ceVlanMap.split(",");
    for (String ceVlanMapComma : ceVlanMapCommas) {
        String[] ceVlanMapColon = ceVlanMapComma.split(":");
        if (ceVlanMapColon.length == 1) {
            ceVlanSet.add(Short.decode(ceVlanMapColon[0]));
        } else {
            short start = Short.decode(ceVlanMapColon[0]);
            short end = Short.decode(ceVlanMapColon[1]);
            if ((start < 0 || end > 4095)) {
                return null;
            } else {
                for (short i = start; i <= end; i++) {
                    ceVlanSet.add(i);
                }
            }
        }
    }

    return ceVlanSet.toArray(new Short[ceVlanSet.size()]);
}

From source file:com.thruzero.common.core.utils.StringUtilsExt.java

/**
 * Create a byte array from the given {@code tokenStream}, using the given token {@code separator}. Leading and trailing spaces of each token are trimmed.
 * <p>//from  ww  w. j av a  2 s . co m
 * Example input: "0xc3, 0x41, 0x55, 0xed, 0xf4, 0x41, 0x3e, 0x61".
 */
public static byte[] tokensToByteArray(final String tokenStream, final String separator) {
    byte[] result;
    String[] stringArray = StringUtils.split(tokenStream, separator);
    int i = 0;

    result = new byte[stringArray.length];
    for (String string : stringArray) {
        int num = Short.decode(string.trim());
        result[i++] = (byte) num;
    }

    return result;
}

From source file:org.jbpm.formModeler.core.processing.fieldHandlers.NumericFieldHandler.java

public Object getTheValue(Field field, String[] paramValue, String desiredClassName) throws Exception {
    if (paramValue == null || paramValue.length == 0)
        return null;

    if (desiredClassName.equals("byte")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Byte((byte) 0);
        else//from   w w  w  . ja v a 2 s .c  om
            return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals("short")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Short((short) 0);
        else
            return Short.decode(paramValue[0]);
    } else if (desiredClassName.equals("int")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Integer(0);
        else
            return Integer.decode(paramValue[0]);
    } else if (desiredClassName.equals("long")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Long(0L);
        else
            return Long.decode(paramValue[0]);
    } else if (desiredClassName.equals(Byte.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals(Short.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Short.decode(paramValue[0]);

    } else if (desiredClassName.equals(Integer.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Integer.decode(paramValue[0]);

    } else if (desiredClassName.equals(Long.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Long.decode(paramValue[0]);

    } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")
            || desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")
            || desiredClassName.equals(BigDecimal.class.getName())) {

        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();

        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale(LocaleManager.currentLang()));
        if (desiredClassName.equals(BigDecimal.class.getName()))
            df.setParseBigDecimal(true);
        String pattern = getFieldPattern(field);
        if (pattern != null && !"".equals(pattern)) {
            df.applyPattern(pattern);
        } else {
            df.applyPattern("###.##");
        }
        ParsePosition pp = new ParsePosition(0);
        Number num = df.parse(paramValue[0], pp);
        if (paramValue[0].length() != pp.getIndex() || num == null) {
            log.debug("Error on parsing value");
            throw new ParseException("Error parsing value", pp.getIndex());
        }

        if (desiredClassName.equals(BigDecimal.class.getName())) {
            return num;
        } else if (desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")) {
            return new Float(num.floatValue());
        } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")) {
            return new Double(num.doubleValue());
        }
    } else if (desiredClassName.equals(BigInteger.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return new BigInteger(paramValue[0]);

    }
    throw new IllegalArgumentException("Invalid class for NumericFieldHandler: " + desiredClassName);
}

From source file:de.questmaster.fatremote.RemoteActivity.java

public void onDebugButton(View v) {
    // short in1 = Short.decode(((TextView)findViewById(R.id.pos1)).getText().toString());
    // short in2 = Short.decode(((TextView)findViewById(R.id.pos2)).getText().toString());
    short keyCode = Short.decode(((TextView) this.findViewById(R.id.pos3)).getText().toString());
    short keyModifier = Short.decode(((TextView) this.findViewById(R.id.pos4)).getText().toString());

    // send keyCode
    NetworkProxy.getInstance(this).addRemoteEvent(new FATRemoteEvent(keyCode, keyModifier));
}

From source file:org.ossie.properties.AnyUtils.java

/**
 * Attempts to convert the string value to the appropriate Java type.
 * /*from   w ww. j a  va2 s . c  om*/
 * @param stringValue the string form of the value
 * @param type the string form of the TypeCode
 * @return A Java object of theString corresponding to the typecode
 */
public static Object convertString(final String stringValue, final String type) {
    if (stringValue == null) {
        return null;
    }
    if (type.equals("string")) {
        return stringValue;
    } else if (type.equals("wstring")) {
        return stringValue;
    } else if (type.equals("boolean")) {
        if ("true".equalsIgnoreCase(stringValue) || "false".equalsIgnoreCase(stringValue)) {
            return Boolean.parseBoolean(stringValue);
        }
        throw new IllegalArgumentException(stringValue + " is not a valid boolean value");
    } else if (type.equals("char")) {
        if (stringValue.length() == 1) {
            return stringValue.charAt(0);
        }
        throw new IllegalArgumentException(stringValue + " is not a valid char value");
    } else if (type.equals("wchar")) {
        return stringValue.charAt(0);
    } else if (type.equals("double")) {
        return Double.parseDouble(stringValue);
    } else if (type.equals("float")) {
        return Float.parseFloat(stringValue);
    } else if (type.equals("short")) {
        return Short.decode(stringValue);
    } else if (type.equals("long")) {
        return Integer.decode(stringValue);
    } else if (type.equals("longlong")) {
        return Long.decode(stringValue);
    } else if (type.equals("ulong")) {
        final long MAX_UINT = 2L * Integer.MAX_VALUE + 1L;
        final Long retVal = Long.decode(stringValue);
        if (retVal < 0 || retVal > MAX_UINT) {
            throw new IllegalArgumentException(
                    "ulong value must be greater than '0' and less than " + MAX_UINT);
        }
        return retVal;
    } else if (type.equals("ushort")) {
        final int MAX_USHORT = 2 * Short.MAX_VALUE + 1;
        final Integer retVal = Integer.decode(stringValue);
        if (retVal < 0 || retVal > MAX_USHORT) {
            throw new IllegalArgumentException(
                    "ushort value must be greater than '0' and less than " + MAX_USHORT);
        }
        return retVal;
    } else if (type.equals("ulonglong")) {
        final BigInteger MAX_ULONG_LONG = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2))
                .add(BigInteger.ONE);
        final BigInteger retVal = bigIntegerDecode(stringValue);
        if (retVal.compareTo(BigInteger.ZERO) < 0 || retVal.compareTo(MAX_ULONG_LONG) > 0) {
            throw new IllegalArgumentException(
                    "ulonglong value must be greater than '0' and less than " + MAX_ULONG_LONG.toString());
        }
        return retVal;
    } else if (type.equals("objref")) {
        List<String> objrefPrefix = Arrays.asList("IOR:", "corbaname:", "corbaloc:");
        for (String prefix : objrefPrefix) {
            if (stringValue.startsWith(prefix)) {
                return stringValue;
            }
        }
        throw new IllegalArgumentException(stringValue + " is not a valid objref value");
    } else if (type.equals("octet")) {
        final short MIN_OCTET = 0;
        final short MAX_OCTET = 0xFF;
        short val = Short.valueOf(stringValue);
        if (val <= MAX_OCTET && val >= MIN_OCTET) {
            return Short.valueOf(val).byteValue();
        }
        throw new IllegalArgumentException(stringValue + " is not a valid octet value");
    } else {
        throw new IllegalArgumentException("Unknown CORBA Type: " + type);
    }
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 *
 * @param text        the text to convert
 * @param targetClass the target class to parse into
 * @return the parsed number/*  w  ww  .j  a v a2s.co  m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {
    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 * @param text the text to convert/*from ww  w.j a va2 s. co m*/
 * @param targetClass the target class to parse into
 * @return the parsed number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {

    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:com.amazonaws.mobileconnectors.pinpoint.internal.core.configuration.AndroidPreferencesConfiguration.java

public Short getShort(final String propertyName) {
    Short value = null;//from w  w  w. ja  va2  s.c om
    String valueString = properties.get(propertyName);

    if (valueString != null) {
        try {
            if (properties.containsKey(propertyName)) {
                value = Short.decode(valueString);
            }
        } catch (Exception ex) {
            // Do not log property due to potential sensitive information.
            log.error("Could not get Short for property.");
        }
    }

    return value;
}