Class FlexiBean
- java.lang.Object
-
- org.joda.beans.impl.flexi.FlexiBean
-
- All Implemented Interfaces:
Serializable,Bean,DynamicBean
public final class FlexiBean extends Object implements DynamicBean, Serializable
Implementation of a fully dynamicBean.Properties are dynamic, and can be added and removed at will from the map. The internal storage is created lazily to allow a flexi-bean to be used as a lightweight extension to another bean.
Each flexi-bean has a different set of properties. As such, there is one instance of meta-bean for each flexi-bean.
The keys of a flexi-bean must be simple identifiers as per '[a-zA-Z_][a-zA-Z0-9_]*'.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FlexiBeanappend(String propertyName, Object newValue)Sets a property in this bean to the specified value.voidclear()Removes all properties.FlexiBeanclone()Clones this bean, returning an independent copy.booleancontains(String propertyName)Checks if the bean contains a specific property.booleanequals(Object obj)Compares this bean to another based on the property names and content.Objectget(String propertyName)Gets the value of the property.<T> Tget(String propertyName, Class<T> type)Gets the value of the property cast to a specific type.booleangetBoolean(String propertyName)Gets the value of the property as aboolean.doublegetDouble(String propertyName)Gets the value of the property as adouble.doublegetDouble(String propertyName, double defaultValue)Gets the value of the property as adoubleusing a default value.intgetInt(String propertyName)Gets the value of the property as aint.intgetInt(String propertyName, int defaultValue)Gets the value of the property as aintusing a default value.longgetLong(String propertyName)Gets the value of the property as along.longgetLong(String propertyName, long defaultValue)Gets the value of the property as alongusing a default value.StringgetString(String propertyName)Gets the value of the property as aString.inthashCode()Returns a suitable hash code.static DynamicMetaBeanmeta()Creates a standalone meta-bean.DynamicMetaBeanmetaBean()Gets the meta-bean representing the parts of the bean that are common across all instances, such as the set of meta-properties.Property<Object>property(String name)Gets a property by name.voidpropertyDefine(String propertyName, Class<?> propertyType)Adds a property to those allowed to be stored in the bean.booleanpropertyExists(String propertyName)Checks if the property exists.ObjectpropertyGet(String propertyName)Gets the value of the property.Set<String>propertyNames()Gets the set of property names.voidpropertyRemove(String propertyName)Removes a property by name.voidpropertySet(String propertyName, Object newValue)Sets the value of the property.Objectput(String propertyName, Object newValue)Sets a property in this bean to the specified value.voidputAll(Map<String,? extends Object> map)Puts the properties in the specified map into this bean.voidputAll(FlexiBean other)Puts the properties in the specified bean into this bean.voidremove(String propertyName)Removes a property.voidset(String propertyName, Object newValue)Sets a property in this bean to the specified value.intsize()Gets the number of properties.Map<String,Object>toMap()Returns a map representing the contents of the bean.StringtoString()Returns a string that summarises the bean.
-
-
-
Constructor Detail
-
FlexiBean
public FlexiBean()
Constructor.
-
FlexiBean
public FlexiBean(FlexiBean copyFrom)
Constructor that copies all the data entries from the specified bean.- Parameters:
copyFrom- the bean to copy from, not null
-
-
Method Detail
-
meta
public static DynamicMetaBean meta()
Creates a standalone meta-bean.This creates a new instance each time in line with dynamic bean principles.
- Returns:
- the meta-bean, not null
-
size
public int size()
Gets the number of properties.- Returns:
- the number of properties
-
contains
public boolean contains(String propertyName)
Checks if the bean contains a specific property.- Parameters:
propertyName- the property name, null returns false- Returns:
- true if the bean contains the property
-
get
public Object get(String propertyName)
Gets the value of the property.This returns null if the property does not exist.
- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property, may be null
-
get
public <T> T get(String propertyName, Class<T> type)
Gets the value of the property cast to a specific type.This returns null if the property does not exist.
- Type Parameters:
T- the value type- Parameters:
propertyName- the property name, not emptytype- the type to cast to, not null- Returns:
- the value of the property, may be null
- Throws:
ClassCastException- if the type is incorrect
-
getString
public String getString(String propertyName)
Gets the value of the property as aString. This will useObject.toString().This returns null if the property does not exist.
- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property, may be null
-
getBoolean
public boolean getBoolean(String propertyName)
Gets the value of the property as aboolean.- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatibleNullPointerException- if the property does not exist or is null
-
getInt
public int getInt(String propertyName)
Gets the value of the property as aint.- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatibleNullPointerException- if the property does not exist or is null
-
getInt
public int getInt(String propertyName, int defaultValue)
Gets the value of the property as aintusing a default value.- Parameters:
propertyName- the property name, not emptydefaultValue- the default value for null or invalid property- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatible
-
getLong
public long getLong(String propertyName)
Gets the value of the property as along.- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatibleNullPointerException- if the property does not exist or is null
-
getLong
public long getLong(String propertyName, long defaultValue)
Gets the value of the property as alongusing a default value.- Parameters:
propertyName- the property name, not emptydefaultValue- the default value for null or invalid property- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatible
-
getDouble
public double getDouble(String propertyName)
Gets the value of the property as adouble.- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatibleNullPointerException- if the property does not exist or is null
-
getDouble
public double getDouble(String propertyName, double defaultValue)
Gets the value of the property as adoubleusing a default value.- Parameters:
propertyName- the property name, not emptydefaultValue- the default value for null or invalid property- Returns:
- the value of the property
- Throws:
ClassCastException- if the value is not compatible
-
append
public FlexiBean append(String propertyName, Object newValue)
Sets a property in this bean to the specified value.This creates a property if one does not exist.
- Parameters:
propertyName- the property name, not emptynewValue- the new value, may be null- Returns:
thisfor chaining, not null
-
set
public void set(String propertyName, Object newValue)
Sets a property in this bean to the specified value.This creates a property if one does not exist.
- Parameters:
propertyName- the property name, not emptynewValue- the new value, may be null
-
put
public Object put(String propertyName, Object newValue)
Sets a property in this bean to the specified value.This creates a property if one does not exist.
- Parameters:
propertyName- the property name, not emptynewValue- the new value, may be null- Returns:
- the old value of the property, may be null
-
putAll
public void putAll(Map<String,? extends Object> map)
Puts the properties in the specified map into this bean.This creates properties if they do not exist.
- Parameters:
map- the map of properties to add, not null
-
putAll
public void putAll(FlexiBean other)
Puts the properties in the specified bean into this bean.This creates properties if they do not exist.
- Parameters:
other- the map of properties to add, not null
-
remove
public void remove(String propertyName)
Removes a property.No error occurs if the property does not exist.
- Parameters:
propertyName- the property name, not empty
-
clear
public void clear()
Removes all properties.
-
propertyExists
public boolean propertyExists(String propertyName)
Checks if the property exists.- Parameters:
propertyName- the property name, not empty- Returns:
- true if the property exists
-
propertyGet
public Object propertyGet(String propertyName)
Gets the value of the property.- Parameters:
propertyName- the property name, not empty- Returns:
- the value of the property, may be null
-
propertySet
public void propertySet(String propertyName, Object newValue)
Sets the value of the property.- Parameters:
propertyName- the property name, not emptynewValue- the new value of the property, may be null
-
metaBean
public DynamicMetaBean metaBean()
Description copied from interface:DynamicBeanGets the meta-bean representing the parts of the bean that are common across all instances, such as the set of meta-properties.- Specified by:
metaBeanin interfaceBean- Specified by:
metaBeanin interfaceDynamicBean- Returns:
- the meta-bean, not null
-
property
public Property<Object> property(String name)
Description copied from interface:DynamicBeanGets a property by name.This will not throw an exception if the property name does not exist. Whether a property is immediately created or not is implementation dependent.
- Specified by:
propertyin interfaceBean- Specified by:
propertyin interfaceDynamicBean- Parameters:
name- the property name to retrieve, not null- Returns:
- the property, not null
-
propertyNames
public Set<String> propertyNames()
Description copied from interface:BeanGets the set of property names.Each bean consists of a known set of properties. This method returns the known property names.
- Specified by:
propertyNamesin interfaceBean- Returns:
- the unmodifiable set of property names, not null
-
propertyDefine
public void propertyDefine(String propertyName, Class<?> propertyType)
Description copied from interface:DynamicBeanAdds a property to those allowed to be stored in the bean.Some implementations will automatically add properties, in which case this method will have no effect.
- Specified by:
propertyDefinein interfaceDynamicBean- Parameters:
propertyName- the property name to check, not empty, not nullpropertyType- the property type, not null
-
propertyRemove
public void propertyRemove(String propertyName)
Description copied from interface:DynamicBeanRemoves a property by name.- Specified by:
propertyRemovein interfaceDynamicBean- Parameters:
propertyName- the property name to remove, null ignored
-
toMap
public Map<String,Object> toMap()
Returns a map representing the contents of the bean.- Returns:
- a map representing the contents of the bean, not null
-
clone
public FlexiBean clone()
Clones this bean, returning an independent copy.
-
equals
public boolean equals(Object obj)
Compares this bean to another based on the property names and content.
-
hashCode
public int hashCode()
Returns a suitable hash code.
-
-