Example usage for java.lang Byte valueOf

List of usage examples for java.lang Byte valueOf

Introduction

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

Prototype

public static Byte valueOf(String s) throws NumberFormatException 

Source Link

Document

Returns a Byte object holding the value given by the specified String .

Usage

From source file:com.beetle.framework.util.ObjectUtil.java

public final static void populate(Object obj, Map<String, Object> map) {
    Set<?> s = map.entrySet();
    Iterator<?> it = s.iterator();
    while (it.hasNext()) {
        String key = "";
        Object o = null;//from w w  w . ja  v a2  s .  c  o m
        @SuppressWarnings("rawtypes")
        Map.Entry me = (Map.Entry) it.next();
        try {
            key = me.getKey().toString();
            o = me.getValue();
            if (o == null) {
                continue;
            }
            setValue(key, obj, o);
        } catch (IllegalArgumentException e) {
            Class<Object> type = ObjectUtil.getType(key, obj);
            String tstr = type.toString();
            if (tstr.equals(Integer.class.toString())) {
                ObjectUtil.setValue(key, obj, Integer.valueOf(o.toString()));
            } else if (tstr.equals(Long.class.toString())) {
                ObjectUtil.setValue(key, obj, Long.valueOf(o.toString()));
            } else if (tstr.equals(Float.class.toString())) {
                ObjectUtil.setValue(key, obj, Float.valueOf(o.toString()));
            } else if (tstr.equals(Double.class.toString())) {
                ObjectUtil.setValue(key, obj, Double.valueOf(o.toString()));
            } else if (tstr.equals(Short.class.toString())) {
                ObjectUtil.setValue(key, obj, Short.valueOf(o.toString()));
            } else if (tstr.equals(Byte.class.toString())) {
                ObjectUtil.setValue(key, obj, Byte.valueOf(o.toString()));
            } else if (tstr.equals(Date.class.toString())) {
                if (o instanceof Date) {
                    ObjectUtil.setValue(key, obj, (Date) o);
                } else {
                    long time = ((Double) o).longValue();
                    ObjectUtil.setValue(key, obj, new Date(time));
                }
            } else if (tstr.equals(java.sql.Timestamp.class.toString())) {
                if (o instanceof java.sql.Timestamp) {
                    ObjectUtil.setValue(key, obj, (Date) o);
                } else {
                    long time = ((Double) o).longValue();
                    ObjectUtil.setValue(key, obj, new java.sql.Timestamp(time));
                }
            } else {
                throw e;
            }
            tstr = null;
            type = null;
        }
    }
}

From source file:org.datacleaner.util.convert.StandardTypeConverter.java

