Example usage for java.lang Byte parseByte

List of usage examples for java.lang Byte parseByte

Introduction

In this page you can find the example usage for java.lang Byte parseByte.

Prototype

public static byte parseByte(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed decimal byte .

Usage

From source file:org.esa.beam.pixex.output.ScatterPlotDecoratingStrategy.java

private Number getOriginalMeasurementAsNumber(String value) {
    if (StringUtils.isNumeric(value, Integer.class)) {
        return Integer.parseInt(value);
    } else if (StringUtils.isNumeric(value, Double.class)) {
        return Double.parseDouble(value);
    } else if (StringUtils.isNumeric(value, Byte.class)) {
        return Byte.parseByte(value);
    } else if (StringUtils.isNumeric(value, Float.class)) {
        return Float.parseFloat(value);
    } else if (StringUtils.isNumeric(value, Long.class)) {
        return Long.parseLong(value);
    } else if (StringUtils.isNumeric(value, Short.class)) {
        return Short.parseShort(value);
    } else {/*w  ww .  j  av a 2  s .com*/
        return Double.NaN;
    }
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts the string value to instance of primitive wrapper.
 *
 * @param fieldClass The field class//  www  . java2  s.  c  o m
 * @param stringValue The string value
 * @return The instance of the field class
 * @throws ParseException Throws when the string isn't parseable
 */
private static Object convertToPrimitive(final Class fieldClass, final String stringValue)
        throws ParseException {
    Object parameter = null;

    if (fieldClass.equals(boolean.class)) {
        parameter = Boolean.parseBoolean(stringValue);
    } else if (fieldClass.equals(byte.class)) {
        parameter = Byte.parseByte(stringValue);
    } else if (fieldClass.equals(double.class)) {
        parameter = Double.parseDouble(stringValue);
    } else if (fieldClass.equals(float.class)) {
        parameter = Float.parseFloat(stringValue);
    } else if (fieldClass.equals(int.class)) {
        parameter = Integer.parseInt(stringValue);
    } else if (fieldClass.equals(long.class)) {
        parameter = Long.parseLong(stringValue);
    } else if (fieldClass.equals(short.class)) {
        parameter = Short.parseShort(stringValue);
    }

    return parameter;
}

From source file:edu.ku.brc.helpers.XMLHelper.java

/**
 * Get a byte attribute value from an element value
 * @param element the element to get the attribute from
 * @param attrName the name of the attribute to get
 * @param defValue the default value if the attribute isn't there
 * @return the attr value or the default value
 */// w  ww . j a v a2  s . c o m
public static byte getAttr(final Element element, final String attrName, final byte defValue) {
    String str = element.attributeValue(attrName);
    return isNotEmpty(str) ? Byte.parseByte(str) : defValue;
}

From source file:org.apereo.portal.properties.PropertiesManager.java

/**
 * Returns the value of a property for a given name as a <code>byte</code>
 * @param name the name of the requested property
 * @return value the property's value as a <code>byte</code>
 * @throws MissingPropertyException - if the property is not set
 * @throws BadPropertyException - if the property cannot be parsed as a byte
 *//*w  ww .j a  v a2 s.c o m*/
public static byte getPropertyAsByte(String name) throws MissingPropertyException, BadPropertyException {
    if (PropertiesManager.props == null)
        loadProps();
    try {
        return Byte.parseByte(getProperty(name));
    } catch (NumberFormatException nfe) {
        throw new BadPropertyException(name, getProperty(name), "byte");
    }

}

From source file:de.Keyle.MyPet.skill.skilltreeloader.SkillTreeLoaderJSON.java

protected void loadSkillTree(ConfigurationJSON jsonConfiguration, SkillTreeMobType skillTreeMobType) {
    JSONArray skilltreeList = (JSONArray) jsonConfiguration.getJSONObject().get("Skilltrees");
    for (Object st_object : skilltreeList) {
        SkillTree skillTree;//from  w  w  w.  j av a  2s.  c  om
        int place;
        try {
            JSONObject skilltreeObject = (JSONObject) st_object;
            skillTree = new SkillTree((String) skilltreeObject.get("Name"));
            place = Integer.parseInt(String.valueOf(skilltreeObject.get("Place")));

            if (skilltreeObject.containsKey("Inherits")) {
                skillTree.setInheritance((String) skilltreeObject.get("Inherits"));
            }
            if (skilltreeObject.containsKey("Permission")) {
                skillTree.setPermission((String) skilltreeObject.get("Permission"));
            }
            if (skilltreeObject.containsKey("Display")) {
                skillTree.setDisplayName((String) skilltreeObject.get("Display"));
            }
            if (skilltreeObject.containsKey("Description")) {
                JSONArray descriptionArray = (JSONArray) skilltreeObject.get("Description");
                for (Object lvl_object : descriptionArray) {
                    skillTree.addDescriptionLine(String.valueOf(lvl_object));
                }
            }

            JSONArray levelList = (JSONArray) skilltreeObject.get("Level");
            for (Object lvl_object : levelList) {
                JSONObject levelObject = (JSONObject) lvl_object;
                int thisLevel = Integer.parseInt(String.valueOf(levelObject.get("Level")));

                SkillTreeLevel newLevel = skillTree.addLevel(thisLevel);
                if (levelObject.containsKey("Message")) {
                    String message = (String) levelObject.get("Message");
                    newLevel.setLevelupMessage(message);
                }

                JSONArray skillList = (JSONArray) levelObject.get("Skills");
                for (Object skill_object : skillList) {
                    JSONObject skillObject = (JSONObject) skill_object;
                    String skillName = (String) skillObject.get("Name");
                    JSONObject skillPropertyObject = (JSONObject) skillObject.get("Properties");

                    if (SkillsInfo.getSkillInfoClass(skillName) != null) {
                        ISkillInfo skill = SkillsInfo.getNewSkillInfoInstance(skillName);

                        if (skill != null) {
                            SkillProperties sp = skill.getClass().getAnnotation(SkillProperties.class);
                            if (sp != null) {
                                TagCompound propertiesCompound = skill.getProperties();
                                for (int i = 0; i < sp.parameterNames().length; i++) {
                                    String propertyName = sp.parameterNames()[i];
                                    NBTdatatypes propertyType = sp.parameterTypes()[i];
                                    if (!propertiesCompound.getCompoundData().containsKey(propertyName)
                                            && skillPropertyObject.containsKey(propertyName)) {
                                        String value = String.valueOf(skillPropertyObject.get(propertyName));
                                        switch (propertyType) {
                                        case Short:
                                            if (Util.isShort(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagShort(Short.parseShort(value)));
                                            }
                                            break;
                                        case Int:
                                            if (Util.isInt(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagInt(Integer.parseInt(value)));
                                            }
                                            break;
                                        case Long:
                                            if (Util.isLong(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagLong(Long.parseLong(value)));
                                            }
                                            break;
                                        case Float:
                                            if (Util.isFloat(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagFloat(Float.parseFloat(value)));
                                            }
                                            break;
                                        case Double:
                                            if (Util.isDouble(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagDouble(Double.parseDouble(value)));
                                            }
                                            break;
                                        case Byte:
                                            if (Util.isByte(value)) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagByte(Byte.parseByte(value)));
                                            }
                                            break;
                                        case Boolean:
                                            if (value == null || value.equalsIgnoreCase("")
                                                    || value.equalsIgnoreCase("off")
                                                    || value.equalsIgnoreCase("false")) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagByte(false));
                                            } else if (value.equalsIgnoreCase("on")
                                                    || value.equalsIgnoreCase("true")) {
                                                propertiesCompound.getCompoundData().put(propertyName,
                                                        new TagByte(true));
                                            }
                                            break;
                                        case String:
                                            propertiesCompound.getCompoundData().put(propertyName,
                                                    new TagString(value));
                                            break;
                                        }
                                    }
                                }

                                skill.setProperties(propertiesCompound);
                                skill.setDefaultProperties();
                                skillTree.addSkillToLevel(thisLevel, skill);
                            }
                        }
                    }
                }
            }
            skillTreeMobType.addSkillTree(skillTree, place);
        } catch (Exception e) {
            DebugLogger.info("Problem in" + skillTreeMobType.getMobTypeName());
            DebugLogger.info(Arrays.toString(e.getStackTrace()));
            e.printStackTrace();
            DebugLogger.printThrowable(e);
            MyPetLogger.write(ChatColor.RED + "Error in " + skillTreeMobType.getMobTypeName().toLowerCase()
                    + ".json -> Skilltree not loaded.");
        }
    }
}

