-
- All Superinterfaces:
MarshallableObject
,Marshaller<Object>
,Validatable
- All Known Subinterfaces:
EntityBean
,VirtualBean
,WritableBean
- All Known Implementing Classes:
AbstractBean
,AbstractVirtualBean
,AdvancedBean
,Bean
,DynamicBean
,SimpleEntityBean
public interface ReadableBean extends Validatable, MarshallableObject
Read interface of aBean
holding arbitraryproperties
. Unlike plain old Java Beans this offers a lot of advanced features:- Simple - no need to write boiler-plate code for implementation such as getters, setters, equals, or hashCode.
- Generic - fast, easy and reliable introspection via
iteration of all properties
. No more greedy and slow reflection at runtime (after bootstrapping). - Dynamic - supports combination of Java's strong typing with
dynamic
beans. E.g. if read data from Database, XML, or JSON you can still map "undefined" properties in yourBean
. This way a client can receive an object from a newer version of a database or service with added properties that will be kept in the object and send back when theBean
is written back. - ReadOnly-Support - create a
read-only
copy
of your object to pass by reference without side-effects. - Powerful -
WritableProperty
supports listeners and bindings as well asgeneric type information
. - Validation - build-in
validation support
. - Marshalling - build-in support to
read
andwrite
theBean
from/to JSON, XML, or other formats. Implement custom datatypes asproperty
and you will not need separate classes or configurations for mapping. - Portable - everything relies only on established Java standard mechanisms. No customization of build processes, IDEs, etc. needed. It just works with any build tool (maven, gradle, buildr, ant, etc.) and IDE (Eclipse, IntelliJ, NetBeans, etc.) without plugins and therefore will also work in the future whatever may come.
-
-
Field Summary
Fields Modifier and Type Field Description static String
PROPERTY_TYPE
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default WritableBean
copy()
WritableBean
copy(boolean readOnly)
static <B extends ReadableBean>
Bcopy(B bean, boolean readOnly)
static <B extends ReadableBean>
BcopyReadOnly(B bean)
default Object
get(String name)
Iterable<? extends ReadableProperty<?>>
getProperties()
ReadableProperty<?>
getProperty(String name)
int
getPropertyCount()
default String
getPropertyNameForAlias(String alias)
default ReadableProperty<?>
getRequiredProperty(String name)
BeanType
getType()
boolean
isDynamic()
default boolean
isEqualTo(ReadableBean other)
ABean
implementation shall not overrideObject.equals(Object)
andObject.hashCode()
for efficient usage inCollection
s andMap
s.default boolean
isPolymorphic()
ABean
may be polymorphic to allow mappings to and from other representations without knowing the exact type.boolean
isPrototype()
boolean
isReadOnly()
WritableBean
newInstance()
static <B extends ReadableBean>
BnewInstance(B bean)
default ValidationResult
validate()
This method performs the actual validation.default void
write(StructuredWriter writer)
-
Methods inherited from interface io.github.mmm.marshall.MarshallableObject
writeObject
-
-
-
-
Field Detail
-
PROPERTY_TYPE
static final String PROPERTY_TYPE
-
-
Method Detail
-
getProperty
ReadableProperty<?> getProperty(String name)
- Parameters:
name
- thename
of the requested property or a potentialalias
of the property.- Returns:
- the requested
WritableProperty
ornull
if no such property exists. - See Also:
WritableBean.addProperty(WritableProperty)
,WritableBean.getOrCreateProperty(String, Class)
-
getProperties
Iterable<? extends ReadableProperty<?>> getProperties()
- Returns:
- an
Iterable
of all properties of this bean.
-
getPropertyCount
int getPropertyCount()
- Returns:
- the number of
properties
of thisReadableBean
.
-
getRequiredProperty
default ReadableProperty<?> getRequiredProperty(String name)
- Parameters:
name
- thename
of the requested property.- Returns:
- the requested
property
. - Throws:
RuntimeException
- if the requested property does not exist.
-
get
default Object get(String name)
- Parameters:
name
- theproperty name
.- Returns:
- the
value
of theproperty with the given name
. Will benull
if no such property exists or theproperty value
isnull
.
-
getPropertyNameForAlias
default String getPropertyNameForAlias(String alias)
An alias is an alternative for aproperty
name
. It allows to support a property under a legacy name after it has been renamed as well as to use a technical name containing special characters (e.g. "@" or ".") for very specific cases.- Parameters:
alias
- the alias name.- Returns:
- the resolved
property name
ornull
if no such alias is defined.
-
getType
BeanType getType()
- Returns:
- the
BeanType
reflecting thisBean
. - See Also:
VirtualBean.getType()
,BeanClass
-
isReadOnly
boolean isReadOnly()
- Returns:
true
if thisBean
is read-only (immutable),false
otherwise.
-
isDynamic
boolean isDynamic()
- Returns:
true
if thisBean
is dynamic meaning that is not strictly typed but allows to dynamically add properties,false
otherwise.- See Also:
VirtualBean
-
isPrototype
boolean isPrototype()
- Returns:
true
if thisBean
is aBeanClass
,false
otherwise (it is a regular instance).- See Also:
BeanClass.getPrototype()
-
isPolymorphic
default boolean isPolymorphic()
ABean
may be polymorphic to allow mappings to and from other representations without knowing the exact type. So assuming a service accepts or returns aBean
of a specific type that has sub-types. If that type is declared as polymorphic then it is possible to unarshall theBean
of the exact sub-type back from its serialized data.
By default aBean
is not polymorphic. Once you declare your customBean
as polymorphic by overriding this method returningtrue
, you may not this method it again. Hence, if you override this method in a class, you should declare it as final.
-
validate
default ValidationResult validate()
Description copied from interface:Validatable
This method performs the actual validation.- Specified by:
validate
in interfaceValidatable
- Returns:
ValidationResult
the validation failure.- See Also:
Validator.validate(Object)
-
isEqualTo
default boolean isEqualTo(ReadableBean other)
ABean
implementation shall not overrideObject.equals(Object)
andObject.hashCode()
for efficient usage inCollection
s andMap
s. Hence the regularequals
method will just check for object identity. For a logical equals check you may use this method. Be aware that is may be expensive as it recursively traverses into all properties that may again contain aReadableBean
.- Parameters:
other
- theReadableBean
to compare with.- Returns:
true
if thisReadableBean
is logically equal to the givenReadableBean
, that is it has the same type and allproperties
areequal
,false
otherwise.
-
write
default void write(StructuredWriter writer)
- Specified by:
write
in interfaceMarshallableObject
- Parameters:
writer
- theStructuredWriter
where to marshall (serialize) the data of this object to.- See Also:
MarshallableObject.writeObject(StructuredWriter, Object)
-
copy
default WritableBean copy()
- Returns:
- a copy of this
WritableBean
. LikenewInstance()
but with the values copied into the new instance.
-
copy
WritableBean copy(boolean readOnly)
- Parameters:
readOnly
- -true
if the copy shall beread-only
.- Returns:
- a
copy
of thisWritableBean
. IfreadOnly
istrue
and this bean is alreadyread-only
, the same instance will be returned.
-
newInstance
WritableBean newInstance()
- Returns:
- a new instance of this
WritableBean
.
-
newInstance
static <B extends ReadableBean> B newInstance(B bean)
- Type Parameters:
B
- type of theWritableBean
.- Parameters:
bean
- theWritableBean
to create anew instance
of.- Returns:
- the
new instance
.
-
copy
static <B extends ReadableBean> B copy(B bean, boolean readOnly)
- Type Parameters:
B
- type of theWritableBean
.- Parameters:
bean
- theWritableBean
tocopy
.readOnly
- -true
if the copy shall beread-only
.- Returns:
- the
copy
.
-
copyReadOnly
static <B extends ReadableBean> B copyReadOnly(B bean)
- Type Parameters:
B
- type of theWritableBean
.- Parameters:
bean
- theWritableBean
tocopy
.- Returns:
- the
read-only
copy
.
-
-