public class ObjectWrapper extends Object
This class is highly optimized for preventing internal objects instantiation, and at the same time guarantees near-zero garbage collection. In addition to this, in a not-concurrent context, is perfectly safe to change the wrapped object and the internal options at any time, without side effects.
Various types of public properties are supported:
- Simple properties are any property of any type (both Object
than primitive). Methods which supports a string pattern allows to traverse
the properties' tree by separating the propety name with a '.' dot notation.
For example "foo.bar.zoo" is parsed to object.getFoo().getBar().getZoo();
- Indexed properties are List, finite Iterable
(any Collection), and any type of array. They usually
preserve their addition order (although it is not guaranteed for Iterable),
and are accessible through an integer index. Methods which supports a string
pattern allows to specify the requested integer index by a '[i]' notation.
- For example "myList[1]" is parsed to object.getMyList().get(1);
- For example "myArray[5]" is parsed to object.getMyArray()[5];
- Mapped properties are Map type and are accessible through an
object key. Methods which supports the a string pattern allows to specify the
requested key by a '[key]' notation. The key string can contains dots.
For example "myMap[my.key]" is parsed to object.getMyMap().get("my.key");
Any pattern combination is allowed, for example "foo.myList[1].bar.myMap[test].myArray[5]" is parsed to object.getFoo().getMyList().get(1).getBar().getMyMap().get("test").getMyArray()[5].
Bean,
Property| Constructor and Description |
|---|
ObjectWrapper(Object obj)
Wraps an object.
|
| Modifier and Type | Method and Description |
|---|---|
org.minimalcode.reflect.Bean<?> |
getBean()
Returns the
Bean cached instance of the wrapped object. |
Object |
getIndexedValue(org.minimalcode.reflect.Property property,
int index)
Returns the value of the specified indexed property from the wrapped object.
|
Object |
getIndexedValue(String propertyName,
int index)
Returns the value of the specified indexed property from the wrapped object.
|
Object |
getMappedValue(org.minimalcode.reflect.Property property,
Object key)
Returns the value of the specified mapped property from the wrapped object.
|
Object |
getMappedValue(String propertyName,
Object key)
Returns the value of the specified mapped property from the wrapped object.
|
static org.minimalcode.reflect.Property |
getProperty(org.minimalcode.reflect.Bean bean,
String propertyPattern) |
org.minimalcode.reflect.Property |
getProperty(String propertyPattern)
Returns a single public
Property of the wrapped object, if present, else null. |
Object |
getSimpleValue(org.minimalcode.reflect.Property property)
Returns the value of the specified simple property from the wrapped object.
|
Object |
getSimpleValue(String propertyName)
Returns the value of the specified simple property from the wrapped object.
|
Object |
getValue(String propertyPattern)
Returns the value of the specified simple, indexed or mapped property locate by the given pattern.
|
void |
setAutoGrowing(boolean isAutoGrowing)
Changes the status of the Auto-Growing option.
|
void |
setAutoInstancing(boolean isAutoInstancing)
Changes the status of the Auto-Instancing option.
|
void |
setIndexedValue(org.minimalcode.reflect.Property property,
int index,
Object value)
Sets the value of the specified indexed property in the wrapped object.
|
void |
setIndexedValue(String propertyName,
int index,
Object value)
Sets the value of the specified indexed property in the wrapped object.
|
void |
setMappedValue(org.minimalcode.reflect.Property property,
Object key,
Object value)
Sets the value of the specified mapped property in the wrapped object.
|
void |
setMappedValue(String propertyName,
Object key,
Object value)
Sets the value of the specified mapped property in the wrapped object.
|
void |
setOutOfBoundsSafety(boolean isOutOfBoundsSafety)
Changes the status of the OutOfBounds-Safety option.
|
void |
setSimpleValue(org.minimalcode.reflect.Property property,
Object value)
Sets the value of the specified property in the wrapped object.
|
void |
setSimpleValue(String propertyName,
Object value)
Sets the value of the specified property in the wrapped object.
|
void |
setValue(String propertyPattern,
Object value)
Sets the value of the specified simple, indexed or mapped property locate
by the given pattern, in the wrapped object.
|
void |
setWrappedObject(Object obj)
Changes, at any time, the wrapped object with a new object.
|
String |
toString()
Returns a textual rapresentation of this wrapper.
|
public ObjectWrapper(Object obj)
obj - the object to wrapIllegalArgumentException - if the obj parameter is nullpublic void setWrappedObject(Object obj)
obj - the new object to wrapIllegalArgumentException - if the obj parameter is nullpublic org.minimalcode.reflect.Bean<?> getBean()
Bean cached instance of the wrapped object.public void setAutoGrowing(boolean isAutoGrowing)
Default: enabled. If enabled, automatically grows-up any unbound
List or array to accomodate an element in an index
position larger than its capacity.
Affected:
isAutoGrowing - the new isAutoGrowing flag option valuepublic void setAutoInstancing(boolean isAutoInstancing)
Default: enabled. If enabled, automatically instances a new object
(a no-args constructor is required) or a new List, Map
or array, if the nested Property has value null
in the wrapped object.
Affected:
- setIndexedValue(String, int, Object)
- setIndexedValue(Property, int, Object)
isAutoInstancing - the new isAutoInstancing option flag valuepublic void setOutOfBoundsSafety(boolean isOutOfBoundsSafety)
Default: enabled. If enabled, always returns a null value
instead of throwing an IndexOutOfBoundsException when trying
to access to an item in an unbound position of a List,
array or any type of finite Iterable, behaving like a
Map.get(Object) implementation.
Affected:
isOutOfBoundsSafety - the new isOutOfBoundsSafety flag option valuepublic org.minimalcode.reflect.Property getProperty(String propertyPattern)
Property of the wrapped object, if present, else null.
Differently from Bean.getProperty(String), this method supports the nested
pattern notation, allowing to statically traverse the properties' tree (ex: "foo.bar.property").
propertyPattern - the nested pattern to traverse, cannot be nullnull if not foundIllegalArgumentException - if the propertyPattern parameter is nullIllegalArgumentException - if the propertyPattern parameter contains an indexed\mapped '[]' notationpublic Object getValue(String propertyPattern)
propertyPattern - the pattern to traverse, cannot be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyPattern parameter is nullIllegalArgumentException - if the propertyPattern parameter contains an unclosed indexed\mapped '[]' notationIllegalArgumentException - if the propertyPattern parameter contains an invalid, not parsable, index integerNullPointerException - if the propertyPattern parameter contains a nested property with a null valueNullPointerException - if the propertyPattern parameter contains a nested not-existent propertyIndexOutOfBoundsException - if the specified index is outside the valid range for the underlying propertypublic Object getSimpleValue(String propertyName)
propertyName - the name of the property whose value is to be extracted, cannot be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyName parameter is nullNullPointerException - if the wrapped object does not have a property with the given namepublic Object getSimpleValue(org.minimalcode.reflect.Property property)
property - the property whose value is to be extracted, cannot be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the property parameter is nullpublic Object getIndexedValue(String propertyName, int index)
propertyName - the name of the indexed property whose value is to be extracted, cannot be nullindex - the index of the property value to be extractedorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyName parameter is nullIllegalArgumentException - if the indexed object in the wrapped object is not a List, Iterable or arrayNullPointerException - if the indexed List, Iterable or array is null in the given objectNullPointerException - if the wrapped object does not have a property with the given nameIndexOutOfBoundsException - if the specified index is outside the valid range for the underlying indexed propertypublic Object getIndexedValue(org.minimalcode.reflect.Property property, int index)
property - the indexed property whose value is to be extracted, cannot be nullindex - the index of the property value to be extractedorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the property parameter is nullIllegalArgumentException - if the indexed object in the wrapped object is not a List, Iterable or arrayNullPointerException - if the indexed List, Iterable or array is null in the wrapped objectIndexOutOfBoundsException - if the specified index is outside the valid range for the underlying indexed propertypublic Object getMappedValue(String propertyName, Object key)
propertyName - the name of the mapped property whose value is to be extracted, cannot be nullkey - the key of the property value to be extracted, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyName parameter is nullIllegalArgumentException - if the mapped object in the wrapped object is not a Map typeNullPointerException - if the mapped object in the wrapped object is nullNullPointerException - if the wrapped object does not have a property with the given namepublic Object getMappedValue(org.minimalcode.reflect.Property property, Object key)
property - the mapped property whose value is to be extracted, cannot be nullkey - the key of the property value to be extracted, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the property parameter is nullIllegalArgumentException - if the mapped object in the wrapped object is not a Map typeNullPointerException - if the mapped object in the wrapped object is nullpublic void setValue(String propertyPattern, Object value)
propertyPattern - the pattern to traverse, cannot be nullvalue - the value to set, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyPattern parameter is nullIllegalArgumentException - if the propertyPattern parameter contains an unclosed indexed\mapped '[]' notationIllegalArgumentException - if the propertyPattern parameter contains an invalid, not parsable, index integerNullPointerException - if the propertyPattern parameter contains a nested property with a null valueNullPointerException - if the propertyPattern parameter contains a nested not-existent propertyIndexOutOfBoundsException - if the specified index is outside the valid range for the underlying propertypublic void setSimpleValue(String propertyName, Object value)
propertyName - the name of the simple property whose value is to be updated, cannot be nullvalue - the value to set, can be nullIllegalArgumentException - if the property parameter is nullNullPointerException - if the wrapped object does not have a property with the given namepublic void setSimpleValue(org.minimalcode.reflect.Property property,
Object value)
property - the property whose value is to be updated, cannot be nullvalue - the value to set, can be nullIllegalArgumentException - if the property parameter is nullpublic void setIndexedValue(String propertyName, int index, Object value)
propertyName - the name of the indexed property whose value is to be updated, cannot be nullindex - the index position of the property value to be setvalue - the indexed value to set, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyName parameter is nullIllegalArgumentException - if the indexed object in the wrapped object is not a List or array typeIndexOutOfBoundsException - if the indexed object in the wrapped object is out of bounds with the given index and autogrowing is disabledNullPointerException - if the indexed object in the wrapped object is nullNullPointerException - if the indexed object in the wrapped object is out of bounds with the given index, but autogrowing (if enabled) is unable to fill the blank positions with nullNullPointerException - if the wrapped object does not have a property with the given namepublic void setIndexedValue(org.minimalcode.reflect.Property property,
int index,
Object value)
property - the indexed property whose value is to be updated, cannot be nullindex - the index position of the property value to be setvalue - the indexed value to set, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the property parameter is nullIllegalArgumentException - if the indexed object in the wrapped object is not a List or array typeIndexOutOfBoundsException - if the indexed object in the wrapped object is out of bounds with the given index and autogrowing is disabledNullPointerException - if the indexed object in the wrapped object is nullNullPointerException - if the indexed object in the wrapped object is out of bounds with the given index, but autogrowing (if enabled) is unable to fill the blank positions with nullpublic void setMappedValue(String propertyName, Object key, Object value)
propertyName - the name of the mapped property whose value is to be extracted, cannot be nullkey - the mapped key of the property value to be setvalue - the property value to set, can be nullorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the propertyName parameter is nullIllegalArgumentException - if the mapped object in the wrapped object is not a Map typeNullPointerException - if the mapped object in the wrapped object is nullNullPointerException - if the wrapped object does not have a property with the given namepublic void setMappedValue(org.minimalcode.reflect.Property property,
Object key,
Object value)
property - the mapped property whose value is to be updated, cannot be nullkey - the mapped key of the property value to be setvalue - the property value to setorg.minimalcode.reflect.ReflectionException - if a reflection error occursIllegalArgumentException - if the property parameter is nullIllegalArgumentException - if the mapped object in the wrapped object is not a MapNullPointerException - if the mapped object in the wrapped object is nullpublic String toString()
public static org.minimalcode.reflect.Property getProperty(org.minimalcode.reflect.Bean bean,
String propertyPattern)
Copyright © 2015 Minimalcode. All Rights Reserved.