Java Code Examples for java.lang.Float#parseFloat()

The following examples show how to use java.lang.Float#parseFloat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoValue_TestClassBundledGCM.java    From auto-value-bundle with MIT License 5 votes vote down vote up
public static TestClassBundledGCM unbundle(Bundle bundle, Gson gson) {
    return new AutoValue_TestClassBundledGCM(
            bundle,
            Byte.parseByte(bundle.getString("some_byte")),
            Boolean.parseBoolean(bundle.getString("some_boolean")),
            Short.parseShort(bundle.getString("some_short")),
            Integer.parseInt(bundle.getString("some_int")),
            Long.parseLong(bundle.getString("some_long")),
            bundle.getString("some_char").charAt(0),
            Float.parseFloat(bundle.getString("some_float")),
            Double.parseDouble(bundle.getString("some_double")),
            bundle.getString("some_string"),
            bundle.getString("some_char_sequence"),
            gson.fromJson(bundle.getString("some_parcelable"), Parcelable.class),
            gson.fromJson(bundle.getString("some_parcelable_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<Parcelable>>(){}.getType()),
            gson.fromJson(bundle.getString("some_parcelable_sparse_array"), new com.google.common.reflect.TypeToken<android.util.SparseArray<Parcelable>>(){}.getType()),
            gson.fromJson(bundle.getString("some_serializable"), Serializable.class),
            gson.fromJson(bundle.getString("some_integer_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<Integer>>(){}.getType()),
            gson.fromJson(bundle.getString("some_string_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<String>>(){}.getType()),
            gson.fromJson(bundle.getString("some_char_sequence_array_list"), new com.google.common.reflect.TypeToken<java.util.ArrayList<CharSequence>>(){}.getType()),
            toPrimitive(gson.fromJson(bundle.getString("some_byte_array"), Byte[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_short_array"), Short[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_char_array"), Character[].class)),
            toPrimitive(gson.fromJson(bundle.getString("some_float_array"), Float[].class)),
            gson.fromJson(bundle.getString("some_unknown_object"), new com.google.common.reflect.TypeToken<UnknownObject>(){}.getType()),
            gson.fromJson(bundle.getString("some_unknown_object_list"), new com.google.common.reflect.TypeToken<ArrayList<UnknownObject>>(){}.getType()),
            gson.fromJson(bundle.getString("test_enum"), new com.google.common.reflect.TypeToken<TestEnum>(){}.getType()));
}
 
Example 2
Source File: AbstractPreferences.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 3
Source File: AbstractPreferences.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 4
Source File: AbstractPreferences.java    From jdk-1.7-annotated with Apache License 2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 5
Source File: AbstractPreferences.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 6
Source File: AbstractPreferences.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 7
Source File: AbstractPreferences.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 8
Source File: AbstractPreferences.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 9
Source File: AbstractPreferences.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 10
Source File: AbstractPreferences.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 11
Source File: AbstractPreferences.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 12
Source File: AbstractPreferences.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the {@code getFloat} method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) get(key,
 * null)}.  If the return value is non-null, the implementation
 * attempts to translate it to an {@code float} with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, {@code def} is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with {@code key}
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         {@code key} in this preference node, or {@code def} if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if {@code key} is {@code null}.
 * @throws IllegalArgumentException if key contains the null control
 *         character, code point U+0000.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 13
Source File: AbstractPreferences.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 14
Source File: AbstractPreferences.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 15
Source File: AbstractPreferences.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 16
Source File: AbstractPreferences.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 17
Source File: AbstractPreferences.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}
 
Example 18
Source File: AbstractPreferences.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Implements the <tt>getFloat</tt> method as per the specification in
 * {@link Preferences#getFloat(String,float)}.
 *
 * <p>This implementation invokes {@link #get(String,String) <tt>get(key,
 * null)</tt>}.  If the return value is non-null, the implementation
 * attempts to translate it to an <tt>float</tt> with
 * {@link Float#parseFloat(String)}.  If the attempt succeeds, the return
 * value is returned by this method.  Otherwise, <tt>def</tt> is returned.
 *
 * @param key key whose associated value is to be returned as a float.
 * @param def the value to be returned in the event that this
 *        preference node has no value associated with <tt>key</tt>
 *        or the associated value cannot be interpreted as a float.
 * @return the float value represented by the string associated with
 *         <tt>key</tt> in this preference node, or <tt>def</tt> if the
 *         associated value does not exist or cannot be interpreted as
 *         a float.
 * @throws IllegalStateException if this node (or an ancestor) has been
 *         removed with the {@link #removeNode()} method.
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>.
 */
public float getFloat(String key, float def) {
    float result = def;
    try {
        String value = get(key, null);
        if (value != null)
            result = Float.parseFloat(value);
    } catch (NumberFormatException e) {
        // Ignoring exception causes specified default to be returned
    }

    return result;
}