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:mil.jpeojtrs.sca.util.AnyUtils.java

/**
 * Attempts to convert the string value to the appropriate Java type.
 * /*from w w  w  .j  a  v  a 2s.  co m*/
 * @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
 */
private static Object primitiveConvertString(final String stringValue, final String type) {
    if (stringValue == null) {
        return null;
    }
    if ("string".equals(type)) {
        return stringValue;
    } else if ("wstring".equals(type)) {
        return stringValue;
    } else if ("boolean".equals(type)) {
        if ("true".equalsIgnoreCase(stringValue) || "false".equalsIgnoreCase(stringValue)) {
            return Boolean.parseBoolean(stringValue);
        }
        throw new IllegalArgumentException(stringValue + " is not a valid boolean value");
    } else if ("char".equals(type)) {
        switch (stringValue.length()) {
        case 1:
            return stringValue.charAt(0);
        case 0:
            return null;
        default:
            throw new IllegalArgumentException(stringValue + " is not a valid char value");
        }
    } else if ("wchar".equals(type)) {
        return stringValue.charAt(0);
    } else if ("double".equals(type)) {
        return Double.parseDouble(stringValue);
    } else if ("float".equals(type)) {
        return Float.parseFloat(stringValue);
    } else if ("short".equals(type)) {
        return Short.decode(stringValue);
    } else if ("long".equals(type)) {
        return Integer.decode(stringValue);
    } else if ("longlong".equals(type)) {
        return Long.decode(stringValue);
    } else if ("ulong".equals(type)) {
        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 ("ushort".equals(type)) {
        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 ("ulonglong".equals(type)) {
        final BigInteger MAX_ULONG_LONG = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2))
                .add(BigInteger.ONE);
        final BigInteger retVal = AnyUtils.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 ("objref".equals(type)) {
        if ("".equals(stringValue)) {
            return null;
        }
        final List<String> objrefPrefix = Arrays.asList("IOR:", "corbaname:", "corbaloc:");
        for (final String prefix : objrefPrefix) {
            if (stringValue.startsWith(prefix)) {
                return stringValue;
            }
        }
        throw new IllegalArgumentException(stringValue + " is not a valid objref value");
    } else if ("octet".equals(type)) {
        final short MIN_OCTET = 0;
        final short MAX_OCTET = 0xFF;
        final short val = Short.decode(stringValue);
        if (val <= MAX_OCTET && val >= MIN_OCTET) {
            return Short.valueOf(val);
        }
        throw new IllegalArgumentException(stringValue + " is not a valid octet value");
    } else {
        throw new IllegalArgumentException("Unknown CORBA Type: " + type);
    }
}

From source file:com.l2jfree.gameserver.templates.StatsSet.java

/**
 * Returns the short associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
 * //from   ww w.  j av a2s .  co m
 * @param name : String designating the key in the set
 * @param deflt : short designating the default value if value associated with the key is null
 * @return short : value associated to the key
 */
public final short getShort(String name, short deflt) {
    Object val = get(name);
    if (val == null)
        return deflt;
    if (val instanceof Number)
        return ((Number) val).shortValue();
    try {
        return Short.decode((String) val);
    } catch (Exception e) {
        throw new IllegalArgumentException("Short value required, but found: " + val);
    }
}

From source file:org.thialfihar.android.apg.keyimport.HkpKeyserver.java

/**
 * @param hostAndPort may be just/*from  w  w  w. j ava 2s .c o m*/
 *                    "<code>hostname</code>" (eg. "<code>pool.sks-keyservers.net</code>"), then it will
 *                    connect using {@link #PORT_DEFAULT}. However, port may be specified after colon
 *                    ("<code>hostname:port</code>", eg. "<code>p80.pool.sks-keyservers.net:80</code>").
 */
public HkpKeyserver(String hostAndPort) {
    String host = hostAndPort;
    short port = PORT_DEFAULT;
    final int colonPosition = hostAndPort.lastIndexOf(':');
    if (colonPosition > 0) {
        host = hostAndPort.substring(0, colonPosition);
        final String portStr = hostAndPort.substring(colonPosition + 1);
        port = Short.decode(portStr);
    }
    mHost = host;
    mPort = port;
}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyServer.java

/**
 * @param hostAndPort may be just//from   w ww  .jav a 2s.  c o  m
 *                    "<code>hostname</code>" (eg. "<code>pool.sks-keyservers.net</code>"), then it will
 *                    connect using {@link #PORT_DEFAULT}. However, port may be specified after colon
 *                    ("<code>hostname:port</code>", eg. "<code>p80.pool.sks-keyservers.net:80</code>").
 */
public HkpKeyServer(String hostAndPort) {
    String host = hostAndPort;
    short port = PORT_DEFAULT;
    final int colonPosition = hostAndPort.lastIndexOf(':');
    if (colonPosition > 0) {
        host = hostAndPort.substring(0, colonPosition);
        final String portStr = hostAndPort.substring(colonPosition + 1);
        port = Short.decode(portStr);
    }
    mHost = host;
    mPort = port;
}

From source file:org.crazydog.util.spring.NumberUtils.java

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

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

From source file:com.l2jfree.gameserver.templates.StatsSet.java

/**
 * Returns the short associated to the key put in parameter ("name").
 * /*from   ww  w  .j  a  v a2 s. co  m*/
 * @param name : String designating the key in the set
 * @return short : value associated to the key
 */
public final short getShort(String name) {
    Object val = get(name);
    if (val == null)
        throw new IllegalArgumentException("Short value required, but not specified");
    if (val instanceof Number)
        return ((Number) val).shortValue();
    try {
        return Short.decode((String) val);
    } catch (Exception e) {
        throw new IllegalArgumentException("Short value required, but found: " + val);
    }
}

From source file:com.l2jfree.config.L2Properties.java

public short getShort(String name, String deflt) {
    String val = getProperty(name, deflt);
    if (val == null)
        throw new IllegalArgumentException("Short value required, but not specified");

    try {//from  w  ww. j  av  a  2 s.  c  o m
        return Short.decode(val);
    } catch (Exception e) {
        throw new IllegalArgumentException("Short value required, but found: " + val);
    }
}

From source file:com.l2jfree.config.L2Properties.java

public short getShort(String name, short deflt) {
    String val = getProperty(name);
    if (val == null)
        return deflt;

    try {// ww w. j av a 2s.  com
        return Short.decode(val);
    } catch (Exception e) {
        throw new IllegalArgumentException("Short value required, but found: " + val);
    }
}

From source file:org.apache.hcatalog.cli.HCatCli.java

private static void validatePermissions(CliSessionState ss, HiveConf conf, String perms) {
    perms = perms.trim();/* w  ww .  j a va2  s.c  om*/
    FsPermission fp = null;

    if (perms.matches("^\\s*([r,w,x,-]{9})\\s*$")) {
        fp = FsPermission.valueOf("d" + perms);
    } else if (perms.matches("^\\s*([0-7]{3})\\s*$")) {
        fp = new FsPermission(Short.decode("0" + perms));
    } else {
        ss.err.println("Invalid permission specification: " + perms);
        System.exit(1);
    }

    if (!HCatUtil.validateMorePermissive(fp.getUserAction(), fp.getGroupAction())) {
        ss.err.println("Invalid permission specification: " + perms
                + " : user permissions must be more permissive than group permission ");
        System.exit(1);
    }
    if (!HCatUtil.validateMorePermissive(fp.getGroupAction(), fp.getOtherAction())) {
        ss.err.println("Invalid permission specification: " + perms
                + " : group permissions must be more permissive than other permission ");
        System.exit(1);
    }
    if ((!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getUserAction()))
            || (!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getGroupAction()))
            || (!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getOtherAction()))) {
        ss.err.println("Invalid permission specification: " + perms
                + " : permissions must have execute permissions if read or write permissions are specified ");
        System.exit(1);
    }

    conf.set(HCatConstants.HCAT_PERMS, "d" + fp.toString());

}

