Class 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 a Map and back.

    Since:
    12851
    • 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 found
      private 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 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> 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 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,
                                                   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 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> 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 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​(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 values
        klass - 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)
      • 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)
      • multiMapFromJson

        private static MultiMap<java.lang.String,​java.lang.String> multiMapFromJson​(java.lang.String s)