Class DTOsImpl

java.lang.Object
aQute.libg.dtos.DTOsImpl
All Implemented Interfaces:
DTOs

public class DTOsImpl extends Object implements DTOs
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • EMPTY_FIELDS

      private final Field[] EMPTY_FIELDS
    • cache

      private final Map<Class<?>,Field[]> cache
    • root

      private final DTOsImpl.Link root
    • ESCAPE_P

      static Pattern ESCAPE_P
    • UNESCAPE_P

      static Pattern UNESCAPE_P
  • Constructor Details

    • DTOsImpl

      public DTOsImpl()
  • Method Details

    • asMap

      public Map<String,Object> asMap(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

      Field[] getFields(Object o)
    • getFields

      Field[] getFields(Class<?> c)
    • bsearch

      int bsearch(Field[] a, int fromIndex, int toIndex, String key)
    • getField

      Field getField(Field[] fields, 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
    • deepCopy

      <T> T deepCopy(T source, DTOsImpl.Link link)
    • fromPathToSegments

      public String[] fromPathToSegments(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

      String[] fromPathToSegments(String path, int start, int n)
    • fromSegmentsToPath

      public String fromSegmentsToPath(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(Object a, 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 String toString(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(Object a, 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(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 Optional<Object> get(Object dto, 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 Optional<Object> get(Object dto, 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 Optional<Object> get(Object dto, String[] path, int i, int max)
    • diff

      public List<DTOs.Difference> diff(Object older, 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(List<DTOs.Difference> diffs, DTOsImpl.Link link, Object older, Object newer)
    • escape

      public String escape(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 String unescape(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(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(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