Package org.httprpc.beans
Class BeanAdapter
- java.lang.Object
-
- java.util.AbstractMap<java.lang.String,java.lang.Object>
-
- org.httprpc.beans.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>Mapadapter for Java bean types.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBeanAdapter.PropertyClass representing a bean property.
-
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> Tadapt(java.lang.Object value)Adapts a value for loosely typed access.static <T> Tadapt(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.Objectget(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.Objectput(java.lang.String key, java.lang.Object value)static <T> java.lang.reflect.ParameterizedTypetypeOf(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
-
-
-
-
Method Detail
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map<java.lang.String,java.lang.Object>- Overrides:
getin classjava.util.AbstractMap<java.lang.String,java.lang.Object>
-
put
public java.lang.Object put(java.lang.String key, java.lang.Object value)- Specified by:
putin interfacejava.util.Map<java.lang.String,java.lang.Object>- Overrides:
putin classjava.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:
entrySetin interfacejava.util.Map<java.lang.String,java.lang.Object>- Specified by:
entrySetin classjava.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 isnullor an instance of one of the following types, it is returned as is:CharSequenceNumberBooleanEnumDateTemporalAccessorTemporalAmountUUIDURL
Iterable, it is wrapped in an adapter that will recursively adapt the iterable's elements. If the value is aMap, 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 aBeanAdapter.- 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
nullvalues are automatically converted to0orfalsefor primitive argument types. - If the target type is
String, the value is adapted viaObject.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 toDate(long). - If the target type is
Instantand the value is an instance ofDate, the value is adapted viaDate.toInstant(). Otherwise, the value's string representation is parsed usingInstant.parse(CharSequence). - If the target type is
LocalDate, the value's string representation is parsed usingLocalDate.parse(CharSequence). - If the target type is
LocalTime, the value's string representation is parsed usingLocalTime.parse(CharSequence). - If the target type is
LocalDateTime, the value's string representation is parsed usingLocalDateTime.parse(CharSequence). - If the target type is
Duration, the value's string representation is parsed usingDuration.parse(CharSequence). - If the target type is
Period, the value's string representation is parsed usingPeriod.parse(CharSequence). - If the target type is
UUID, the value's string representation is adapted viaUUID.fromString(String). - If the target type is
URL, the value's string representation is adapted viaURL(String). - If the target type is
ListorMap, the value is wrapped in an adapter of the same type that automatically adapts its elements.
- 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.
-
-