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:com.mahisoft.elasticsearchprediction.engine.ElasticsearchGenericIndexEngine.java

public void createIndex(File dataFile) throws IOException {
    Client client = null;/*w  ww. j av  a2 s.c  o  m*/

    try {
        String indexName = dataProperties.getValue("index.name");
        String clusterName = dataProperties.getValue("cluster.name");
        String nodeName = dataProperties.getValue("node.name");
        String host = dataProperties.getValue("host");
        String mappingFilename = dataProperties.getValue("mapping.filename");
        short port = Short.parseShort(dataProperties.getValue("port"));
        Settings settings = buildSettings(clusterName, nodeName);
        TransportClient transportClient = new TransportClient(settings);

        client = transportClient.addTransportAddress(new InetSocketTransportAddress(host, port));
        deleteIndex(client, indexName);
        loadData(dataFile, client, indexName, mappingFilename);
    } finally {
        if (client != null)
            client.close();
    }
}

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

/**
 *
 * @param data//from   w  w  w .java 2s  .  c  o m
 * @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:com.junly.common.util.ReflectUtils.java

/**
 * <p class="detail">//www  . jav  a  2  s.  c o  m
 * 
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj 
 * @param name    ??
 * @param value  (?)??false
 * @return
 */
public static boolean setProperty(Object obj, String name, Object value) {
    if (obj != null) {
        Class<?> clazz = obj.getClass();
        while (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
                continue;
            }
            try {
                Class<?> type = field.getType();
                if (type.isPrimitive() == true && value != null) {
                    if (value instanceof String) {
                        if (type.equals(int.class) == true) {
                            value = Integer.parseInt((String) value);
                        } else if (type.equals(double.class) == true) {
                            value = Double.parseDouble((String) value);
                        } else if (type.equals(boolean.class) == true) {
                            value = Boolean.parseBoolean((String) value);
                        } else if (type.equals(long.class) == true) {
                            value = Long.parseLong((String) value);
                        } else if (type.equals(byte.class) == true) {
                            value = Byte.parseByte((String) value);
                        } else if (type.equals(char.class) == true) {
                            value = Character.valueOf(((String) value).charAt(0));
                        } else if (type.equals(float.class) == true) {
                            value = Float.parseFloat((String) value);
                        } else if (type.equals(short.class) == true) {
                            value = Short.parseShort((String) value);
                        }
                    }
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                if (value == null || type.equals(value.getClass()) == true) {
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
    return false;
}

From source file:XmlValueEncoder.java

/**
 * {@inheritDoc}//from   w  w  w.  ja v  a  2 s  . co  m
 *
 * @see org.jboss.dna.common.text.TextDecoder#decode(java.lang.String)
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '&') {
            int index = iter.getIndex();

            do {
                c = iter.next();
            } while (c != CharacterIterator.DONE && c != ';');

            // We found a closing semicolon
            if (c == ';') {
                String s = encodedText.substring(index + 1, iter.getIndex());

                if (SPECIAL_ENTITIES.containsKey(s)) {
                    sb.append(SPECIAL_ENTITIES.get(s));
                    continue;

                }

                if (s.length() > 0 && s.charAt(0) == '#') {
                    try {
                        sb.append((char) Short.parseShort(s.substring(1, s.length())));
                        continue;
                    } catch (NumberFormatException nfe) {
                        // This is possible in malformed encodings, but let it fall through
                    }
                }
            }

            // Malformed encoding, restore state and pass poorly encoded data back
            c = '&';
            iter.setIndex(index);
        }

        sb.append(c);

    }
    return sb.toString();
}

From source file:com.netflix.explorers.PropertiesGlobalModelContext.java

public PropertiesGlobalModelContext(Properties props) {
    this.properties = props;

    environmentName = props.getProperty(PROPERTY_ENVIRONMENT_NAME);
    currentRegion = props.getProperty(PROPERTY_CURRENT_REGION);
    applicationVersion = props.getProperty(PROPERTY_APPLICATION_VERSION);
    applicationName = props.getProperty(PROPERTY_APPLICATION_NAME);
    isLocal = Boolean.parseBoolean(props.getProperty(PROPERTY_IS_LOCAL, "false"));
    homePageUrl = props.getProperty(PROPERTY_HOME_PAGE);
    defaultPort = Short.parseShort(props.getProperty(PROPERTY_DEFAULT_PORT, "8080"));
    dataCenter = props.getProperty(PROPERTY_DATA_CENTER);
    defaultExplorerName = props.getProperty(PROPERTY_DEFAULT_EXPLORER);

    try {/*from w  ww  .ja  v a  2s. c  om*/
        Map<Object, Object> dcs = ConfigurationConverter
                .getMap(ConfigurationConverter.getConfiguration(props).subset(PROPERTIES_PREFIX + ".dc"));
        for (Entry<Object, Object> dc : dcs.entrySet()) {
            String key = StringUtils.substringBefore(dc.getKey().toString(), ".");
            String attr = StringUtils.substringAfter(dc.getKey().toString(), ".");

            CrossLink link = links.get(key);
            if (link == null) {
                link = new CrossLink();
                links.put(key, link);
            }

            BeanUtils.setProperty(link, attr, dc.getValue());
        }
    } catch (Exception e) {
        LOG.error("Exception ", e);
        throw new RuntimeException(e);
    }
}

From source file:com.sqewd.open.dal.core.persistence.handlers.StringArrayConvertor.java

private Object getArrayData(String[] data, Field field) throws Exception {
    Class<?> type = field.getType().getComponentType();
    if (type.equals(String.class)) {
        return data;
    } else if (EnumPrimitives.isPrimitiveType(type)) {
        EnumPrimitives et = EnumPrimitives.type(type);
        if (type.isPrimitive()) {
            switch (et) {
            case ECharacter:
                char[] carr = new char[data.length];
                for (int ii = 0; ii < carr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        carr[ii] = data[ii].charAt(0);
                }//www.  ja  v  a2s .  c o  m
                return carr;
            case EShort:
                short[] sarr = new short[data.length];
                for (int ii = 0; ii < sarr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        sarr[ii] = Short.parseShort(data[ii]);
                }
                return sarr;
            case EInteger:
                int[] iarr = new int[data.length];
                for (int ii = 0; ii < iarr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        iarr[ii] = Integer.parseInt(data[ii]);
                }
                return iarr;
            case ELong:
                long[] larr = new long[data.length];
                for (int ii = 0; ii < larr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        larr[ii] = Long.parseLong(data[ii]);
                }
                return larr;
            case EFloat:
                float[] farr = new float[data.length];
                for (int ii = 0; ii < farr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        farr[ii] = Float.parseFloat(data[ii]);
                }
                return farr;
            case EDouble:
                double[] darr = new double[data.length];
                for (int ii = 0; ii < darr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        darr[ii] = Double.parseDouble(data[ii]);
                }
                return darr;
            }
        } else {
            switch (et) {
            case ECharacter:
                Character[] carr = new Character[data.length];
                for (int ii = 0; ii < carr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        carr[ii] = data[ii].charAt(0);
                }
                return carr;
            case EShort:
                Short[] sarr = new Short[data.length];
                for (int ii = 0; ii < sarr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        sarr[ii] = Short.parseShort(data[ii]);
                }
                return sarr;
            case EInteger:
                Integer[] iarr = new Integer[data.length];
                for (int ii = 0; ii < iarr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        iarr[ii] = Integer.parseInt(data[ii]);
                }
                return iarr;
            case ELong:
                Long[] larr = new Long[data.length];
                for (int ii = 0; ii < larr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        larr[ii] = Long.parseLong(data[ii]);
                }
                return larr;
            case EFloat:
                Float[] farr = new Float[data.length];
                for (int ii = 0; ii < farr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        farr[ii] = Float.parseFloat(data[ii]);
                }
                return farr;
            case EDouble:
                Double[] darr = new Double[data.length];
                for (int ii = 0; ii < darr.length; ii++) {
                    if (data[ii] != null && !data[ii].isEmpty())
                        darr[ii] = Double.parseDouble(data[ii]);
                }
                return darr;
            }
        }
    } else if (type.equals(Date.class)) {
        Date[] dtarr = new Date[data.length];
        for (int ii = 0; ii < dtarr.length; ii++) {
            if (data[ii] != null && !data[ii].isEmpty()) {
                dtarr[ii] = DateUtils.fromString(data[ii]);
            }
        }
    }
    throw new Exception("Unsupported Array element type [" + type.getCanonicalName() + "]");
}

From source file:javadz.beanutils.converters.ShortArrayConverter.java

/**
 * Convert the specified input object into an output object of the
 * specified type./*from   ww w.j a  va2s.  co  m*/
 *
 * @param type Data type to which this value should be converted
 * @param value The input value to be converted
 * @return the converted value
 *
 * @exception ConversionException if conversion cannot be performed
 *  successfully
 */
public Object convert(Class type, Object value) {

    // Deal with a null value
    if (value == null) {
        if (useDefault) {
            return (defaultValue);
        } else {
            throw new ConversionException("No value specified");
        }
    }

    // Deal with the no-conversion-needed case
    if (MODEL.getClass() == value.getClass()) {
        return (value);
    }

    // Deal with input value as a String array
    if (strings.getClass() == value.getClass()) {
        try {
            String[] values = (String[]) value;
            short[] results = new short[values.length];
            for (int i = 0; i < values.length; i++) {
                results[i] = Short.parseShort(values[i]);
            }
            return (results);
        } catch (Exception e) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException(value.toString(), e);
            }
        }
    }

    // Parse the input value as a String into elements
    // and convert to the appropriate type
    try {
        List list = parseElements(value.toString());
        short[] results = new short[list.size()];
        for (int i = 0; i < results.length; i++) {
            results[i] = Short.parseShort((String) list.get(i));
        }
        return (results);
    } catch (Exception e) {
        if (useDefault) {
            return (defaultValue);
        } else {
            throw new ConversionException(value.toString(), e);
        }
    }

}

