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