001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.preferences;
003
004/**
005 * A property containing an {@code Double} value.
006 * @since 3246
007 */
008public class DoubleProperty extends AbstractToStringProperty<Double> {
009
010    /**
011     * Constructs a new {@code DoubleProperty}.
012     * @param key The property key
013     * @param defaultValue The default value
014     */
015    public DoubleProperty(String key, double defaultValue) {
016        super(key, defaultValue);
017    }
018
019    @Override
020    public Double get() {
021        // Removing this implementation breaks binary compatibility
022        return super.get();
023    }
024
025    @Override
026    public boolean put(Double value) {
027        // Removing this implementation breaks binary compatibility
028        return super.put(value);
029    }
030
031    @Override
032    protected Double fromString(String string) {
033        try {
034            return Double.valueOf(string);
035        } catch (NumberFormatException e) {
036            throw new InvalidPreferenceValueException(e);
037        }
038    }
039
040    @Override
041    protected String toString(Double t) {
042        return t.toString();
043    }
044
045    /**
046     * parses and saves a double precision value
047     * @param value the value to be parsed
048     * @return true - preference value has changed
049     *         false - parsing failed or preference value has not changed
050     */
051    public boolean parseAndPut(String value) {
052        try {
053            return put(Double.valueOf(value));
054        } catch (NumberFormatException ex) {
055            return false;
056        }
057    }
058}