Changeset 12840 in josm for trunk/src/org
- Timestamp:
- 2017-09-13T16:03:51+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r12649 r12840 56 56 57 57 import org.openstreetmap.josm.Main; 58 import org.openstreetmap.josm.data.preferences.AbstractPreferences; 58 59 import org.openstreetmap.josm.data.preferences.BooleanProperty; 59 60 import org.openstreetmap.josm.data.preferences.ColorProperty; 60 import org.openstreetmap.josm.data.preferences. DoubleProperty;61 import org.openstreetmap.josm.data.preferences.IPreferences; 61 62 import org.openstreetmap.josm.data.preferences.IntegerProperty; 62 63 import org.openstreetmap.josm.data.preferences.ListListSetting; … … 104 105 * @since 74 105 106 */ 106 public class Preferences {107 public class Preferences extends AbstractPreferences { 107 108 108 109 private static final String COLOR_PREFIX = "color."; … … 255 256 * @param listener The listener to add 256 257 */ 258 @Override 257 259 public void addPreferenceChangeListener(PreferenceChangedListener listener) { 258 260 if (listener != null) { … … 265 267 * @param listener The listener to remove 266 268 */ 269 @Override 267 270 public void removePreferenceChangeListener(PreferenceChangedListener listener) { 268 271 listeners.removeListener(listener); … … 275 278 * @since 10824 276 279 */ 280 @Override 277 281 public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) { 278 282 listenersForKey(key).addListener(listener); … … 303 307 * @param listener The listener to add. 304 308 */ 309 @Override 305 310 public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) { 306 311 Optional.ofNullable(keyListeners.get(key)).orElseThrow( … … 480 485 481 486 /** 482 * Get settings value for a certain key.483 * @param key the identifier for the setting484 * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.485 */486 public synchronized String get(final String key) {487 String value = get(key, null);488 return value == null ? "" : value;489 }490 491 /**492 * Get settings value for a certain key and provide default a value.493 * @param key the identifier for the setting494 * @param def the default value. For each call of get() with a given key, the default value must be the same.495 * @return the corresponding value if the property has been set before, {@code def} otherwise496 */497 public synchronized String get(final String key, final String def) {498 return getSetting(key, new StringSetting(def), StringSetting.class).getValue();499 }500 501 /**502 487 * Gets all normal (string) settings that have a key starting with the prefix 503 488 * @param prefix The start of the key … … 552 537 553 538 /** 554 * Gets a boolean preference555 * @param key The preference key556 * @return The boolean or <code>false</code> if it could not be parsed557 * @see IntegerProperty#get()558 */559 public synchronized boolean getBoolean(final String key) {560 String s = get(key, null);561 return s != null && Boolean.parseBoolean(s);562 }563 564 /**565 * Gets a boolean preference566 * @param key The preference key567 * @param def The default value to use568 * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset569 * @see IntegerProperty#get()570 */571 public synchronized boolean getBoolean(final String key, final boolean def) {572 return Boolean.parseBoolean(get(key, Boolean.toString(def)));573 }574 575 /**576 539 * Gets an boolean that may be specialized 577 540 * @param key The basic key … … 591 554 592 555 /** 593 * Set a value for a certain setting.594 * @param key the unique identifier for the setting595 * @param value the value of the setting. Can be null or "" which both removes the key-value entry.596 * @return {@code true}, if something has changed (i.e. value is different than before)597 */598 public boolean put(final String key, String value) {599 return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));600 }601 602 /**603 556 * Set a boolean value for a certain setting. 604 557 * @param key the unique identifier for the setting … … 606 559 * @return {@code true}, if something has changed (i.e. value is different than before) 607 560 * @see BooleanProperty 608 */ 561 * @deprecated use {@link IPreferences#putBoolean(String, boolean)} 562 */ 563 @Deprecated 609 564 public boolean put(final String key, final boolean value) { 610 565 return put(key, Boolean.toString(value)); … … 617 572 * @return {@code true}, if something has changed (i.e. value is different than before) 618 573 * @see IntegerProperty#put(Integer) 619 */ 574 * @deprecated use {@link IPreferences#putInt(String, int)} 575 */ 576 @Deprecated 620 577 public boolean putInteger(final String key, final Integer value) { 621 578 return put(key, Integer.toString(value)); … … 628 585 * @return {@code true}, if something has changed (i.e. value is different than before) 629 586 * @see DoubleProperty#put(Double) 630 */ 587 * @deprecated use {@link IPreferences#putDouble(java.lang.String, double)} 588 */ 589 @Deprecated 631 590 public boolean putDouble(final String key, final Double value) { 632 591 return put(key, Double.toString(value)); … … 663 622 if (!defaults) { 664 623 /* currently unused, but may help to fix configuration issues in future */ 665 putInt eger("josm.version", Version.getInstance().getVersion());624 putInt("josm.version", Version.getInstance().getVersion()); 666 625 667 626 updateSystemProperties(); … … 974 933 * @return The integer 975 934 * @see IntegerProperty#get() 976 */ 935 * @deprecated use {@link IPreferences#getInt(String, int)} 936 */ 937 @Deprecated 977 938 public synchronized int getInteger(String key, int def) { 978 939 String v = get(key, Integer.toString(def)); … … 1034 995 1035 996 /** 1036 * Gets a double preference1037 * @param key The preference key1038 * @param def The default value to use1039 * @return The double value or the default value if it could not be parsed1040 * @see LongProperty#get()1041 */1042 public synchronized double getDouble(String key, double def) {1043 String v = get(key, Double.toString(def));1044 if (null == v)1045 return def;1046 1047 try {1048 return Double.parseDouble(v);1049 } catch (NumberFormatException e) {1050 // fall out1051 Logging.trace(e);1052 }1053 return def;1054 }1055 1056 /**1057 997 * Get a list of values for a certain key 1058 998 * @param key the identifier for the setting 1059 999 * @param def the default value. 1060 1000 * @return the corresponding value if the property has been set before, {@code def} otherwise 1061 */ 1001 * @deprecated use {@link IPreferences#getList(java.lang.String, java.util.List)} 1002 */ 1003 @Deprecated 1062 1004 public Collection<String> getCollection(String key, Collection<String> def) { 1063 1005 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue(); … … 1068 1010 * @param key the identifier for the setting 1069 1011 * @return the corresponding value if the property has been set before, an empty collection otherwise. 1070 */ 1012 * @deprecated use {@link IPreferences#getList(java.lang.String)} 1013 */ 1014 @Deprecated 1071 1015 public Collection<String> getCollection(String key) { 1072 Collection<String> val = get Collection(key, null);1016 Collection<String> val = getList(key, null); 1073 1017 return val == null ? Collections.<String>emptyList() : val; 1074 1018 } … … 1078 1022 * @param key The preference key the collection is stored with 1079 1023 * @param value The value that should be removed in the collection 1080 * @see #get Collection(String)1024 * @see #getList(String) 1081 1025 */ 1082 1026 public synchronized void removeFromCollection(String key, String value) { 1083 List<String> a = new ArrayList<>(get Collection(key, Collections.<String>emptyList()));1027 List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList())); 1084 1028 a.remove(value); 1085 put Collection(key, a);1029 putList(key, a); 1086 1030 } 1087 1031 … … 1093 1037 * @return {@code true}, if something has changed (i.e. value is different than before) 1094 1038 */ 1039 @Override 1095 1040 public boolean putSetting(final String key, Setting<?> setting) { 1096 1041 CheckParameterUtil.ensureParameterNotNull(key); … … 1146 1091 */ 1147 1092 @SuppressWarnings("unchecked") 1093 @Override 1148 1094 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) { 1149 1095 CheckParameterUtil.ensureParameterNotNull(key); … … 1172 1118 * @param value value 1173 1119 * @return {@code true}, if something has changed (i.e. value is different than before) 1174 */ 1120 * @deprecated use {@link IPreferences#putList(java.lang.String, java.util.List)} 1121 */ 1122 @Deprecated 1175 1123 public boolean putCollection(String key, Collection<String> value) { 1176 1124 return putSetting(key, value == null ? null : ListSetting.create(value)); … … 1185 1133 */ 1186 1134 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) { 1187 Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));1135 List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size())); 1188 1136 for (String i : val) { 1189 1137 if (newCollection.size() >= maxsize) { … … 1192 1140 newCollection.add(i); 1193 1141 } 1194 return put Collection(key, newCollection);1142 return putList(key, newCollection); 1195 1143 } 1196 1144 … … 1212 1160 * @param key The key 1213 1161 * @return The collection of string collections or an empty collection as default 1214 */ 1162 * @deprecated use {@link IPreferences#getListOfLists(java.lang.String)} 1163 */ 1164 @Deprecated 1215 1165 public Collection<Collection<String>> getArray(String key) { 1216 1166 Collection<Collection<String>> res = getArray(key, null); … … 1223 1173 * @param value value 1224 1174 * @return {@code true}, if something has changed (i.e. value is different than before) 1225 */ 1175 * @deprecated use {@link IPreferences#putListOfLists(java.lang.String, java.util.List)} 1176 */ 1177 @Deprecated 1226 1178 public boolean putArray(String key, Collection<Collection<String>> value) { 1227 1179 return putSetting(key, value == null ? null : ListListSetting.create(value)); … … 1233 1185 * @param def The default value to use 1234 1186 * @return The stored value or the default one if it could not be parsed 1235 */ 1187 * @deprecated use {@link IPreferences#getListOfMaps(java.lang.String, java.util.List)} 1188 */ 1189 @Deprecated 1236 1190 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) { 1237 1191 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue(); … … 1243 1197 * @param value A list of key/value maps 1244 1198 * @return <code>true</code> if the value was changed 1245 * @see #getListOfStructs(String, Collection) 1246 */ 1199 * @see #getListOfMaps(String, Collection) 1200 * @deprecated use {@link IPreferences#putListOfMaps(java.lang.String, java.util.List)} 1201 */ 1202 @Deprecated 1247 1203 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) { 1248 1204 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value))); … … 1290 1246 */ 1291 1247 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) { 1292 Collection<Map<String, String>> prop =1293 getListOf Structs(key, def == null ? null : serializeListOfStructs(def, klass));1248 List<Map<String, String>> prop = 1249 getListOfMaps(key, def == null ? null : serializeListOfStructs(def, klass)); 1294 1250 if (prop == null) 1295 1251 return def == null ? null : new ArrayList<>(def); … … 1314 1270 */ 1315 1271 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) { 1316 return putListOf Structs(key, serializeListOfStructs(val, klass));1317 } 1318 1319 private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {1272 return putListOfMaps(key, serializeListOfStructs(val, klass)); 1273 } 1274 1275 private static <T> List<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) { 1320 1276 if (l == null) 1321 1277 return null; 1322 Collection<Map<String, String>> vals = new ArrayList<>();1278 List<Map<String, String>> vals = new ArrayList<>(); 1323 1279 for (T struct : l) { 1324 1280 if (struct != null) { … … 1575 1531 */ 1576 1532 public Collection<String> getPluginSites() { 1577 return get Collection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));1533 return getList("pluginmanager.sites", Collections.singletonList(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>")); 1578 1534 } 1579 1535 … … 1602 1558 */ 1603 1559 public void setPluginSites(Collection<String> sites) { 1604 put Collection("pluginmanager.sites", sites);1560 putList("pluginmanager.sites", new ArrayList<>(sites)); 1605 1561 } 1606 1562 … … 1702 1658 } 1703 1659 if (modified) { 1704 putListOf Structs(key, l);1660 putListOfMaps(key, l); 1705 1661 } 1706 1662 } … … 1718 1674 l.add(helper.serialize(val.get())); 1719 1675 } 1720 putListOf Structs(key, l);1676 putListOfMaps(key, l); 1721 1677 } 1722 1678 } -
trunk/src/org/openstreetmap/josm/data/preferences/CollectionProperty.java
r10824 r12840 8 8 /** 9 9 * A property containing a {@code Collection} of {@code String} as value. 10 * @deprecated use {@link ListProperty} 10 11 */ 12 @Deprecated 11 13 public class CollectionProperty extends AbstractProperty<Collection<String>> { 12 14
Note:
See TracChangeset
for help on using the changeset viewer.