Class StructUtils
- java.lang.Object
-
- org.openstreetmap.josm.data.StructUtils
-
public final class StructUtils extends java.lang.Object
Utility methods to convert struct-like classes to a string map and back.A "struct" is a class that has some fields annotated with
StructUtils.StructEntry
. Those fields will be respected when converting an object to aMap
and back.- Since:
- 12851
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StructUtils.SerializeOptions
static interface
StructUtils.StructEntry
Annotation used for converting objects to String Maps and vice versa.static interface
StructUtils.WriteExplicitly
Annotation used for converting objects to String Maps.
-
Field Summary
Fields Modifier and Type Field Description private static StringParser
STRING_PARSER
-
Constructor Summary
Constructors Modifier Constructor Description private
StructUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
deserializeStruct(java.util.Map<java.lang.String,java.lang.String> hash, java.lang.Class<T> klass)
Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning map values to the corresponding fields.private static <T> java.lang.reflect.Field
getDeclaredFieldInClassOrSuperTypes(java.lang.Class<T> clazz, java.lang.String fieldName)
private static <T> java.lang.reflect.Field[]
getDeclaredFieldsInClassOrSuperTypes(java.lang.Class<T> clazz)
static <T> java.util.List<T>
getListOfStructs(IPreferences preferences, java.lang.String key, java.lang.Class<T> klass)
Get a list of hashes which are represented by a struct-like class.static <T> java.util.List<T>
getListOfStructs(IPreferences preferences, java.lang.String key, java.util.Collection<T> def, java.lang.Class<T> klass)
same as above, but returns def if nothing was foundprivate static java.util.Map<java.lang.String,java.lang.String>
mapFromJson(java.lang.String s)
private static java.lang.String
mapToJson(java.util.Map<?,?> map)
private static MultiMap<java.lang.String,java.lang.String>
multiMapFromJson(java.lang.String s)
private static java.lang.String
multiMapToJson(MultiMap<?,?> map)
static <T> boolean
putListOfStructs(IPreferences preferences, java.lang.String key, java.util.Collection<T> val, java.lang.Class<T> klass)
Convenience method that saves a MapListSetting which is provided as a collection of objects.private static <T> java.util.List<java.util.Map<java.lang.String,java.lang.String>>
serializeListOfStructs(java.util.Collection<T> l, java.lang.Class<T> klass)
static <T> java.util.Map<java.lang.String,java.lang.String>
serializeStruct(T struct, java.lang.Class<T> klass, StructUtils.SerializeOptions... options)
Convert an object to a String Map, by using field names and values as map key and value.
-
-
-
Field Detail
-
STRING_PARSER
private static final StringParser STRING_PARSER
-
-
Constructor Detail
-
StructUtils
private StructUtils()
-
-
Method Detail
-
getListOfStructs
public static <T> java.util.List<T> getListOfStructs(IPreferences preferences, java.lang.String key, java.lang.Class<T> klass)
Get a list of hashes which are represented by a struct-like class. Possible properties are given by fields of the class klass that have the @StructEntry annotation. Default constructor is used to initialize the struct objects, properties then override some of these default values.- Type Parameters:
T
- klass type- Parameters:
preferences
- preferences to look up the valuekey
- main preference keyklass
- The struct class- Returns:
- a list of objects of type T or an empty list if nothing was found
-
getListOfStructs
public static <T> java.util.List<T> getListOfStructs(IPreferences preferences, java.lang.String key, java.util.Collection<T> def, java.lang.Class<T> klass)
same as above, but returns def if nothing was found- Type Parameters:
T
- klass type- Parameters:
preferences
- preferences to look up the valuekey
- main preference keydef
- default valueklass
- The struct class- Returns:
- a list of objects of type T or
def
if nothing was found
-
putListOfStructs
public static <T> boolean putListOfStructs(IPreferences preferences, java.lang.String key, java.util.Collection<T> val, java.lang.Class<T> klass)
Convenience method that saves a MapListSetting which is provided as a collection of objects.Each object is converted to a
Map<String, String>
using the fields withStructUtils.StructEntry
annotation. The field name is the key and the value will be converted to a string.Considers only fields that have the
@StructEntry
annotation. In addition it does not write fields with null values. (Thus they are cleared) Default values are given by the field values after default constructor has been called. Fields equal to the default value are not written unless the field has theStructUtils.WriteExplicitly
annotation.- Type Parameters:
T
- the class,- Parameters:
preferences
- the preferences to save tokey
- main preference keyval
- the list that is supposed to be savedklass
- The struct class- Returns:
- true if something has changed
-
serializeListOfStructs
private static <T> java.util.List<java.util.Map<java.lang.String,java.lang.String>> serializeListOfStructs(java.util.Collection<T> l, java.lang.Class<T> klass)
-
serializeStruct
public static <T> java.util.Map<java.lang.String,java.lang.String> serializeStruct(T struct, java.lang.Class<T> klass, StructUtils.SerializeOptions... options)
Convert an object to a String Map, by using field names and values as map key and value.The field value is converted to a String.
Only fields with annotation
StructUtils.StructEntry
are taken into account.Fields will not be written to the map if the value is null or unchanged (compared to an object created with the no-arg-constructor). The
StructUtils.WriteExplicitly
annotation overrides this behavior, i.e. the default value will also be written.- Type Parameters:
T
- the class of the objectstruct
- Parameters:
struct
- the object to be convertedklass
- the class Toptions
- optional serialization options- Returns:
- the resulting map (same data content as
struct
)
-
deserializeStruct
public static <T> T deserializeStruct(java.util.Map<java.lang.String,java.lang.String> hash, java.lang.Class<T> klass)
Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning map values to the corresponding fields.The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double, Double, String, Map<String, String> and Map<String, List<String>>.
Only fields with annotation
StructUtils.StructEntry
are taken into account.- Type Parameters:
T
- the class- Parameters:
hash
- the string map with initial valuesklass
- the class T- Returns:
- an object of class T, initialized as described above
-
getDeclaredFieldInClassOrSuperTypes
private static <T> java.lang.reflect.Field getDeclaredFieldInClassOrSuperTypes(java.lang.Class<T> clazz, java.lang.String fieldName)
-
getDeclaredFieldsInClassOrSuperTypes
private static <T> java.lang.reflect.Field[] getDeclaredFieldsInClassOrSuperTypes(java.lang.Class<T> clazz)
-
mapToJson
private static java.lang.String mapToJson(java.util.Map<?,?> map)
-
mapFromJson
private static java.util.Map<java.lang.String,java.lang.String> mapFromJson(java.lang.String s)
-
multiMapToJson
private static java.lang.String multiMapToJson(MultiMap<?,?> map)
-
multiMapFromJson
private static MultiMap<java.lang.String,java.lang.String> multiMapFromJson(java.lang.String s)
-
-