Changeset 9129 in josm
- Timestamp:
- 2015-12-15T11:11:43+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r9120 r9129 1284 1284 } 1285 1285 1286 @Retention(RetentionPolicy.RUNTIME) public @interface pref { } 1287 @Retention(RetentionPolicy.RUNTIME) public @interface writeExplicitly { } 1286 /** 1287 * Annotation used for converting objects to String Maps and vice versa. 1288 * Indicates that a certain field should be considered in the conversion 1289 * process. Otherwise it is ignored. 1290 * 1291 * @see #serializeStruct(java.lang.Object, java.lang.Class) 1292 * @see #deserializeStruct(java.util.Map, java.lang.Class) 1293 */ 1294 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime 1295 public @interface pref { } 1296 1297 /** 1298 * Annotation used for converting objects to String Maps. 1299 * Indicates that a certain field should be written to the map, even if 1300 * the value is the same as the default value. 1301 * 1302 * @see #serializeStruct(java.lang.Object, java.lang.Class) 1303 */ 1304 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime 1305 public @interface writeExplicitly { } 1288 1306 1289 1307 /** … … 1322 1340 1323 1341 /** 1324 * Save a list of hashes represented by a struct-like class. 1342 * Convenience method that saves a MapListSetting which is provided as a 1343 * Collection of objects. 1344 * 1345 * Each object is converted to a <code>Map<String, String></code> using 1346 * the fields with {@link pref} annotation. The field name is the key and 1347 * the value will be converted to a string. 1348 * 1325 1349 * Considers only fields that have the @pref annotation. 1326 1350 * In addition it does not write fields with null values. (Thus they are cleared) … … 1329 1353 * Fields equal to the default value are not written unless the field has 1330 1354 * the @writeExplicitly annotation. 1355 * @param <T> the class, 1331 1356 * @param key main preference key 1332 1357 * @param val the list that is supposed to be saved … … 1384 1409 } 1385 1410 1411 /** 1412 * Convert an object to a String Map, by using field names and values as map 1413 * key and value. 1414 * 1415 * The field value is converted to a String. 1416 * 1417 * Only fields with annotation {@link pref} are taken into account. 1418 * 1419 * Fields will not be written to the map if the value is null or unchanged 1420 * (compared to an object created with the no-arg-constructor). 1421 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the 1422 * default value will also be written. 1423 * 1424 * @param <T> the class of the object <code>struct</code> 1425 * @param struct the object to be converted 1426 * @param klass the class T 1427 * @return the resulting map (same data content as <code>struct</code>) 1428 */ 1386 1429 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) { 1387 1430 T structPrototype; … … 1418 1461 } 1419 1462 1463 /** 1464 * Converts a String-Map to an object of a certain class, by comparing 1465 * map keys to field names of the class and assigning map values to the 1466 * corresponding fields. 1467 * 1468 * The map value (a String) is converted to the field type. Supported 1469 * types are: boolean, Boolean, int, Integer, double, Double, String and 1470 * Map<String, String>. 1471 * 1472 * Only fields with annotation {@link pref} are taken into account. 1473 * @param <T> the class 1474 * @param hash the string map with initial values 1475 * @param klass the class T 1476 * @return an object of class T, initialized as described above 1477 */ 1420 1478 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) { 1421 1479 T struct = null;
Note:
See TracChangeset
for help on using the changeset viewer.