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