Class BeanAdapter

  • All Implemented Interfaces:
    java.util.Map<java.lang.String,​java.lang.Object>

    public class BeanAdapter
    extends java.util.AbstractMap<java.lang.String,​java.lang.Object>
    Map adapter for Java bean types.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  BeanAdapter.Property
      Class representing a bean property.
      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      BeanAdapter​(java.lang.Object bean)
      Constructs a new bean adapter.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T adapt​(java.lang.Object value)
      Adapts a value for loosely typed access.
      static <T> T adapt​(java.lang.Object value, java.lang.reflect.Type type)
      Adapts a value for strongly typed access.
      static <E> java.util.List<E> adaptList​(java.util.List<?> list, java.lang.Class<? extends E> elementType)
      Adapts a list instance for typed access.
      static <K,​V>
      java.util.Map<K,​V>
      adaptMap​(java.util.Map<K,​?> map, java.lang.Class<? extends V> valueType)
      Adapts a map instance for typed access.
      java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> entrySet()  
      java.lang.Object get​(java.lang.Object key)  
      static java.util.Map<java.lang.String,​BeanAdapter.Property> getProperties​(java.lang.Class<?> type)
      Returns the properties for a given type.
      java.lang.Object put​(java.lang.String key, java.lang.Object value)  
      static <T> java.lang.reflect.ParameterizedType typeOf​(java.lang.Class<T> rawType, java.lang.reflect.Type... actualTypeArguments)
      Generates a parameterized type descriptor.
      • Methods inherited from class java.util.AbstractMap

        clear, clone, containsKey, containsValue, equals, hashCode, isEmpty, keySet, putAll, remove, size, toString, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • BeanAdapter

        public BeanAdapter​(java.lang.Object bean)
        Constructs a new bean adapter.
        Parameters:
        bean - The source bean.
    • Method Detail

      • get

        public java.lang.Object get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<java.lang.String,​java.lang.Object>
        Overrides:
        get in class java.util.AbstractMap<java.lang.String,​java.lang.Object>
      • put

        public java.lang.Object put​(java.lang.String key,
                                    java.lang.Object value)
        Specified by:
        put in interface java.util.Map<java.lang.String,​java.lang.Object>
        Overrides:
        put in class java.util.AbstractMap<java.lang.String,​java.lang.Object>
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,​java.lang.Object>
        Specified by:
        entrySet in class java.util.AbstractMap<java.lang.String,​java.lang.Object>
      • adapt

        public static <T> T adapt​(java.lang.Object value)
        Adapts a value for loosely typed access. If the value is null or an instance of one of the following types, it is returned as is:
        • CharSequence
        • Number
        • Boolean
        • Enum
        • Date
        • TemporalAccessor
        • TemporalAmount
        • UUID
        • URL
        If the value is an Iterable, it is wrapped in an adapter that will recursively adapt the iterable's elements. If the value is a Map, it is wrapped in an adapter that will recursively adapt the map's values. Otherwise, the value is assumed to be a bean and is wrapped in a BeanAdapter.
        Type Parameters:
        T - The target type.
        Parameters:
        value - The value to adapt.
        Returns:
        The adapted value.
      • adapt

        public static <T> T adapt​(java.lang.Object value,
                                  java.lang.reflect.Type type)
        Adapts a value for strongly typed access.
        • If the value is already an instance of the given type, it is returned as is.
        • If the target type is a number or boolean, the value is parsed or coerced using the appropriate conversion method. Missing or null values are automatically converted to 0 or false for primitive argument types.
        • If the target type is String, the value is adapted via Object.toString().
        • If the target type is an Enum, the adapted value is the first constant whose string representation matches the value's string representation.
        • If the target type is Date, the value is coerced to a long value and passed to Date(long).
        • If the target type is Instant and the value is an instance of Date, the value is adapted via Date.toInstant(). Otherwise, the value's string representation is parsed using Instant.parse(CharSequence).
        • If the target type is LocalDate, the value's string representation is parsed using LocalDate.parse(CharSequence).
        • If the target type is LocalTime, the value's string representation is parsed using LocalTime.parse(CharSequence).
        • If the target type is LocalDateTime, the value's string representation is parsed using LocalDateTime.parse(CharSequence).
        • If the target type is Duration, the value's string representation is parsed using Duration.parse(CharSequence).
        • If the target type is Period, the value's string representation is parsed using Period.parse(CharSequence).
        • If the target type is UUID, the value's string representation is adapted via UUID.fromString(String).
        • If the target type is URL, the value's string representation is adapted via URL(String).
        • If the target type is List or Map, the value is wrapped in an adapter of the same type that automatically adapts its elements.
        Otherwise, the value is assumed to be a map and is adapted as follows:
        • If the target type is an interface, the return value is a proxy implementation of the interface that maps accessor methods to entries in the map. `Object` methods are delegated to the underlying map.
        • If the target type is a concrete class, an instance of the type is dynamically created and populated using the entries in the map.
        Type Parameters:
        T - The target type.
        Parameters:
        value - The value to adapt.
        type - The target type.
        Returns:
        The adapted value.
      • adaptList

        public static <E> java.util.List<E> adaptList​(java.util.List<?> list,
                                                      java.lang.Class<? extends E> elementType)
        Adapts a list instance for typed access.
        Type Parameters:
        E - The target element type.
        Parameters:
        list - The list to adapt.
        elementType - The target element type.
        Returns:
        An list implementation that will adapt the list's elements as documented for adapt(Object, Type).
      • adaptMap

        public static <K,​V> java.util.Map<K,​V> adaptMap​(java.util.Map<K,​?> map,
                                                                    java.lang.Class<? extends V> valueType)
        Adapts a map instance for typed access.
        Type Parameters:
        K - The key type.
        V - The target value type.
        Parameters:
        map - The map to adapt.
        valueType - The target value type.
        Returns:
        An map implementation that will adapt the map's values as documented for adapt(Object, Type).
      • typeOf

        public static <T> java.lang.reflect.ParameterizedType typeOf​(java.lang.Class<T> rawType,
                                                                     java.lang.reflect.Type... actualTypeArguments)
        Generates a parameterized type descriptor.
        Type Parameters:
        T - The raw type.
        Parameters:
        rawType - The raw type.
        actualTypeArguments - The actual type arguments.
        Returns:
        A parameterized type that describes the given raw type and actual type arguments.
      • getProperties

        public static java.util.Map<java.lang.String,​BeanAdapter.Property> getProperties​(java.lang.Class<?> type)
        Returns the properties for a given type.
        Parameters:
        type - The bean type.
        Returns:
        The properties defined by the given type.