From source file:org.apache.hive.hcatalog.cli.HCatCli.java

private static void validatePermissions(CliSessionState ss, HiveConf conf, String perms) {
    perms = perms.trim();/*from  w ww.j a  v  a 2 s  .  c om*/
    FsPermission fp = null;

    if (perms.matches("^\\s*([r,w,x,-]{9})\\s*$")) {
        fp = FsPermission.valueOf("d" + perms);
    } else if (perms.matches("^\\s*([0-7]{3})\\s*$")) {
        fp = new FsPermission(Short.decode("0" + perms));
    } else {
        ss.err.println("Invalid permission specification: " + perms);
        sysExit(ss, 1);
    }

    if (!HCatUtil.validateMorePermissive(fp.getUserAction(), fp.getGroupAction())) {
        ss.err.println("Invalid permission specification: " + perms
                + " : user permissions must be more permissive than group permission ");
        sysExit(ss, 1);
    }
    if (!HCatUtil.validateMorePermissive(fp.getGroupAction(), fp.getOtherAction())) {
        ss.err.println("Invalid permission specification: " + perms
                + " : group permissions must be more permissive than other permission ");
        sysExit(ss, 1);
    }
    if ((!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getUserAction()))
            || (!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getGroupAction()))
            || (!HCatUtil.validateExecuteBitPresentIfReadOrWrite(fp.getOtherAction()))) {
        ss.err.println("Invalid permission specification: " + perms
                + " : permissions must have execute permissions if read or write permissions are specified ");
        sysExit(ss, 1);
    }

    conf.set(HCatConstants.HCAT_PERMS, "d" + fp.toString());

}