-
- 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 aBeanholding 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
dynamicbeans. 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 theBeanis written back. - ReadOnly-Support - create a
read-onlycopyof your object to pass by reference without side-effects. - Powerful -
WritablePropertysupports listeners and bindings as well asgeneric type information. - Validation - build-in
validation support. - Marshalling - build-in support to
readandwritetheBeanfrom/to JSON, XML, or other formats. Implement custom datatypes aspropertyand 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 StringPROPERTY_TYPE
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default WritableBeancopy()WritableBeancopy(boolean readOnly)static <B extends ReadableBean>
Bcopy(B bean, boolean readOnly)static <B extends ReadableBean>
BcopyReadOnly(B bean)default Objectget(String name)Iterable<? extends ReadableProperty<?>>getProperties()ReadableProperty<?>getProperty(String name)intgetPropertyCount()default StringgetPropertyNameForAlias(String alias)default ReadableProperty<?>getRequiredProperty(String name)BeanTypegetType()booleanisDynamic()default booleanisEqualTo(ReadableBean other)ABeanimplementation shall not overrideObject.equals(Object)andObject.hashCode()for efficient usage inCollections andMaps.default booleanisPolymorphic()ABeanmay be polymorphic to allow mappings to and from other representations without knowing the exact type.booleanisPrototype()booleanisReadOnly()WritableBeannewInstance()static <B extends ReadableBean>
BnewInstance(B bean)default ValidationResultvalidate()This method performs the actual validation.default voidwrite(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- thenameof the requested property or a potentialaliasof the property.- Returns:
- the requested
WritablePropertyornullif no such property exists. - See Also:
WritableBean.addProperty(WritableProperty),WritableBean.getOrCreateProperty(String, Class)
-
getProperties
Iterable<? extends ReadableProperty<?>> getProperties()
- Returns:
- an
Iterableof all properties of this bean.
-
getPropertyCount
int getPropertyCount()
- Returns:
- the number of
propertiesof thisReadableBean.
-
getRequiredProperty
default ReadableProperty<?> getRequiredProperty(String name)
- Parameters:
name- thenameof 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
valueof theproperty with the given name. Will benullif no such property exists or theproperty valueisnull.
-
getPropertyNameForAlias
default String getPropertyNameForAlias(String alias)
An alias is an alternative for apropertyname. 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 nameornullif no such alias is defined.
-
getType
BeanType getType()
- Returns:
- the
BeanTypereflecting thisBean. - See Also:
VirtualBean.getType(),BeanClass
-
isReadOnly
boolean isReadOnly()
- Returns:
trueif thisBeanis read-only (immutable),falseotherwise.
-
isDynamic
boolean isDynamic()
- Returns:
trueif thisBeanis dynamic meaning that is not strictly typed but allows to dynamically add properties,falseotherwise.- See Also:
VirtualBean
-
isPrototype
boolean isPrototype()
- Returns:
trueif thisBeanis aBeanClass,falseotherwise (it is a regular instance).- See Also:
BeanClass.getPrototype()
-
isPolymorphic
default boolean isPolymorphic()
ABeanmay be polymorphic to allow mappings to and from other representations without knowing the exact type. So assuming a service accepts or returns aBeanof a specific type that has sub-types. If that type is declared as polymorphic then it is possible to unarshall theBeanof the exact sub-type back from its serialized data.
By default aBeanis not polymorphic. Once you declare your customBeanas 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:ValidatableThis method performs the actual validation.- Specified by:
validatein interfaceValidatable- Returns:
ValidationResultthe validation failure.- See Also:
Validator.validate(Object)
-
isEqualTo
default boolean isEqualTo(ReadableBean other)
ABeanimplementation shall not overrideObject.equals(Object)andObject.hashCode()for efficient usage inCollections andMaps. Hence the regularequalsmethod 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- theReadableBeanto compare with.- Returns:
trueif thisReadableBeanis logically equal to the givenReadableBean, that is it has the same type and allpropertiesareequal,falseotherwise.
-
write
default void write(StructuredWriter writer)
- Specified by:
writein interfaceMarshallableObject- Parameters:
writer- theStructuredWriterwhere 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- -trueif the copy shall beread-only.- Returns:
- a
copyof thisWritableBean. IfreadOnlyistrueand 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- theWritableBeanto create anew instanceof.- Returns:
- the
new instance.
-
copy
static <B extends ReadableBean> B copy(B bean, boolean readOnly)
- Type Parameters:
B- type of theWritableBean.- Parameters:
bean- theWritableBeantocopy.readOnly- -trueif 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- theWritableBeantocopy.- Returns:
- the
read-onlycopy.
-
-