Changeset 9759 in josm for trunk/src


Ignore:
Timestamp:
2016-02-08T08:52:08+01:00 (8 years ago)
Author:
Don-vip
Message:

refactor preferences settings classes

Location:
trunk/src/org/openstreetmap/josm
Files:
7 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ShowStatusReportAction.java

    r9332 r9759  
    2323
    2424import org.openstreetmap.josm.Main;
    25 import org.openstreetmap.josm.data.Preferences.Setting;
    2625import org.openstreetmap.josm.data.Version;
    2726import org.openstreetmap.josm.data.osm.DataSet;
    2827import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
     28import org.openstreetmap.josm.data.preferences.Setting;
    2929import org.openstreetmap.josm.gui.ExtendedDialog;
    3030import org.openstreetmap.josm.gui.widgets.JosmTextArea;
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r9524 r9759  
    4242
    4343import org.openstreetmap.josm.Main;
    44 import org.openstreetmap.josm.data.Preferences.ListListSetting;
    45 import org.openstreetmap.josm.data.Preferences.ListSetting;
    46 import org.openstreetmap.josm.data.Preferences.MapListSetting;
    47 import org.openstreetmap.josm.data.Preferences.Setting;
    48 import org.openstreetmap.josm.data.Preferences.StringSetting;
     44import org.openstreetmap.josm.data.preferences.ListListSetting;
     45import org.openstreetmap.josm.data.preferences.ListSetting;
     46import org.openstreetmap.josm.data.preferences.MapListSetting;
     47import org.openstreetmap.josm.data.preferences.Setting;
     48import org.openstreetmap.josm.data.preferences.StringSetting;
    4949import org.openstreetmap.josm.gui.io.DownloadFileTask;
    5050import org.openstreetmap.josm.plugins.PluginDownloadTask;
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r9755 r9759  
    6565import org.openstreetmap.josm.Main;
    6666import org.openstreetmap.josm.data.preferences.ColorProperty;
     67import org.openstreetmap.josm.data.preferences.ListListSetting;
     68import org.openstreetmap.josm.data.preferences.ListSetting;
     69import org.openstreetmap.josm.data.preferences.MapListSetting;
     70import org.openstreetmap.josm.data.preferences.Setting;
     71import org.openstreetmap.josm.data.preferences.SettingVisitor;
     72import org.openstreetmap.josm.data.preferences.StringSetting;
    6773import org.openstreetmap.josm.io.CachedFile;
    6874import org.openstreetmap.josm.io.OfflineAccessException;
     
    158164     */
    159165    protected boolean initSuccessful = false;
    160 
    161     /**
    162      * Interface for a preference value.
    163      *
    164      * Implementations must provide a proper <code>equals</code> method.
    165      *
    166      * @param <T> the data type for the value
    167      */
    168     public interface Setting<T> {
    169         /**
    170          * Returns the value of this setting.
    171          *
    172          * @return the value of this setting
    173          */
    174         T getValue();
    175 
    176         /**
    177          * Check if the value of this Setting object is equal to the given value.
    178          * @param otherVal the other value
    179          * @return true if the values are equal
    180          */
    181         boolean equalVal(T otherVal);
    182 
    183         /**
    184          * Clone the current object.
    185          * @return an identical copy of the current object
    186          */
    187         Setting<T> copy();
    188 
    189         /**
    190          * Enable usage of the visitor pattern.
    191          *
    192          * @param visitor the visitor
    193          */
    194         void visit(SettingVisitor visitor);
    195 
    196         /**
    197          * Returns a setting whose value is null.
    198          *
    199          * Cannot be static, because there is no static inheritance.
    200          * @return a Setting object that isn't null itself, but returns null
    201          * for {@link #getValue()}
    202          */
    203         Setting<T> getNullInstance();
    204     }
    205 
    206     /**
    207      * Base abstract class of all settings, holding the setting value.
    208      *
    209      * @param <T> The setting type
    210      */
    211     public abstract static class AbstractSetting<T> implements Setting<T> {
    212         protected final T value;
    213         /**
    214          * Constructs a new {@code AbstractSetting} with the given value
    215          * @param value The setting value
    216          */
    217         public AbstractSetting(T value) {
    218             this.value = value;
    219         }
    220 
    221         @Override
    222         public T getValue() {
    223             return value;
    224         }
    225 
    226         @Override
    227         public String toString() {
    228             return value != null ? value.toString() : "null";
    229         }
    230 
    231         @Override
    232         public int hashCode() {
    233             return Objects.hash(value);
    234         }
    235 
    236         @Override
    237         public boolean equals(Object obj) {
    238             if (this == obj) return true;
    239             if (obj == null || getClass() != obj.getClass()) return false;
    240             AbstractSetting<?> that = (AbstractSetting<?>) obj;
    241             return Objects.equals(value, that.value);
    242         }
    243     }
    244 
    245     /**
    246      * Setting containing a {@link String} value.
    247      */
    248     public static class StringSetting extends AbstractSetting<String> {
    249         /**
    250          * Constructs a new {@code StringSetting} with the given value
    251          * @param value The setting value
    252          */
    253         public StringSetting(String value) {
    254             super(value);
    255         }
    256 
    257         @Override
    258         public boolean equalVal(String otherVal) {
    259             if (value == null) return otherVal == null;
    260             return value.equals(otherVal);
    261         }
    262 
    263         @Override
    264         public StringSetting copy() {
    265             return new StringSetting(value);
    266         }
    267 
    268         @Override
    269         public void visit(SettingVisitor visitor) {
    270             visitor.visit(this);
    271         }
    272 
    273         @Override
    274         public StringSetting getNullInstance() {
    275             return new StringSetting(null);
    276         }
    277 
    278         @Override
    279         public boolean equals(Object other) {
    280             if (!(other instanceof StringSetting)) return false;
    281             return equalVal(((StringSetting) other).getValue());
    282         }
    283     }
    284 
    285     /**
    286      * Setting containing a {@link List} of {@link String} values.
    287      */
    288     public static class ListSetting extends AbstractSetting<List<String>> {
    289         /**
    290          * Constructs a new {@code ListSetting} with the given value
    291          * @param value The setting value
    292          */
    293         public ListSetting(List<String> value) {
    294             super(value);
    295             consistencyTest();
    296         }
    297 
    298         /**
    299          * Convenience factory method.
    300          * @param value the value
    301          * @return a corresponding ListSetting object
    302          */
    303         public static ListSetting create(Collection<String> value) {
    304             return new ListSetting(value == null ? null : Collections.unmodifiableList(new ArrayList<>(value)));
    305         }
    306 
    307         @Override
    308         public boolean equalVal(List<String> otherVal) {
    309             return Utils.equalCollection(value, otherVal);
    310         }
    311 
    312         @Override
    313         public ListSetting copy() {
    314             return ListSetting.create(value);
    315         }
    316 
    317         private void consistencyTest() {
    318             if (value != null && value.contains(null))
    319                 throw new RuntimeException("Error: Null as list element in preference setting");
    320         }
    321 
    322         @Override
    323         public void visit(SettingVisitor visitor) {
    324             visitor.visit(this);
    325         }
    326 
    327         @Override
    328         public ListSetting getNullInstance() {
    329             return new ListSetting(null);
    330         }
    331 
    332         @Override
    333         public boolean equals(Object other) {
    334             if (!(other instanceof ListSetting)) return false;
    335             return equalVal(((ListSetting) other).getValue());
    336         }
    337     }
    338 
    339     /**
    340      * Setting containing a {@link List} of {@code List}s of {@link String} values.
    341      */
    342     public static class ListListSetting extends AbstractSetting<List<List<String>>> {
    343 
    344         /**
    345          * Constructs a new {@code ListListSetting} with the given value
    346          * @param value The setting value
    347          */
    348         public ListListSetting(List<List<String>> value) {
    349             super(value);
    350             consistencyTest();
    351         }
    352 
    353         /**
    354          * Convenience factory method.
    355          * @param value the value
    356          * @return a corresponding ListListSetting object
    357          */
    358         public static ListListSetting create(Collection<Collection<String>> value) {
    359             if (value != null) {
    360                 List<List<String>> valueList = new ArrayList<>(value.size());
    361                 for (Collection<String> lst : value) {
    362                     valueList.add(new ArrayList<>(lst));
    363                 }
    364                 return new ListListSetting(valueList);
    365             }
    366             return new ListListSetting(null);
    367         }
    368 
    369         @Override
    370         public boolean equalVal(List<List<String>> otherVal) {
    371             if (value == null) return otherVal == null;
    372             if (otherVal == null) return false;
    373             if (value.size() != otherVal.size()) return false;
    374             Iterator<List<String>> itA = value.iterator();
    375             Iterator<List<String>> itB = otherVal.iterator();
    376             while (itA.hasNext()) {
    377                 if (!Utils.equalCollection(itA.next(), itB.next())) return false;
    378             }
    379             return true;
    380         }
    381 
    382         @Override
    383         public ListListSetting copy() {
    384             if (value == null) return new ListListSetting(null);
    385 
    386             List<List<String>> copy = new ArrayList<>(value.size());
    387             for (Collection<String> lst : value) {
    388                 List<String> lstCopy = new ArrayList<>(lst);
    389                 copy.add(Collections.unmodifiableList(lstCopy));
    390             }
    391             return new ListListSetting(Collections.unmodifiableList(copy));
    392         }
    393 
    394         private void consistencyTest() {
    395             if (value == null) return;
    396             if (value.contains(null)) throw new RuntimeException("Error: Null as list element in preference setting");
    397             for (Collection<String> lst : value) {
    398                 if (lst.contains(null)) throw new RuntimeException("Error: Null as inner list element in preference setting");
    399             }
    400         }
    401 
    402         @Override
    403         public void visit(SettingVisitor visitor) {
    404             visitor.visit(this);
    405         }
    406 
    407         @Override
    408         public ListListSetting getNullInstance() {
    409             return new ListListSetting(null);
    410         }
    411 
    412         @Override
    413         public boolean equals(Object other) {
    414             if (!(other instanceof ListListSetting)) return false;
    415             return equalVal(((ListListSetting) other).getValue());
    416         }
    417     }
    418 
    419     /**
    420      * Setting containing a {@link List} of {@link Map}s of {@link String} values.
    421      */
    422     public static class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
    423 
    424         /**
    425          * Constructs a new {@code MapListSetting} with the given value
    426          * @param value The setting value
    427          */
    428         public MapListSetting(List<Map<String, String>> value) {
    429             super(value);
    430             consistencyTest();
    431         }
    432 
    433         @Override
    434         public boolean equalVal(List<Map<String, String>> otherVal) {
    435             if (value == null) return otherVal == null;
    436             if (otherVal == null) return false;
    437             if (value.size() != otherVal.size()) return false;
    438             Iterator<Map<String, String>> itA = value.iterator();
    439             Iterator<Map<String, String>> itB = otherVal.iterator();
    440             while (itA.hasNext()) {
    441                 if (!equalMap(itA.next(), itB.next())) return false;
    442             }
    443             return true;
    444         }
    445 
    446         private static boolean equalMap(Map<String, String> a, Map<String, String> b) {
    447             if (a == null) return b == null;
    448             if (b == null) return false;
    449             if (a.size() != b.size()) return false;
    450             for (Entry<String, String> e : a.entrySet()) {
    451                 if (!Objects.equals(e.getValue(), b.get(e.getKey()))) return false;
    452             }
    453             return true;
    454         }
    455 
    456         @Override
    457         public MapListSetting copy() {
    458             if (value == null) return new MapListSetting(null);
    459             List<Map<String, String>> copy = new ArrayList<>(value.size());
    460             for (Map<String, String> map : value) {
    461                 Map<String, String> mapCopy = new LinkedHashMap<>(map);
    462                 copy.add(Collections.unmodifiableMap(mapCopy));
    463             }
    464             return new MapListSetting(Collections.unmodifiableList(copy));
    465         }
    466 
    467         private void consistencyTest() {
    468             if (value == null) return;
    469             if (value.contains(null)) throw new RuntimeException("Error: Null as list element in preference setting");
    470             for (Map<String, String> map : value) {
    471                 if (map.keySet().contains(null)) throw new RuntimeException("Error: Null as map key in preference setting");
    472                 if (map.values().contains(null)) throw new RuntimeException("Error: Null as map value in preference setting");
    473             }
    474         }
    475 
    476         @Override
    477         public void visit(SettingVisitor visitor) {
    478             visitor.visit(this);
    479         }
    480 
    481         @Override
    482         public MapListSetting getNullInstance() {
    483             return new MapListSetting(null);
    484         }
    485 
    486         @Override
    487         public boolean equals(Object other) {
    488             if (!(other instanceof MapListSetting)) return false;
    489             return equalVal(((MapListSetting) other).getValue());
    490         }
    491     }
    492 
    493     public interface SettingVisitor {
    494         void visit(StringSetting setting);
    495 
    496         void visit(ListSetting value);
    497 
    498         void visit(ListListSetting value);
    499 
    500         void visit(MapListSetting value);
    501     }
    502166
    503167    /**
  • trunk/src/org/openstreetmap/josm/gui/JosmUserIdentityManager.java

    r9527 r9759  
    99import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
    1010import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
    11 import org.openstreetmap.josm.data.Preferences.StringSetting;
    1211import org.openstreetmap.josm.data.osm.User;
    1312import org.openstreetmap.josm.data.osm.UserInfo;
     13import org.openstreetmap.josm.data.preferences.StringSetting;
    1414import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    1515import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r9685 r9759  
    4141import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
    4242import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
    43 import org.openstreetmap.josm.data.Preferences.Setting;
    4443import org.openstreetmap.josm.data.Version;
    4544import org.openstreetmap.josm.data.osm.Changeset;
    4645import org.openstreetmap.josm.data.osm.DataSet;
    4746import org.openstreetmap.josm.data.osm.OsmPrimitive;
     47import org.openstreetmap.josm.data.preferences.Setting;
    4848import org.openstreetmap.josm.gui.ExtendedDialog;
    4949import org.openstreetmap.josm.gui.HelpAwareOptionPane;
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

    r9622 r9759  
    4040import org.openstreetmap.josm.data.CustomConfigurator;
    4141import org.openstreetmap.josm.data.Preferences;
    42 import org.openstreetmap.josm.data.Preferences.Setting;
     42import org.openstreetmap.josm.data.preferences.Setting;
     43import org.openstreetmap.josm.data.preferences.StringSetting;
    4344import org.openstreetmap.josm.gui.dialogs.LogShowDialog;
    4445import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
     
    241242        for (PrefEntry p: table.getSelectedItems()) {
    242243            // preferences with default values are not saved
    243             if (!(p.getValue() instanceof Preferences.StringSetting)) {
     244            if (!(p.getValue() instanceof StringSetting)) {
    244245                hasLists = true; // => append and replace differs
    245246            }
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ExportProfileAction.java

    r9239 r9759  
    2020import org.openstreetmap.josm.data.CustomConfigurator;
    2121import org.openstreetmap.josm.data.Preferences;
    22 import org.openstreetmap.josm.data.Preferences.Setting;
     22import org.openstreetmap.josm.data.preferences.Setting;
    2323import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    2424import org.openstreetmap.josm.tools.Utils;
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ListEditor.java

    r9507 r9759  
    1616import javax.swing.table.AbstractTableModel;
    1717
    18 import org.openstreetmap.josm.data.Preferences.ListSetting;
     18import org.openstreetmap.josm.data.preferences.ListSetting;
    1919import org.openstreetmap.josm.gui.widgets.JosmTextField;
    2020import org.openstreetmap.josm.tools.GBC;
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ListListEditor.java

    r9507 r9759  
    1212import javax.swing.table.AbstractTableModel;
    1313
    14 import org.openstreetmap.josm.data.Preferences.ListListSetting;
     14import org.openstreetmap.josm.data.preferences.ListListSetting;
    1515
    1616/**
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/MapListEditor.java

    r9507 r9759  
    1616import javax.swing.table.AbstractTableModel;
    1717
    18 import org.openstreetmap.josm.data.Preferences.MapListSetting;
     18import org.openstreetmap.josm.data.preferences.MapListSetting;
    1919
    2020/**
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/PrefEntry.java

    r9078 r9759  
    22package org.openstreetmap.josm.gui.preferences.advanced;
    33
    4 import org.openstreetmap.josm.data.Preferences.Setting;
     4import org.openstreetmap.josm.data.preferences.Setting;
    55import org.openstreetmap.josm.tools.CheckParameterUtil;
    66
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/PreferencesTable.java

    r9224 r9759  
    2929
    3030import org.openstreetmap.josm.Main;
    31 import org.openstreetmap.josm.data.Preferences.ListListSetting;
    32 import org.openstreetmap.josm.data.Preferences.ListSetting;
    33 import org.openstreetmap.josm.data.Preferences.MapListSetting;
    34 import org.openstreetmap.josm.data.Preferences.Setting;
    35 import org.openstreetmap.josm.data.Preferences.StringSetting;
     31import org.openstreetmap.josm.data.preferences.ListListSetting;
     32import org.openstreetmap.josm.data.preferences.ListSetting;
     33import org.openstreetmap.josm.data.preferences.MapListSetting;
     34import org.openstreetmap.josm.data.preferences.Setting;
     35import org.openstreetmap.josm.data.preferences.StringSetting;
    3636import org.openstreetmap.josm.gui.ExtendedDialog;
    3737import org.openstreetmap.josm.gui.util.GuiHelper;
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/StringEditor.java

    r9239 r9759  
    1010import javax.swing.JPanel;
    1111
    12 import org.openstreetmap.josm.data.Preferences.StringSetting;
     12import org.openstreetmap.josm.data.preferences.StringSetting;
    1313import org.openstreetmap.josm.gui.ExtendedDialog;
    1414import org.openstreetmap.josm.gui.widgets.JosmTextField;
Note: See TracChangeset for help on using the changeset viewer.