001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.data.preferences;
003    
004    import org.openstreetmap.josm.Main;
005    
006    /**
007     * Captures the common functionality of preference properties
008     * @param <T> The type of object accessed by this property
009     */
010    public abstract class AbstractProperty<T> {
011        protected final String key;
012        protected final T defaultValue;
013    
014        /**
015         * Constructs a new {@code AbstractProperty}.
016         * @param key The property key
017         * @param defaultValue The default value
018         * @since 5464
019         */
020        public AbstractProperty(String key, T defaultValue) {
021            this.key = key;
022            this.defaultValue = defaultValue;
023        }
024    
025        /**
026         * Replies the property key.
027         * @return The property key
028         */
029        public String getKey() {
030            return key;
031        }
032    
033        /**
034         * Determines if this property is currently set in JOSM preferences.
035         * @return true if {@code Main.pref} contains this property.
036         */
037        public boolean isSet() {
038            return !Main.pref.get(key).isEmpty();
039        }
040    
041        /**
042         * Replies the default value of this property.
043         * @return The default value of this property
044         */
045        public T getDefaultValue() {
046            return defaultValue;
047        }
048    
049        /**
050         * Removes this property from JOSM preferences (i.e replace it by its default value).
051         */
052        public void remove() {
053            Main.pref.put(getKey(), String.valueOf(getDefaultValue()));
054        }
055        
056        /**
057         * Replies the value of this property.
058         * @return the value of this property
059         * @since 5464
060         */
061        public abstract T get();
062        
063        /**
064         * Sets this property to the specified value.
065         * @param value The new value of this property
066         * @return true if something has changed (i.e. value is different than before)
067         * @since 5464
068         */
069        public abstract boolean put(T value);
070    }