@Override
public Object fromString(Class<?> type, String str) {
    if (ReflectionUtils.isString(type)) {
        return str;
    }//from  w  w w  .j  av a  2 s. c  o m
    if (ReflectionUtils.isBoolean(type)) {
        return Boolean.valueOf(str);
    }
    if (ReflectionUtils.isCharacter(type)) {
        return Character.valueOf(str.charAt(0));
    }
    if (ReflectionUtils.isInteger(type)) {
        return Integer.valueOf(str);
    }
    if (ReflectionUtils.isLong(type)) {
        return Long.valueOf(str);
    }
    if (ReflectionUtils.isByte(type)) {
        return Byte.valueOf(str);
    }
    if (ReflectionUtils.isShort(type)) {
        return Short.valueOf(str);
    }
    if (ReflectionUtils.isDouble(type)) {
        return Double.valueOf(str);
    }
    if (ReflectionUtils.isFloat(type)) {
        return Float.valueOf(str);
    }
    if (ReflectionUtils.is(type, Class.class)) {
        try {
            return Class.forName(str);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Class not found: " + str, e);
        }
    }
    if (ReflectionUtils.is(type, EnumerationValue.class)) {
        return new EnumerationValue(str);
    }
    if (type.isEnum()) {
        try {
            Object[] enumConstants = type.getEnumConstants();

            // first look for enum constant matches
            Method nameMethod = Enum.class.getMethod("name");
            for (Object e : enumConstants) {
                String name = (String) nameMethod.invoke(e);
                if (name.equals(str)) {
                    return e;
                }
            }

            // check for aliased enums
            for (Object e : enumConstants) {
                String name = (String) nameMethod.invoke(e);
                Field field = type.getField(name);
                Alias alias = ReflectionUtils.getAnnotation(field, Alias.class);
                if (alias != null) {
                    String[] aliasValues = alias.value();
                    for (String aliasValue : aliasValues) {
                        if (aliasValue.equals(str)) {
                            return e;
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Unexpected error occurred while examining enum", e);
        }
        throw new IllegalArgumentException("No such enum '" + str + "' in enum class: " + type.getName());
    }
    if (ReflectionUtils.isDate(type)) {
        return toDate(str);
    }
    if (ReflectionUtils.is(type, File.class)) {
        final FileResolver fileResolver = new FileResolver(_configuration);
        return fileResolver.toFile(str);
    }
    if (ReflectionUtils.is(type, Calendar.class)) {
        Date date = toDate(str);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }
    if (ReflectionUtils.is(type, Pattern.class)) {
        try {
            return Pattern.compile(str);
        } catch (PatternSyntaxException e) {
            throw new IllegalArgumentException("Invalid regular expression syntax in '" + str + "'.", e);
        }
    }
    if (ReflectionUtils.is(type, java.sql.Date.class)) {
        Date date = toDate(str);
        return new java.sql.Date(date.getTime());
    }
    if (ReflectionUtils.isNumber(type)) {
        return ConvertToNumberTransformer.transformValue(str);
    }
    if (ReflectionUtils.is(type, Serializable.class)) {
        logger.warn("fromString(...): No built-in handling of type: {}, using deserialization", type.getName());
        byte[] bytes = (byte[]) _parentConverter.fromString(byte[].class, str);
        ChangeAwareObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ChangeAwareObjectInputStream(new ByteArrayInputStream(bytes));
            objectInputStream.addClassLoader(type.getClassLoader());
            Object obj = objectInputStream.readObject();
            return obj;
        } catch (Exception e) {
            throw new IllegalStateException("Could not deserialize to " + type + ".", e);
        } finally {
            FileHelper.safeClose(objectInputStream);
        }
    }

    throw new IllegalArgumentException("Could not convert to type: " + type.getName());
}

From source file:org.red5.io.mock.Output.java

/** {@inheritDoc} */
public void writeMap(Collection array, Serializer serializer) {
    list.add(Byte.valueOf(DataTypes.CORE_MAP));
    list.add(array);
}

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

/**
     * Parse value of specified type. The string value has to be in
     * standard notation for the specified type.
     *//*  w  w w  .j  a  v  a2  s. c  o m*/
    public static Object parse(Class type, String value) throws Exception {
        if (value == null) {
            return nullValue(type);
        } else if (value.length() == 0) {
            return type == String.class ? value : nullValue(type);
        }

        type = objectType(type);

        if (type == BigDecimal.class) {
            return new BigDecimal(value);
        } else if (type == BigInteger.class) {
            return new BigInteger(value);
        } else if (type == Boolean.class) {
            return parseBoolean(value);
        } else if (type == Byte.class) {
            return Byte.valueOf(value);
        } else if (type == Character.class) {
            return parseCharacter(value);
        } else if (type == Date.class) {
            return parseDate(value);
        } else if (type == Double.class) {
            return Double.valueOf(value);
        } else if (type == Float.class) {
            return Float.valueOf(value);
        } else if (type == Integer.class) {
            return Integer.valueOf(value);
        } else if (type == Long.class) {
            return Long.valueOf(value);
        } else if (type == Short.class) {
            return Short.valueOf(value);
        } else if (type == String.class) {
            return value;
        }
        throw new ParseException("Cannot parse type " + type, 0);
    }

From source file:unikn.dbis.univis.dataexchange.schemaimport.ImportUtil.java

/**
 * Returns the value at where the xpath is pointing to as type of the
 * <code>Class</code> type parameter. This method returns a default value
 * if no value was found at the xpath.// w  w  w  . j  a v  a2  s  .c  o m
 *
 * @param xpath The relative xpath to the value.
 * @param type          The <code>Class</code> of the return value.
 * @param defaultValue  The default value that should return if no value was
 *                      found at the xpath.
 * @return The value of the <code>Class</code> of the type parameter.
 * @throws SchemaImportException This exception occurs while trying
 *                                      to get the value.
 */
public static <T> T getValue(Document document, Class<T> type, String xpath, T defaultValue)
        throws SchemaImportException {

    String value = getContent(document, xpath);

    if (value == null) {
        return defaultValue;
    }

    if (String.class.isAssignableFrom(type)) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + String.class + " [XPath=\"" + xpath
                    + "\", StringValue=\"" + value + "\"].");
        }

        //noinspection unchecked
        return (T) value;
    } else if (Number.class.isAssignableFrom(type)) {

        if (Long.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Long.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Long.valueOf(value);
        } else if (Integer.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Integer.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Integer.valueOf(value);
        } else if (Double.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Double.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Double.valueOf(value);
        } else if (Float.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Float.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Float.valueOf(value);
        } else if (Byte.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Byte.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Byte.valueOf(value);
        } else if (Short.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + Short.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) Short.valueOf(value);
        } else if (BigDecimal.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + BigDecimal.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) new BigDecimal(value);
        } else if (BigInteger.class.isAssignableFrom(type)) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("The type is assignable from " + BigInteger.class + " [XPath=\"" + xpath
                        + "\", StringValue=\"" + value + "\"].");
            }

            //noinspection unchecked
            return (T) new BigInteger(value);
        }
    } else if (Boolean.class.isAssignableFrom(type)) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + Boolean.class + " [XPath=\"" + xpath
                    + "\", StringValue=\"" + value + "\"].");
        }

        //noinspection unchecked
        return (T) Boolean.valueOf(value);
    }
    /*
    else if (java.util.Date.class.isAssignableFrom(type)) {
            
    if (Date.class.isAssignableFrom(type)) {
            
        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + Date.class + " [XPath=\"" + xpath + "\", StringValue=\"" + value + "\"].");
        }
            
        //noinspection unchecked
        return (T) new java.sql.Date(getDate(value, params).getTime());
    }
    else if (Time.class.isAssignableFrom(type)) {
            
        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + Time.class + " [XPath=\"" + xpath + "\", StringValue=\"" + value + "\"].");
        }
            
        //noinspection unchecked
        return (T) new java.sql.Time(getDate(value, params).getTime());
    }
    else if (Timestamp.class.isAssignableFrom(type)) {
            
        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + Timestamp.class + " [XPath=\"" + xpath + "\", StringValue=\"" + value + "\"].");
        }
            
        //noinspection unchecked
        return (T) new java.sql.Timestamp(getDate(value, params).getTime());
    }
            
    if (LOG.isDebugEnabled()) {
        LOG.debug("The type is assignable from " + java.util.Date.class + " [XPath=\"" + xpath + "\", StringValue=\"" + value + "\"].");
    }
            
    //noinspection unchecked
    return (T) getDate(value, params);
    }
    */
    else if (Locale.class.isAssignableFrom(type)) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("The type is assignable from " + Locale.class + " [XPath=\"" + xpath
                    + "\", StringValue=\"" + value + "\"].");
        }

        //noinspection unchecked
        return (T) new Locale(value);
    }

    throw new SchemaImportException("The class [" + type + "] isn't assignable. The assignable types are ["
            + String.class + ", " + Long.class + ", " + Integer.class + ", " + Double.class + ", " + Float.class
            + ", " + Byte.class + ", " + Short.class + ", " + BigDecimal.class + ", " + BigInteger.class + ", "
            + Boolean.class + ", " + Date.class + ", " + java.sql.Date.class + ", " + java.sql.Time.class + ", "
            + java.sql.Timestamp.class + "] [XPath=\"" + xpath + "\", StringValue=\"" + value + "\"]");
}