From source file:org.acoustid.server.util.ParameterMap.java

public int getShort(String name, int minValue) throws ParameterFormatException, MissingParameterException {
    String stringValue = getString(name);
    try {/* w w  w  .  j a  v a2 s .co m*/
        short value = Short.parseShort(stringValue);
        if (value < minValue) {
            throw new ParameterFormatException(
                    "Parameter '" + name + "' must be greater than or equal to " + minValue);
        }
        return value;
    } catch (NumberFormatException ex) {
        throw new ParameterFormatException("Parameter '" + name + "' is not a valid number", ex);
    }
}

From source file:org.jcodec.player.filters.http.Downloader.java

private static TapeTimecode parseTimecode(String timecodeRaw) {
    if (StringUtils.isEmpty(timecodeRaw))
        return null;
    String[] split = StringUtils.split(timecodeRaw, ":");
    if (split.length == 4) {
        return new TapeTimecode(Short.parseShort(split[0]), Byte.parseByte(split[1]), Byte.parseByte(split[2]),
                Byte.parseByte(split[3]), false);
    } else if (split.length == 3) {
        String[] split1 = StringUtils.split(split[2], ";");
        if (split1.length == 2)
            return new TapeTimecode(Short.parseShort(split[0]), Byte.parseByte(split[1]),
                    Byte.parseByte(split1[0]), Byte.parseByte(split1[1]), true);
    }//from  ww w.jav a2s.  c  o m
    return null;
}

