Changeset 13173 in josm for trunk/src/org/openstreetmap/josm/data/Preferences.java
- Timestamp:
- 2017-11-28T00:56:29+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r13076 r13173 11 11 import java.io.Reader; 12 12 import java.io.StringWriter; 13 import java.lang.annotation.Retention;14 import java.lang.annotation.RetentionPolicy;15 13 import java.nio.charset.StandardCharsets; 16 14 import java.util.ArrayList; … … 38 36 39 37 import org.openstreetmap.josm.Main; 40 import org.openstreetmap.josm.data.preferences.BooleanProperty;41 38 import org.openstreetmap.josm.data.preferences.ColorInfo; 42 import org.openstreetmap.josm.data.preferences.DoubleProperty;43 import org.openstreetmap.josm.data.preferences.IntegerProperty;44 import org.openstreetmap.josm.data.preferences.LongProperty;45 39 import org.openstreetmap.josm.data.preferences.NamedColorProperty; 46 40 import org.openstreetmap.josm.data.preferences.PreferencesReader; … … 51 45 import org.openstreetmap.josm.spi.preferences.Config; 52 46 import org.openstreetmap.josm.spi.preferences.IBaseDirectories; 53 import org.openstreetmap.josm.spi.preferences.IPreferences;54 import org.openstreetmap.josm.spi.preferences.ListListSetting;55 47 import org.openstreetmap.josm.spi.preferences.ListSetting; 56 import org.openstreetmap.josm.spi.preferences.MapListSetting;57 48 import org.openstreetmap.josm.spi.preferences.Setting; 58 49 import org.openstreetmap.josm.spi.preferences.StringSetting; … … 137 128 protected boolean initSuccessful; 138 129 139 /**140 * Event triggered when a preference entry value changes.141 * @deprecated use {@link org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent}142 */143 @Deprecated144 public interface PreferenceChangeEvent {145 /**146 * Returns the preference key.147 * @return the preference key148 */149 String getKey();150 151 /**152 * Returns the old preference value.153 * @return the old preference value154 */155 Setting<?> getOldValue();156 157 /**158 * Returns the new preference value.159 * @return the new preference value160 */161 Setting<?> getNewValue();162 }163 164 /**165 * Listener to preference change events.166 * @since 10600 (functional interface)167 * @deprecated use {@link org.openstreetmap.josm.spi.preferences.PreferenceChangedListener}168 */169 @FunctionalInterface170 @Deprecated171 public interface PreferenceChangedListener {172 /**173 * Trigerred when a preference entry value changes.174 * @param e the preference change event175 */176 void preferenceChanged(PreferenceChangeEvent e);177 }178 179 /**180 * @deprecated private class is deprecated181 */182 @Deprecated183 private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {184 private final String key;185 private final Setting<?> oldValue;186 private final Setting<?> newValue;187 188 DefaultPreferenceChangeEvent(String key, Setting<?> oldValue, Setting<?> newValue) {189 this.key = key;190 this.oldValue = oldValue;191 this.newValue = newValue;192 }193 194 @Override195 public String getKey() {196 return key;197 }198 199 @Override200 public Setting<?> getOldValue() {201 return oldValue;202 }203 204 @Override205 public Setting<?> getNewValue() {206 return newValue;207 }208 }209 210 130 private final ListenerList<org.openstreetmap.josm.spi.preferences.PreferenceChangedListener> listeners = ListenerList.create(); 211 131 212 132 private final HashMap<String, ListenerList<org.openstreetmap.josm.spi.preferences.PreferenceChangedListener>> keyListeners = new HashMap<>(); 213 214 /**215 * @deprecated deprecated private field216 */217 @Deprecated218 private final ListenerList<Preferences.PreferenceChangedListener> listenersDeprecated = ListenerList.create();219 220 /**221 * @deprecated deprecated private field222 */223 @Deprecated224 private final HashMap<String, ListenerList<Preferences.PreferenceChangedListener>> keyListenersDeprecated = new HashMap<>();225 133 226 134 /** … … 266 174 267 175 /** 268 * Adds a new preferences listener.269 * @param listener The listener to add270 * @deprecated use {@link #addPreferenceChangeListener(org.openstreetmap.josm.spi.preferences.PreferenceChangedListener)}271 */272 @Deprecated273 public void addPreferenceChangeListener(Preferences.PreferenceChangedListener listener) {274 if (listener != null) {275 listenersDeprecated.addListener(listener);276 }277 }278 279 /**280 176 * Removes a preferences listener. 281 177 * @param listener The listener to remove … … 285 181 public void removePreferenceChangeListener(org.openstreetmap.josm.spi.preferences.PreferenceChangedListener listener) { 286 182 listeners.removeListener(listener); 287 }288 289 /**290 * Removes a preferences listener.291 * @param listener The listener to remove292 * @deprecated use {@link #removePreferenceChangeListener(org.openstreetmap.josm.spi.preferences.PreferenceChangedListener)}293 */294 @Deprecated295 public void removePreferenceChangeListener(Preferences.PreferenceChangedListener listener) {296 listenersDeprecated.removeListener(listener);297 183 } 298 184 … … 309 195 310 196 /** 311 * Adds a listener that only listens to changes in one preference312 * @param key The preference key to listen to313 * @param listener The listener to add.314 * @since 10824315 * @deprecated use316 * {@link #addKeyPreferenceChangeListener(java.lang.String, org.openstreetmap.josm.spi.preferences.PreferenceChangedListener)}317 */318 @Deprecated319 public void addKeyPreferenceChangeListener(String key, Preferences.PreferenceChangedListener listener) {320 listenersForKeyDeprecated(key).addListener(listener);321 }322 323 /**324 197 * Adds a weak listener that only listens to changes in one preference 325 198 * @param key The preference key to listen to … … 333 206 private ListenerList<org.openstreetmap.josm.spi.preferences.PreferenceChangedListener> listenersForKey(String key) { 334 207 return keyListeners.computeIfAbsent(key, k -> ListenerList.create()); 335 }336 337 /**338 * @param key preference key339 * @return listener list for this key340 * @deprecated deprecated private method341 */342 @Deprecated343 private ListenerList<Preferences.PreferenceChangedListener> listenersForKeyDeprecated(String key) {344 return keyListenersDeprecated.computeIfAbsent(key, k -> ListenerList.create());345 208 } 346 209 … … 358 221 } 359 222 360 /**361 * Removes a listener that only listens to changes in one preference362 * @param key The preference key to listen to363 * @param listener The listener to add.364 * @deprecated use365 * {@link #removeKeyPreferenceChangeListener(java.lang.String, org.openstreetmap.josm.spi.preferences.PreferenceChangedListener)}366 */367 @Deprecated368 public void removeKeyPreferenceChangeListener(String key, Preferences.PreferenceChangedListener listener) {369 Optional.ofNullable(keyListenersDeprecated.get(key)).orElseThrow(370 () -> new IllegalArgumentException("There are no listeners registered for " + key))371 .removeListener(listener);372 }373 374 223 protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) { 375 224 final org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent evt = … … 381 230 forKey.fireEvent(listener -> listener.preferenceChanged(evt)); 382 231 } 383 firePreferenceChangedDeprecated(key, oldValue, newValue); 384 } 385 386 /** 387 * @param key preference key 388 * @param oldValue old value 389 * @param newValue new value 390 * @deprecated deprecated private method 391 */ 392 @Deprecated 393 private void firePreferenceChangedDeprecated(String key, Setting<?> oldValue, Setting<?> newValue) { 394 final Preferences.PreferenceChangeEvent evtDeprecated = new Preferences.DefaultPreferenceChangeEvent(key, oldValue, newValue); 395 listenersDeprecated.fireEvent(listener -> listener.preferenceChanged(evtDeprecated)); 396 397 ListenerList<Preferences.PreferenceChangedListener> forKeyDeprecated = keyListenersDeprecated.get(key); 398 if (forKeyDeprecated != null) { 399 forKeyDeprecated.fireEvent(listener -> listener.preferenceChanged(evtDeprecated)); 400 } 401 } 402 403 /** 404 * Get the base name of the JOSM directories for preferences, cache and 405 * user data. 232 } 233 234 /** 235 * Get the base name of the JOSM directories for preferences, cache and user data. 406 236 * Default value is "JOSM", unless overridden by system property "josm.dir.name". 407 * @return the base name of the JOSM directories for preferences, cache and 408 * user data 237 * @return the base name of the JOSM directories for preferences, cache and user data 409 238 */ 410 239 public String getJOSMDirectoryBaseName() { … … 639 468 } 640 469 return all; 641 }642 643 /**644 * Gets an boolean that may be specialized645 * @param key The basic key646 * @param specName The sub-key to append to the key647 * @param def The default value648 * @return The boolean value or the default value if it could not be parsed649 * @deprecated use {@link PreferencesUtils#getBoolean(IPreferences, String, String, boolean)}650 */651 @Deprecated652 public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {653 boolean generic = getBoolean(key, def);654 String skey = key+'.'+specName;655 Setting<?> prop = settingsMap.get(skey);656 if (prop instanceof StringSetting)657 return Boolean.parseBoolean(((StringSetting) prop).getValue());658 else659 return generic;660 }661 662 /**663 * Set a boolean value for a certain setting.664 * @param key the unique identifier for the setting665 * @param value The new value666 * @return {@code true}, if something has changed (i.e. value is different than before)667 * @see BooleanProperty668 * @deprecated use {@link IPreferences#putBoolean(String, boolean)}669 */670 @Deprecated671 public boolean put(final String key, final boolean value) {672 return put(key, Boolean.toString(value));673 }674 675 /**676 * Set a boolean value for a certain setting.677 * @param key the unique identifier for the setting678 * @param value The new value679 * @return {@code true}, if something has changed (i.e. value is different than before)680 * @see IntegerProperty#put(Integer)681 * @deprecated use {@link IPreferences#putInt(String, int)}682 */683 @Deprecated684 public boolean putInteger(final String key, final Integer value) {685 return put(key, Integer.toString(value));686 }687 688 /**689 * Set a boolean value for a certain setting.690 * @param key the unique identifier for the setting691 * @param value The new value692 * @return {@code true}, if something has changed (i.e. value is different than before)693 * @see DoubleProperty#put(Double)694 * @deprecated use {@link IPreferences#putDouble(java.lang.String, double)}695 */696 @Deprecated697 public boolean putDouble(final String key, final Double value) {698 return put(key, Double.toString(value));699 }700 701 /**702 * Set a boolean value for a certain setting.703 * @param key the unique identifier for the setting704 * @param value The new value705 * @return {@code true}, if something has changed (i.e. value is different than before)706 * @see LongProperty#put(Long)707 * @deprecated use {@link IPreferences#putLong(java.lang.String, long)}708 */709 @Deprecated710 public boolean putLong(final String key, final Long value) {711 return put(key, Long.toString(value));712 470 } 713 471 … … 1050 808 1051 809 /** 1052 * Gets an integer preference1053 * @param key The preference key1054 * @param def The default value to use1055 * @return The integer1056 * @see IntegerProperty#get()1057 * @deprecated use {@link IPreferences#getInt(String, int)}1058 */1059 @Deprecated1060 public synchronized int getInteger(String key, int def) {1061 String v = get(key, Integer.toString(def));1062 if (v.isEmpty())1063 return def;1064 1065 try {1066 return Integer.parseInt(v);1067 } catch (NumberFormatException e) {1068 // fall out1069 Logging.trace(e);1070 }1071 return def;1072 }1073 1074 /**1075 * Gets an integer that may be specialized1076 * @param key The basic key1077 * @param specName The sub-key to append to the key1078 * @param def The default value1079 * @return The integer value or the default value if it could not be parsed1080 * @deprecated use {@link PreferencesUtils#getInteger(IPreferences, String, String, int)}1081 */1082 @Deprecated1083 public synchronized int getInteger(String key, String specName, int def) {1084 String v = get(key+'.'+specName);1085 if (v.isEmpty())1086 v = get(key, Integer.toString(def));1087 if (v.isEmpty())1088 return def;1089 1090 try {1091 return Integer.parseInt(v);1092 } catch (NumberFormatException e) {1093 // fall out1094 Logging.trace(e);1095 }1096 return def;1097 }1098 1099 /**1100 * Get a list of values for a certain key1101 * @param key the identifier for the setting1102 * @param def the default value.1103 * @return the corresponding value if the property has been set before, {@code def} otherwise1104 * @deprecated use {@link IPreferences#getList(java.lang.String, java.util.List)}1105 */1106 @Deprecated1107 public Collection<String> getCollection(String key, Collection<String> def) {1108 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();1109 }1110 1111 /**1112 * Get a list of values for a certain key1113 * @param key the identifier for the setting1114 * @return the corresponding value if the property has been set before, an empty collection otherwise.1115 * @deprecated use {@link IPreferences#getList(java.lang.String)}1116 */1117 @Deprecated1118 public Collection<String> getCollection(String key) {1119 Collection<String> val = getList(key, null);1120 return val == null ? Collections.<String>emptyList() : val;1121 }1122 1123 /**1124 * Removes a value from a given String collection1125 * @param key The preference key the collection is stored with1126 * @param value The value that should be removed in the collection1127 * @see #getList(String)1128 * @deprecated use {@link PreferencesUtils#removeFromList(IPreferences, String, String)}1129 */1130 @Deprecated1131 public synchronized void removeFromCollection(String key, String value) {1132 List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));1133 a.remove(value);1134 putList(key, a);1135 }1136 1137 /**1138 810 * Set a value for a certain setting. The changed setting is saved to the preference file immediately. 1139 811 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem. … … 1218 890 } 1219 891 1220 /**1221 * Put a collection.1222 * @param key key1223 * @param value value1224 * @return {@code true}, if something has changed (i.e. value is different than before)1225 * @deprecated use {@link IPreferences#putList(java.lang.String, java.util.List)}1226 */1227 @Deprecated1228 public boolean putCollection(String key, Collection<String> value) {1229 return putSetting(key, value == null ? null : ListSetting.create(value));1230 }1231 1232 /**1233 * Saves at most {@code maxsize} items of collection {@code val}.1234 * @param key key1235 * @param maxsize max number of items to save1236 * @param val value1237 * @return {@code true}, if something has changed (i.e. value is different than before)1238 * @deprecated use {@link PreferencesUtils#putListBounded(IPreferences, String, int, List)}1239 */1240 @Deprecated1241 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {1242 List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));1243 for (String i : val) {1244 if (newCollection.size() >= maxsize) {1245 break;1246 }1247 newCollection.add(i);1248 }1249 return putList(key, newCollection);1250 }1251 1252 /**1253 * Used to read a 2-dimensional array of strings from the preference file.1254 * If not a single entry could be found, <code>def</code> is returned.1255 * @param key preference key1256 * @param def default array value1257 * @return array value1258 * @deprecated use {@link #getListOfLists(java.lang.String, java.util.List)}1259 */1260 @Deprecated1261 @SuppressWarnings({ "unchecked", "rawtypes" })1262 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {1263 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);1264 return (Collection) val.getValue();1265 }1266 1267 /**1268 * Gets a collection of string collections for the given key1269 * @param key The key1270 * @return The collection of string collections or an empty collection as default1271 * @deprecated use {@link IPreferences#getListOfLists(java.lang.String)}1272 */1273 @Deprecated1274 public Collection<Collection<String>> getArray(String key) {1275 Collection<Collection<String>> res = getArray(key, null);1276 return res == null ? Collections.<Collection<String>>emptyList() : res;1277 }1278 1279 /**1280 * Put an array.1281 * @param key key1282 * @param value value1283 * @return {@code true}, if something has changed (i.e. value is different than before)1284 * @deprecated use {@link IPreferences#putListOfLists(java.lang.String, java.util.List)}1285 */1286 @Deprecated1287 public boolean putArray(String key, Collection<Collection<String>> value) {1288 return putSetting(key, value == null ? null : ListListSetting.create(value));1289 }1290 1291 /**1292 * Gets a collection of key/value maps.1293 * @param key The key to search at1294 * @param def The default value to use1295 * @return The stored value or the default one if it could not be parsed1296 * @deprecated use {@link IPreferences#getListOfMaps(java.lang.String, java.util.List)}1297 */1298 @Deprecated1299 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {1300 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();1301 }1302 1303 /**1304 * Stores a list of structs1305 * @param key The key to store the list in1306 * @param value A list of key/value maps1307 * @return <code>true</code> if the value was changed1308 * @see #getListOfMaps(java.lang.String, java.util.List)1309 * @deprecated use {@link IPreferences#putListOfMaps(java.lang.String, java.util.List)}1310 */1311 @Deprecated1312 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {1313 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));1314 }1315 1316 /**1317 * Annotation used for converting objects to String Maps and vice versa.1318 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.1319 *1320 * @see #serializeStruct(java.lang.Object, java.lang.Class)1321 * @see #deserializeStruct(java.util.Map, java.lang.Class)1322 * @deprecated use {@link StructUtils.StructEntry}1323 */1324 @Deprecated1325 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime1326 public @interface pref { }1327 1328 /**1329 * Annotation used for converting objects to String Maps.1330 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.1331 *1332 * @see #serializeStruct(java.lang.Object, java.lang.Class)1333 * @deprecated use {@link StructUtils.WriteExplicitly}1334 */1335 @Deprecated1336 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime1337 public @interface writeExplicitly { }1338 1339 /**1340 * Get a list of hashes which are represented by a struct-like class.1341 * Possible properties are given by fields of the class klass that have the @pref annotation.1342 * Default constructor is used to initialize the struct objects, properties then override some of these default values.1343 * @param <T> klass type1344 * @param key main preference key1345 * @param klass The struct class1346 * @return a list of objects of type T or an empty list if nothing was found1347 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Class)}1348 */1349 @Deprecated1350 public <T> List<T> getListOfStructs(String key, Class<T> klass) {1351 return StructUtils.getListOfStructs(this, key, klass);1352 }1353 1354 /**1355 * same as above, but returns def if nothing was found1356 * @param <T> klass type1357 * @param key main preference key1358 * @param def default value1359 * @param klass The struct class1360 * @return a list of objects of type T or {@code def} if nothing was found1361 * @deprecated use {@link StructUtils#getListOfStructs(IPreferences, String, Collection, Class)}1362 */1363 @Deprecated1364 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {1365 return StructUtils.getListOfStructs(this, key, def, klass);1366 }1367 1368 /**1369 * Convenience method that saves a MapListSetting which is provided as a collection of objects.1370 *1371 * Each object is converted to a <code>Map<String, String></code> using the fields with {@link pref} annotation.1372 * The field name is the key and the value will be converted to a string.1373 *1374 * Considers only fields that have the @pref annotation.1375 * In addition it does not write fields with null values. (Thus they are cleared)1376 * Default values are given by the field values after default constructor has been called.1377 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.1378 * @param <T> the class,1379 * @param key main preference key1380 * @param val the list that is supposed to be saved1381 * @param klass The struct class1382 * @return true if something has changed1383 * @deprecated use {@link StructUtils#putListOfStructs(IPreferences, String, Collection, Class)}1384 */1385 @Deprecated1386 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {1387 return StructUtils.putListOfStructs(this, key, val, klass);1388 }1389 1390 /**1391 * Convert an object to a String Map, by using field names and values as map key and value.1392 *1393 * The field value is converted to a String.1394 *1395 * Only fields with annotation {@link pref} are taken into account.1396 *1397 * Fields will not be written to the map if the value is null or unchanged1398 * (compared to an object created with the no-arg-constructor).1399 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.1400 *1401 * @param <T> the class of the object <code>struct</code>1402 * @param struct the object to be converted1403 * @param klass the class T1404 * @return the resulting map (same data content as <code>struct</code>)1405 * @deprecated use {@link StructUtils#serializeStruct(java.lang.Object, java.lang.Class)}1406 */1407 @Deprecated1408 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {1409 return StructUtils.serializeStruct(struct, klass);1410 }1411 1412 /**1413 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning1414 * map values to the corresponding fields.1415 *1416 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,1417 * Double, String, Map<String, String> and Map<String, List<String>>.1418 *1419 * Only fields with annotation {@link pref} are taken into account.1420 * @param <T> the class1421 * @param hash the string map with initial values1422 * @param klass the class T1423 * @return an object of class T, initialized as described above1424 * @deprecated use {@link StructUtils#deserializeStruct(java.util.Map, java.lang.Class)}1425 */1426 @Deprecated1427 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {1428 return StructUtils.deserializeStruct(hash, klass);1429 }1430 1431 892 @Override 1432 893 public Set<String> getKeySet() {
Note:
See TracChangeset
for help on using the changeset viewer.