From source file:com.lonepulse.icklebot.test.fragment.support.SupportedModelToViewBindingFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);
    shadow.onViewCreated(view, savedInstanceState);

    user = new User();
    user.setFullName("Lahiru Sahan J.");
    user.setGithubUsername("sahan");

    Contact contact = new Contact();
    contact.setEmail("lahiru@lonepulse.com");
    contact.setMobile("201184919");
    user.setContact(contact);//w  w w  . j a  v a2 s.  c om
    user.setOnline(true);

    Inventory inventory = new Inventory();
    inventory.setItem1(null);
    inventory.setItem2("bot");

    Map<String, String> items = new HashMap<String, String>();
    items.put("SKU1111", "Item1");
    items.put("SKU2222", "Item2");
    items.put("SKU3333", "Item3");
    items.put("SKU4444", "Item4");

    inventory.setItems(items);
    inventory.setLocations(new String[] { "Loc1", "Loc2", "Loc3", "Loc4" });
    user.setInventory(inventory);

    Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
    Bitmap decodedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    decodedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

    byte[] primitiveBytes = baos.toByteArray();

    Byte[] wrapperBytes = new Byte[primitiveBytes.length];

    for (int i = 0; i < primitiveBytes.length; i++)
        wrapperBytes[i] = Byte.valueOf(primitiveBytes[i]);

    user.setIcInt(R.drawable.ic_launcher);
    user.setIcWrapperInt(Integer.valueOf(R.drawable.ic_launcher));
    user.setIcDrawable(drawable);
    user.setIcBitmap(decodedBitmap);
    user.setIcPrimitiveBytes(primitiveBytes);
    user.setIcWrapperBytes(wrapperBytes);

    bindManager.bind(rootView, user);
}

From source file:eu.udig.catalog.jgrass.core.JGTtmsGeoResource.java