From source file:io.github.xxyy.simplegiveall.SimpleGiveallMain.java

@SuppressWarnings("deprecation") //ItemStack IDs
private static ItemStack handleGiveallSpecific(final CommandSender sender, final String[] args) { //giveall item[:damage] amount
    ItemStack finalStack;/*  ww  w.  j  a  v  a  2s  . c om*/
    int amount = 1;
    short damage = 0;

    if (args.length >= 2) {
        if (!StringUtils.isNumeric(args[1])) {
            sender.sendMessage(MessageFormat.format(bundle.getString("AMOUNT_NAN"), args[1]));
            return null;
        }

        amount = Integer.parseInt(args[1]);
    }

    final String[] itemInfo = args[0].split(bundle.getString("MATERIAL_DAMAGE_SEPERATOR")); //default is ':'

    if (itemInfo.length > 1) { //If we have a damage given, use that
        if (StringUtils.isNumeric(itemInfo[1])) { //Check that the damage is actually numeric
            damage = Short.parseShort(itemInfo[1]);
        } else {
            sender.sendMessage(MessageFormat.format(bundle.getString("INVALID_DAMAGE"), itemInfo[1]));
            return null;
        }
    }

    if (StringUtils.isNumeric(itemInfo[0])) { //Check if we have been passed an item ID
        finalStack = new ItemStack(Integer.parseInt(itemInfo[0]), amount, damage);
        sender.sendMessage(MessageFormat.format(bundle.getString("ITEMIDS_DEPRECATED"), finalStack.getType())); //Send the user a message noting that item IDs will be removed in a future update
    } else { //Else, we probably have a material name
        final Material material = Material.matchMaterial(itemInfo[0].replace("-", "_")); //replace dashes with underscores because that's a common mistake
        if (material == null) { //If the user passed an invalid material name
            sender.sendMessage(
                    MessageFormat.format(bundle.getString("UNKNOWN_MATERIAL"), itemInfo[0].toUpperCase()));
            printHelpTo(sender);
            return null;
        }
        finalStack = new ItemStack(material, amount, damage);
    }

    return finalStack;
}