From source file:org.occiware.mart.server.utils.Utils.java

/**
 * Parse a string to a number without knowning its type output.
 *
 * @param str           value to convert.
 * @param instanceClass can be null, represent the class type of the value to convert (like Integer etc.)
 * @return a non null number object.//from w  w w  .  j ava2s .c  om
 * @throws NumberFormatException if the value cannot be converted.
 */
public static Number parseNumber(String str, Class<?> instanceClass) throws NumberFormatException {
    Number number;
    if (instanceClass == null) {

        try {
            number = Float.parseFloat(str);

        } catch (NumberFormatException e) {
            try {
                number = Double.parseDouble(str);
            } catch (NumberFormatException e1) {
                try {
                    number = Integer.parseInt(str);
                } catch (NumberFormatException e2) {
                    try {
                        number = Long.parseLong(str);
                    } catch (NumberFormatException e3) {
                        number = new BigDecimal(str);
                    }
                }
            }
        }
    } else {
        if (instanceClass == Integer.class) {
            number = Integer.parseInt(str);
        } else if (instanceClass == Long.class) {
            number = Long.parseLong(str);
        } else if (instanceClass == Float.class) {
            number = Float.parseFloat(str);
        } else if (instanceClass == Double.class) {
            number = Double.parseDouble(str);
        } else if (instanceClass == Short.class) {
            number = Short.parseShort(str);
        } else if (instanceClass == Byte.class) {
            number = Byte.parseByte(str);
        } else if (instanceClass == BigDecimal.class) {
            number = new BigDecimal(str);
        } else {
            throw new NumberFormatException("Unknown format.");
        }

    }

    return number;
}

