Example usage for com.google.common.primitives Floats tryParse

List of usage examples for com.google.common.primitives Floats tryParse

Introduction

In this page you can find the example usage for com.google.common.primitives Floats tryParse.

Prototype

@Beta
@Nullable
@CheckForNull
@GwtIncompatible("regular expressions")
public static Float tryParse(String string) 

Source Link

Document

Parses the specified string as a single-precision floating point value.

Usage

From source file:asap.zeno.planunit.StopAnimationZU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    Float f = Floats.tryParse(value);
    if (f != null) {
        setFloatParameterValue(name, f);
    } else {/*from w w  w  .j a va 2  s  . c  om*/
        throw new InvalidParameterException(name, value);
    }
}

From source file:asap.zeno.planunit.SpeakZU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    if (name.equals("text")) {
        text = value;// ww w  .  j a  v  a 2s.co  m
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:asap.zeno.planunit.AnimationZU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    if (name.equals("animation")) {
        animation = value;/*from  w  w  w.j a  va  2 s. c  o m*/
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:asap.srnao.planunit.RunChoregrapheClipNU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {

    if (name.equals("clipName")) {
        clipName = value;/*from www  . j av a  2  s. c  o  m*/
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:asap.eyepiengine.planunit.GazeShiftEPU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    if (name.equals("topic"))
        topic = value;//  www  . j  a  v a 2 s  .  c om
    else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:asap.srnao.planunit.MoveJointNU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    if (name.equals("jointName")) {
        jointName = value;/*from w  w w  . j a  v  a  2  s  . co  m*/
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:com.cinchapi.common.reflect.Types.java

/**
 * Coerce the {@code object} in{@code to} the provided {@link Type}, if
 * possible. If the coercion cannot be done, an
 * {@link UnsupportedOperationException} is thrown.
 * /*  w  w  w  . ja  v a 2s .c o m*/
 * @param object
 * @param to
 * @param converter
 * @return an instance of {@link Type} {@code to} that is coerced from the
 *         original {@code object}
 * @throws UnsupportedOperationException
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T coerce(Object object, Type to, BiFunction<Type, String, T> converter)
        throws UnsupportedOperationException {
    if (to instanceof Class) {
        Class<?> type = (Class<?>) to;
        Object coerced = null;
        if (type == object.getClass() || type.isAssignableFrom(object.getClass())) {
            coerced = object;
        } else if (type.isEnum()) {
            coerced = Enums.parseIgnoreCase((Class<? extends Enum>) type, object);
        } else if (Number.class.isAssignableFrom(type)) {
            coerced = AnyStrings.tryParseNumberStrict(object.toString());
        } else if (type == int.class) {
            coerced = Ints.tryParse(object.toString());
        } else if (type == long.class) {
            coerced = Longs.tryParse(object.toString());
        } else if (type == float.class) {
            coerced = Floats.tryParse(object.toString());
        } else if (type == double.class) {
            coerced = Doubles.tryParse(object.toString());
        } else if (type == String.class) {
            coerced = object.toString();
        } else if (type == char.class || type == Character.class && object.toString().length() == 1) {
            coerced = object.toString().charAt(0);
        } else if (type == boolean.class || type == Boolean.class) {
            coerced = AnyStrings.tryParseBoolean(object.toString());
        } else if (type == Map.class || Sequences.isSequenceType(type)) {
            // Assume that the String representation contains JSON and use
            // nashorn to parse.
            try {
                coerced = nashorn.eval("Java.asJSONCompatible(" + object.toString() + ")");
                if (type.isArray()) {
                    // If the desired type is an array, we must coerce each
                    // of the elements to the array's component type.
                    Class<?> componentType = type.getComponentType();
                    Object array = Array.newInstance(componentType, ((Collection) coerced).size());
                    AtomicInteger index = new AtomicInteger(0);
                    Sequences.forEach(coerced, item -> Array.set(array, index.getAndIncrement(),
                            Types.coerce(item, componentType)));
                    coerced = array;
                } else if (Map.class.isAssignableFrom(type)) {
                    coerced = ImmutableMap.copyOf((Map) coerced);
                } else {
                    coerced = ImmutableList.copyOf((Iterable) coerced);
                }
            } catch (ScriptException e) {
            }
        } else {
            try {
                coerced = type.cast(object);
            } catch (ClassCastException e) {
            }
            if (coerced == null) {
                // As a last resort, try applying the #converter to the
                // string representation of the argument
                try {
                    Object converted = converter.apply(to, object.toString());
                    coerced = type.isAssignableFrom(converted.getClass()) ? converted : null;
                } catch (Exception e) {
                }
            }
        }
        if (coerced != null) {
            return (T) coerced;
        } else {
            throw new UnsupportedOperationException("Unable to coerce " + object + " into " + to);
        }
    } else {
        throw new UnsupportedOperationException("Unsupported type " + to);
    }
}

From source file:asap.srnao.planunit.SetJointAngleNU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {

    if (name.equals("jointName")) {
        jointName = value;//from w  ww .ja va2 s  .c om
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}

From source file:org.lbogdanov.poker.util.Settings.java

/**
 * Returns a value of a setting as <code>Float</code>. The value is returned as an instance of
 * {@link Optional} class to indicate the fact that it can be missing.
 * // w w  w  .j av a2 s  . co m
 * @return the value of the setting
 */
public Optional<Float> asFloat() {
    return Optional.fromNullable(Floats.tryParse(asString().or("")));
}

From source file:asap.zeno.planunit.DummySpeakZU.java

@Override
public void setParameterValue(String name, String value) throws ParameterException {
    if (name.equals("duration")) {
        try {/* w w w. jav a2s.c o  m*/
            duration = Integer.parseInt(value);
        } catch (NumberFormatException nfe) {
            throw new InvalidParameterException(name, value);
        }
    } else {
        Float f = Floats.tryParse(value);
        if (f != null) {
            setFloatParameterValue(name, f);
        } else {
            throw new InvalidParameterException(name, value);
        }
    }
}