Class DTOsImpl

  • All Implemented Interfaces:
    DTOs

    public class DTOsImpl
    extends java.lang.Object
    implements DTOs
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.Class<?>,​java.lang.reflect.Field[]> cache  
      private java.lang.reflect.Field[] EMPTY_FIELDS  
      (package private) static java.util.regex.Pattern ESCAPE_P  
      private static org.slf4j.Logger logger  
      private DTOsImpl.Link root  
      (package private) static java.util.regex.Pattern UNESCAPE_P  
    • Constructor Summary

      Constructors 
      Constructor Description
      DTOsImpl()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<java.lang.String,​java.lang.Object> asMap​(java.lang.Object dto)
      Return a partially read only Map object that maps directly to a DTO.
      (package private) int bsearch​(java.lang.reflect.Field[] a, int fromIndex, int toIndex, java.lang.String key)  
      <T> T deepCopy​(T source)
      Deep copy
      (package private) <T> T deepCopy​(T source, DTOsImpl.Link link)  
      boolean deepEquals​(java.lang.Object a, java.lang.Object b)
      Check if two DTOs fields are equal.
      java.util.List<DTOs.Difference> diff​(java.lang.Object older, java.lang.Object newer)
      Return a list of paths where the two objects differ.
      private boolean diff​(java.util.List<DTOs.Difference> diffs, DTOsImpl.Link link, java.lang.Object older, java.lang.Object newer)  
      boolean equals​(java.lang.Object a, java.lang.Object b)
      Check if two dtos fields are equal.
      java.lang.String escape​(java.lang.String unescaped)
      Escape a string to be used in a path.
      java.lang.String[] fromPathToSegments​(java.lang.String path)
      Takes a path with escaped '.'and '\' and then turns it into an array of unescaped keys
      (package private) java.lang.String[] fromPathToSegments​(java.lang.String path, int start, int n)  
      java.lang.String fromSegmentsToPath​(java.lang.String[] segments)
      Takes a path with unescaped keys and turns it into a string path where the \ and .
      java.util.Optional<java.lang.Object> get​(java.lang.Object dto, java.lang.String path)
      Access a DTO with a path.
      java.util.Optional<java.lang.Object> get​(java.lang.Object dto, java.lang.String... path)
      Access a DTO with a path that consists of an array with segments.
      private java.util.Optional<java.lang.Object> get​(java.lang.Object dto, java.lang.String[] path, int i, int max)  
      (package private) java.lang.reflect.Field getField​(java.lang.reflect.Field[] fields, java.lang.String name)  
      (package private) java.lang.reflect.Field[] getFields​(java.lang.Class<?> c)  
      (package private) java.lang.reflect.Field[] getFields​(java.lang.Object o)  
      int hashCode​(java.lang.Object dto)
      Calculate a hash Code for the fields in this DTO.
      boolean isComplex​(java.lang.Object a)
      Return true if the give dto is complex (either Map, Collection, Array, or has public fields.
      boolean isDTO​(java.lang.Object o)
      An object with public non-static non-synthetic fields.
      <T> T shallowCopy​(T source)
      Shallow copy
      java.lang.String toString​(java.lang.Object dto)
      Convert a DTO to a human readable string presentation.
      java.lang.String unescape​(java.lang.String unescaped)
      Unescapes a string to be used in a path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private static final org.slf4j.Logger logger
      • EMPTY_FIELDS

        private final java.lang.reflect.Field[] EMPTY_FIELDS
      • cache

        private final java.util.Map<java.lang.Class<?>,​java.lang.reflect.Field[]> cache
      • ESCAPE_P

        static java.util.regex.Pattern ESCAPE_P
      • UNESCAPE_P

        static java.util.regex.Pattern UNESCAPE_P
    • Constructor Detail

      • DTOsImpl

        public DTOsImpl()
    • Method Detail

      • asMap

        public java.util.Map<java.lang.String,​java.lang.Object> asMap​(java.lang.Object dto)
        Description copied from interface: DTOs
        Return a partially read only Map object that maps directly to a DTO. I.e. changes are reflected in the DTO. If a field is a DTO, then this field will also become a Map.
        Specified by:
        asMap in interface DTOs
        Parameters:
        dto - the DTO
        Returns:
        a Map where the keys map to the field names and the values to the field values. This map is not modifiable.
      • getFields

        java.lang.reflect.Field[] getFields​(java.lang.Object o)
      • getFields

        java.lang.reflect.Field[] getFields​(java.lang.Class<?> c)
      • bsearch

        int bsearch​(java.lang.reflect.Field[] a,
                    int fromIndex,
                    int toIndex,
                    java.lang.String key)
      • getField

        java.lang.reflect.Field getField​(java.lang.reflect.Field[] fields,
                                         java.lang.String name)
      • shallowCopy

        public <T> T shallowCopy​(T source)
        Shallow copy
        Specified by:
        shallowCopy in interface DTOs
        Parameters:
        source - the source object
        Returns:
        a shallow copy of object
      • deepCopy

        public <T> T deepCopy​(T source)
        Deep copy
        Specified by:
        deepCopy in interface DTOs
        Parameters:
        source - the object to deep copy
        Returns:
        the deep copied object
      • fromPathToSegments

        public java.lang.String[] fromPathToSegments​(java.lang.String path)
        Description copied from interface: DTOs
        Takes a path with escaped '.'and '\' and then turns it into an array of unescaped keys
        Specified by:
        fromPathToSegments in interface DTOs
        Parameters:
        path - the path with escaped \ and .
        Returns:
        a path array with unescaped segments
      • fromPathToSegments

        java.lang.String[] fromPathToSegments​(java.lang.String path,
                                              int start,
                                              int n)
      • fromSegmentsToPath

        public java.lang.String fromSegmentsToPath​(java.lang.String[] segments)
        Description copied from interface: DTOs
        Takes a path with unescaped keys and turns it into a string path where the \ and . are escaped.
        Specified by:
        fromSegmentsToPath in interface DTOs
        Parameters:
        segments - The unescaped segments of the path
        Returns:
        a string path where the . and \ are escaped.
      • deepEquals

        public boolean deepEquals​(java.lang.Object a,
                                  java.lang.Object b)
        Description copied from interface: DTOs
        Check if two DTOs fields are equal. This is deep equal, that is the fields of this DTO are using this method is the object at a field is a DTO, recursively.
        Specified by:
        deepEquals in interface DTOs
        Parameters:
        a - the first object
        b - the second object
        Returns:
        true if both are null or the DTO's primary fields are equal
      • toString

        public java.lang.String toString​(java.lang.Object dto)
        Description copied from interface: DTOs
        Convert a DTO to a human readable string presentation. This is primarily for debugging since the toString can truncate fields. This method must print all public fields, also non primary. Output formats can vary (e.g. YAML like) so the actual output should NOT be treated as standard.
        Specified by:
        toString in interface DTOs
        Parameters:
        dto - the dto to turn into a string
        Returns:
        a human readable string (not json!)
      • equals

        public boolean equals​(java.lang.Object a,
                              java.lang.Object b)
        Description copied from interface: DTOs
        Check if two dtos fields are equal. This is shallow equal, that is the fields of this DTO are using the equals() instance method.
        Specified by:
        equals in interface DTOs
        Parameters:
        a - the first object
        b - the second object
        Returns:
        true if both are null or the DTO's primary fields are equal
      • hashCode

        public int hashCode​(java.lang.Object dto)
        Description copied from interface: DTOs
        Calculate a hash Code for the fields in this DTO. The dto must have at least one public field.
        Specified by:
        hashCode in interface DTOs
        Parameters:
        dto - the object to calculate the hashcode for, must not be null .
        Returns:
        a hashcode
      • get

        public java.util.Optional<java.lang.Object> get​(java.lang.Object dto,
                                                        java.lang.String path)
        Description copied from interface: DTOs
        Access a DTO with a path. A path is a '.' separated string. Each part in the path is either a field name, key in a map, or an index in a list. If the path segments contain dots or backslashes, then these must be escaped
        Specified by:
        get in interface DTOs
        Parameters:
        dto - the root
        path - the path, should only contain dots as separators
        Returns:
        the value of the object or empty if not found.
      • get

        public java.util.Optional<java.lang.Object> get​(java.lang.Object dto,
                                                        java.lang.String... path)
        Description copied from interface: DTOs
        Access a DTO with a path that consists of an array with segments. Each segment in the path is either a field name, key in a map, or an index in a list.
        Specified by:
        get in interface DTOs
        Parameters:
        dto - the root
        path - the path
        Returns:
        the value of the object or empty if not found.
      • get

        private java.util.Optional<java.lang.Object> get​(java.lang.Object dto,
                                                         java.lang.String[] path,
                                                         int i,
                                                         int max)
      • diff

        public java.util.List<DTOs.Difference> diff​(java.lang.Object older,
                                                    java.lang.Object newer)
        Description copied from interface: DTOs
        Return a list of paths where the two objects differ. The objects must be of the same class.
        Specified by:
        diff in interface DTOs
        Parameters:
        older - the older object
        newer - the newer object
        Returns:
        A list of differences, if there is no difference, the list is empty.
      • diff

        private boolean diff​(java.util.List<DTOs.Difference> diffs,
                             DTOsImpl.Link link,
                             java.lang.Object older,
                             java.lang.Object newer)
      • escape

        public java.lang.String escape​(java.lang.String unescaped)
        Description copied from interface: DTOs
        Escape a string to be used in a path. This will put a backslash ('\') in front of full stops ('.') and the backslash ('\').
        Specified by:
        escape in interface DTOs
        Parameters:
        unescaped - the string to be escaped
        Returns:
        a string where all '.' and '\' are escaped with a '\'.
      • unescape

        public java.lang.String unescape​(java.lang.String unescaped)
        Description copied from interface: DTOs
        Unescapes a string to be used in a path. This will remove a backslash ('\') in front of full stops ('.') and the backslash ('\').
        Specified by:
        unescape in interface DTOs
        Parameters:
        unescaped - the string to be unescaped
        Returns:
        a string where all '\.' and '\\' have the preceding backslash removed with a '\'.
      • isComplex

        public boolean isComplex​(java.lang.Object a)
        Description copied from interface: DTOs
        Return true if the give dto is complex (either Map, Collection, Array, or has public fields.
        Specified by:
        isComplex in interface DTOs
        Parameters:
        a - The DTO to check
        Returns:
        true if this is a DTO with fields or length.
      • isDTO

        public boolean isDTO​(java.lang.Object o)
        Description copied from interface: DTOs
        An object with public non-static non-synthetic fields.
        Specified by:
        isDTO in interface DTOs
        Parameters:
        o - the object to check
        Returns:
        true if this object has public fields or extends DTO