From source file:com.krawler.br.spring.RConverterImpl.java

private Object getSimpleValue(String cls, String valStr) {
    Object val = null;
    if (cls.equals(ModuleBag.PRIMITIVE.BOOLEAN.className())) {
        val = Boolean.parseBoolean(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.BYTE.className())) {
        val = Byte.parseByte(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.CHAR.className())) {
        val = valStr.charAt(0);

    } else if (cls.equals(ModuleBag.PRIMITIVE.SHORT.className())) {
        val = Short.parseShort(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.INT.className())) {
        val = Integer.parseInt(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.LONG.className())) {
        val = Long.parseLong(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.FLOAT.className())) {
        val = Float.parseFloat(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.DOUBLE.className())) {
        val = Double.parseDouble(valStr);

    } else if (cls.equals(ModuleBag.PRIMITIVE.STRING.className())) {
        val = valStr;

    } else if (cls.equals(ModuleBag.PRIMITIVE.DATE.className())) {
        try {/*from  w w  w .ja  va 2 s.co  m*/
            val = df.parse(valStr);
        } catch (Exception ex) {
            val = null;
        }
    }
    return val;
}

From source file:de.julielab.jcore.reader.xmlmapper.typeBuilder.StandardTypeBuilder.java

private Object parseValueStringToValueType(String value, String type) {
    if (type.equals("boolean")) {
        return Boolean.parseBoolean(value);
    } else if (type.equals("int")) {
        return Integer.parseInt(value);
    } else if (type.equals("long")) {
        return Long.parseLong(value);
    } else if (type.equals("float")) {
        return Float.parseFloat(value);
    } else if (type.equals("double")) {
        return Double.parseDouble(value);
    } else if (type.equals("byte")) {
        return Byte.parseByte(value);
    } else if (type.equals("char")) {
        return value.charAt(0);
    }/*from w w  w .  ja  v a  2  s .  c  o m*/
    return null;
}

From source file:org.openhab.binding.noolite.internal.NooliteBinding.java

/**
 * @{inheritDoc}/*from  ww  w.ja  v  a2 s .c  om*/
 */
@Override
protected void internalReceiveCommand(String itemName, Command command) {

    logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);

    for (NooliteBindingProvider provider : providers) {
        for (String itemname : provider.getItemNames()) {
            if ((itemname.equals(itemName)) && (provider.getChannel(itemName).equals("bind"))) {

                if (provider.getType(itemName).equals("Receive")) {
                    rxw.bindChannel(Byte.parseByte(command.toString()));
                } else {
                    pc.bindChannel(Byte.parseByte(command.toString()));
                }
                logger.debug("binding " + command.toString() + " channel");

            } else if ((itemname.equals(itemName)) && (provider.getChannel(itemName).equals("unbind"))) {

                if (provider.getType(itemName).equals("Receive")) {
                    rxw.unbindChannel(Byte.parseByte(command.toString()));
                    logger.debug("unbinding Receive " + command.toString() + " channel");
                } else {
                    pc.unbindChannel(Byte.parseByte(command.toString()));
                    logger.debug("unbinding Send " + command.toString() + " channel");
                }
                logger.debug("unbinding " + command.toString() + " channel");

            } else if ((itemname.equals(itemName)) && (provider.getChannel(itemName).equals("unbindAll"))) {

                if (provider.getType(itemName).equals("Receive")) {
                    rxw.unbindAllChannels();
                }
                logger.debug("unbinding all channels");

            } else if ((itemname.equals(itemName))
                    && (provider.getItemType(itemname).toString().contains("SwitchItem"))
                    && !(provider.getChannel(itemName).equals("bind"))) {

                if (command.toString().equals("ON")) {
                    pc.turnOn(Byte.parseByte(provider.getChannel(itemName)));
                    logger.debug(provider.getChannel(itemName));
                } else if (command.toString().equals("OFF")) {
                    pc.turnOff(Byte.parseByte(provider.getChannel(itemName)));
                    logger.debug(provider.getChannel(itemName));
                }
            } else if ((itemname.equals(itemName))
                    && (provider.getItemType(itemname).toString().contains("DimmerItem"))) {
                pc.setLevel(Byte.parseByte(provider.getChannel(itemName)),
                        (byte) Integer.parseInt(command.toString()));
                //logger.debug(provider.getChannel(itemName));
            } else if ((itemname.equals(itemName))
                    && (provider.getItemType(itemname).toString().contains("StringItem"))) {
                String[] Colors = command.toString().split(",");

                byte red = (byte) (Integer.parseInt(Colors[0]) * 2.55);
                byte green = (byte) (Integer.parseInt(Colors[1]) * 2.55);
                byte blue = (byte) (Integer.parseInt(Colors[2]) * 2.55);

                pc.setLevelRGB(Byte.parseByte(provider.getChannel(itemName)), red, green, blue);

            }

        }
    }
}