Example usage for java.lang Short parseShort

List of usage examples for java.lang Short parseShort

Introduction

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

Prototype

public static short parseShort(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed decimal short .

Usage

From source file:Main.java

public static final int parseIPAddress(String address) {
    if (TextUtils.isEmpty(address))
        throw new IllegalArgumentException("address cannot be null or empty");
    String[] tokens = address.split("\\.");
    boolean isOk = true;
    int ip = 0;/*from   ww w  .j  ava  2 s .  c om*/
    if (tokens.length == 4) {
        for (int i = 0; i < 4; i++) {
            try {
                short num = Short.parseShort(tokens[i]);
                if (num < 0 || num > 255) {
                    isOk = false;
                    break;
                }
                ip |= (num << (8 * i));
            } catch (NumberFormatException ex) {
                isOk = false;
                break;
            }
        }
    } else
        isOk = false;

    if (!isOk)
        throw new IllegalArgumentException("address is not in the form X.X.X.X, with X in the range 0-255");

    return ip;
}

From source file:Main.java

public static short parse(String str, short fallback) {
    try {/* w ww .j  a va  2  s .  c  o m*/
        return Short.parseShort(str);
    } catch (NumberFormatException e) {
        return fallback;
    }
}

From source file:Main.java

/**
 * This method parses the given value into the specified primitive or wrapper class.
 * @param clazz - primitive or wrapper class used to parse
 * @param value - string to be parsed/*from   w  ww.jav  a2s  .c  o  m*/
 * @return object of type clazz parsed from the string
 * @author Trisan Bepler
 */
public static Object toObject(Class<?> clazz, String value) {
    if (Boolean.TYPE == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.TYPE == clazz)
        return Byte.parseByte(value);
    if (Short.TYPE == clazz)
        return Short.parseShort(value);
    if (Integer.TYPE == clazz)
        return Integer.parseInt(value);
    if (Long.TYPE == clazz)
        return Long.parseLong(value);
    if (Float.TYPE == clazz)
        return Float.parseFloat(value);
    if (Double.TYPE == clazz)
        return Double.parseDouble(value);
    if (Boolean.class == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.class == clazz)
        return Byte.parseByte(value);
    if (Short.class == clazz)
        return Short.parseShort(value);
    if (Integer.class == clazz)
        return Integer.parseInt(value);
    if (Long.class == clazz)
        return Long.parseLong(value);
    if (Float.class == clazz)
        return Float.parseFloat(value);
    if (Double.class == clazz)
        return Double.parseDouble(value);
    if (Character.class == clazz)
        return value.charAt(0);
    if (Character.TYPE == clazz)
        return value.charAt(0);
    return value;
}

From source file:Main.java

public static void setField(Object object, String fieldName, Object fieldValue) {
    Class<?> objectClass = object.getClass();
    if (objectClass != null) {
        try {/*from  ww w.  j a  va2  s. c om*/
            Field field = objectClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Type type = field.getGenericType();

            //This is bad, but dear God I can't figure out how to convert a runtime Type to its actual type through reflection. I know how in C#....
            if (type.toString().toLowerCase().contains("short")) {
                fieldValue = Short.parseShort((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("integer")) {
                fieldValue = Integer.parseInt((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("long")) {
                fieldValue = Long.parseLong((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("boolean")) {
                fieldValue = "1".equals(fieldValue); //Java, you're the worst language. And SQLite isn't helping.
            }

            field.set(object, fieldValue);
        }

        catch (NoSuchFieldException e) {
            return;
        }

        catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

From source file:Main.java

public static byte[] getBodyBytes(String ip, String port, String key)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);//from w  w  w . j a  v a  2s .co  m
    dos.writeShort(Short.parseShort(port));
    dos.writeByte(key.getBytes().length);
    dos.write(key.getBytes());
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}

From source file:Main.java

/**
 * Parses the supplied xsd:short string and returns its value.
 * /*  w  ww  .j a  v  a 2 s.  c  o m*/
 * @param s
 *        A string representation of an xsd:short value.
 * @return The <tt>short</tt> value represented by the supplied string argument.
 * @throws NumberFormatException
 *         If the supplied string is not a valid xsd:short value.
 */
public static short parseShort(String s) {
    s = trimPlusSign(s);
    return Short.parseShort(s);
}

From source file:gov.nih.nci.caarray.application.util.Utils.java

/**
 * @param value the value to test.//w  w  w  . ja  v a 2  s  .c o  m
 * @return true is the value can be parsed as an Short.
 */
public static boolean isShort(String value) {
    try {
        Short.parseShort(value);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

From source file:org.openwms.common.comm.CommonMessageFactory.java

/**
 * Create a {@link CommHeader} from a passed telegram structure.
 *
 * @param telegram The telegram//from w ww.  ja va  2s.  c o  m
 * @return A {@link CommHeader} instance
 */
public static CommHeader createHeader(String telegram) {
    String sync = telegram.substring(0, CommHeader.LENGTH_SYNC_FIELD);

    int start = sync.length();
    int end = start + CommHeader.LENGTH_MESSAGE_LENGTH_FIELD;
    short messageLength = Short.parseShort(telegram.substring(start, end));

    start = end;
    end += CommHeader.LENGTH_SENDER_FIELD;
    String sender = telegram.substring(start, end);

    start = end;
    end += CommHeader.LENGTH_RECEIVER_FIELD;
    String receiver = telegram.substring(start, end);

    start = end;
    end += CommHeader.LENGTH_SEQUENCE_NO_FIELD;
    short sequenceNo = Short.parseShort(telegram.substring(start, end));
    return new CommHeader(sync, messageLength, sender, receiver, sequenceNo);
}

From source file:uk.ac.ebi.intact.editor.config.property.ShortPropertyConverter.java

@Override
public Short convertFromString(String str) {
    if (str == null)
        return null;

    return Short.parseShort(str);
}

From source file:org.ext4spring.parameter.converter.simple.ShortConverter.java

@Override
public <T> T toTypedValue(String stringValue, Class<T> type) {
    return (stringValue != null) ? type.cast(Short.parseShort(stringValue)) : null;
}