Class StructUtils

java.lang.Object
org.openstreetmap.josm.data.StructUtils

public final class StructUtils extends 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 a Map and back.

Since:
12851
  • Field Details

  • Constructor Details

  • Method Details

    • getListOfStructs

      public static <T> List<T> getListOfStructs(IPreferences preferences, String key, 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 value
      key - main preference key
      klass - The struct class
      Returns:
      a list of objects of type T or an empty list if nothing was found
    • getListOfStructs

      public static <T> List<T> getListOfStructs(IPreferences preferences, String key, Collection<T> def, 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 value
      key - main preference key
      def - default value
      klass - 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, String key, Collection<T> val, 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 with StructUtils.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 the StructUtils.WriteExplicitly annotation.

      Type Parameters:
      T - the class,
      Parameters:
      preferences - the preferences to save to
      key - main preference key
      val - the list that is supposed to be saved
      klass - The struct class
      Returns:
      true if something has changed
    • serializeListOfStructs

      private static <T> List<Map<String,String>> serializeListOfStructs(Collection<T> l, Class<T> klass)
    • serializeStruct

      public static <T> Map<String,String> serializeStruct(T struct, 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 object struct
      Parameters:
      struct - the object to be converted
      klass - the class T
      options - optional serialization options
      Returns:
      the resulting map (same data content as struct)
    • deserializeStruct

      public static <T> T deserializeStruct(Map<String,String> hash, 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 values
      klass - the class T
      Returns:
      an object of class T, initialized as described above
    • getDeclaredFieldInClassOrSuperTypes

      private static <T> Field getDeclaredFieldInClassOrSuperTypes(Class<T> clazz, String fieldName)
    • getDeclaredFieldsInClassOrSuperTypes

      private static <T> Field[] getDeclaredFieldsInClassOrSuperTypes(Class<T> clazz)
    • mapToJson

      private static String mapToJson(Map<?,?> map)
    • mapFromJson

      private static Map<String,String> mapFromJson(String s)
    • multiMapToJson

      private static String multiMapToJson(MultiMap<?,?> map)
    • multiMapFromJson