private JGTtmsProperties getTmsProperties(File tmsPropertiesFile) {
    JGTtmsProperties tmsProperties = new JGTtmsProperties();
    List<String> fileLines = new ArrayList<String>();
    try {//from   w w w  .j a v a2  s.co m
        fileLines = FileUtils.readLines(tmsPropertiesFile);
    } catch (IOException e1) {
        msg = e1;
        e1.printStackTrace();
    }

    for (String line : fileLines) {
        line = line.trim();
        if (line.length() == 0) {
            continue;
        }

        int split = line.indexOf('=');
        if (split != -1) {
            String value = line.substring(split + 1).trim();
            if (line.startsWith("url")) {

                int indexOfZ = value.indexOf("ZZZ");
                tmsProperties.HOST_NAME = value.substring(0, indexOfZ);
                tmsProperties.tilePart = value.substring(indexOfZ);
                if (value.startsWith("http")) {
                    // remove http
                    tmsProperties.HOST_NAME = tmsProperties.HOST_NAME.substring(7);
                } else {
                    tmsProperties.PROTOCOL = "file";
                    tmsProperties.HOST_NAME = tmsPropertiesFile.getParent() + File.separator
                            + tmsProperties.HOST_NAME;
                    tmsProperties.isFile = true;
                }
            }
            if (line.startsWith("minzoom")) {
                try {
                    tmsProperties.ZOOM_MIN = Byte.valueOf(value);
                } catch (Exception e) {
                    // use default: handle exception
                }
            }
            if (line.startsWith("maxzoom")) {
                try {
                    tmsProperties.ZOOM_MAX = Byte.valueOf(value);
                } catch (Exception e) {
                    // use default: handle exception
                }
            }
            if (line.startsWith("center")) {
                try {
                    String[] coord = value.split("\\s+"); //$NON-NLS-1$
                    double x = Double.parseDouble(coord[0]);
                    double y = Double.parseDouble(coord[1]);
                    tmsProperties.centerPoint = new Coordinate(x, y);
                } catch (NumberFormatException e) {
                    // use default
                }
            }
            if (line.startsWith("type")) {
                if (value.equals(JGTtmsProperties.TILESCHEMA.tms.toString())) {
                    tmsProperties.type = JGTtmsProperties.TILESCHEMA.tms;
                }
            }
        }
    }
    return tmsProperties;
}

From source file:it.greenvulcano.gvesb.gviamx.service.internal.PasswordResetManager.java

public void createPasswordResetRequest(String email) throws UserNotFoundException, UnverifiableUserException {

    User user = usersManager.getUser(email.toLowerCase());

    if (user.getPassword().isPresent()) {

        PasswordResetRequest passwordResetRequest = repository
                .get(email.toLowerCase(), PasswordResetRequest.class).orElseGet(PasswordResetRequest::new);
        passwordResetRequest.setUser((UserJPA) user);
        passwordResetRequest.setEmail(email.toLowerCase());
        passwordResetRequest.setIssueTime(new Date());
        passwordResetRequest.setExpireTime(expireTime);
        passwordResetRequest.setNotificationStatus(NotificationStatus.PENDING);

        byte[] token = new byte[4];
        secureRandom.nextBytes(token);/*from w ww .j a  va  2s  .c  om*/

        String clearTextToken = String.format(Locale.US, "%02x%02x%02x%02x",
                IntStream.range(0, token.length).mapToObj(i -> Byte.valueOf(token[i])).toArray());
        passwordResetRequest.setToken(DigestUtils.sha256Hex(clearTextToken));

        repository.add(passwordResetRequest);

        passwordResetRequest.setClearToken(clearTextToken);
        notificationServices.stream().map(
                n -> new NotificationManager.NotificationTask(n, passwordResetRequest, repository, "reset"))
                .forEach(executor::submit);

    } else {
        throw new UnverifiableUserException(email);
    }
}

From source file:org.red5.io.mock.Output.java

/** {@inheritDoc} */
public void writeObject(Object object, Serializer serializer) {
    list.add(Byte.valueOf(DataTypes.CORE_OBJECT));
    list.add(object);
}

From source file:org.datanucleus.store.scalaris.fieldmanager.FetchFieldManager.java

public byte fetchByteField(int fieldNumber) {
    String memberName = storeMgr.getNamingFactory()
            .getColumnName(acmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber), ColumnType.COLUMN);
    if (result.isNull(memberName)) {
        return 0;
    }//from   www  . j ava  2s . co  m
    try {
        String str = result.getString(memberName);
        return Byte.valueOf(str).byteValue();
    } catch (JSONException e) {
        // ignore
        return 0;
    }
}