- All Superinterfaces:
AttributeReadOnly,io.github.mmm.marshall.MarshallableObject,io.github.mmm.marshall.Marshaller<Object>,io.github.mmm.validation.Validatable
- All Known Subinterfaces:
VirtualBean,WritableBean
- All Known Implementing Classes:
AbstractBean,AbstractVirtualBean,AdvancedBean,Bean,DynamicBean
public interface ReadableBean
extends io.github.mmm.validation.Validatable, io.github.mmm.marshall.MarshallableObject, AttributeReadOnly
Read interface of a
Bean holding arbitrary properties. 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
FieldsModifier and TypeFieldDescriptionstatic final intPropertyIDforPROPERTY_TYPE_NAME.static final Stringstatic final StringThe optional suffix for a property method (when following JavaFx conventions what is not recommended by mmm-bean). -
Method Summary
Modifier and TypeMethodDescriptioncopy(boolean readOnly) static <B extends ReadableBean>
Bcopy(B bean, boolean readOnly) static <B extends ReadableBean>
BcopyReadOnly(B bean) default booleandoEquals(ReadableBean other) Method to implement#equals(Object)directly in a bean interface as default method.default StringMethod to implement#toString()directly in a bean interface as default method.default <V> VCollection<? extends ReadableProperty<?>>getProperty(String name) intdefault StringgetPropertyNameForAlias(String alias) default ReadableProperty<?>getRequiredProperty(String name) getType()booleandefault booleanisEqualTo(ReadableBean other) ABeanimplementation shall not overrideObject.equals(Object)andObject.hashCode()for efficient usage inCollections andMaps.default booleanABeanmay be polymorphic to allow mappings to and from other representations without knowing the exact type.booleandefault voidmapPropertyIds(PropertyIdCollector mapping) Defines how thepropertiesof thisReadableBeanare mapped to numeric IDs.static <B extends ReadableBean>
BnewInstance(B bean) default voiddefault io.github.mmm.validation.ValidationResultvalidate()Methods inherited from interface io.github.mmm.property.AttributeReadOnly
isReadOnlyMethods inherited from interface io.github.mmm.marshall.MarshallableObject
write, writeObjectMethods inherited from interface io.github.mmm.validation.Validatable
validateOrThrow
-
Field Details
-
SUFFIX_PROPERTY
The optional suffix for a property method (when following JavaFx conventions what is not recommended by mmm-bean).- See Also:
-
PROPERTY_TYPE_NAME
- See Also:
-
isPolymorphic()MarshallableObject.write(StructuredWriter)- Constant Field Values
-
PROPERTY_TYPE_ID
static final int PROPERTY_TYPE_IDPropertyIDforPROPERTY_TYPE_NAME.- See Also:
-
-
Method Details
-
getProperty
- Parameters:
name- thenameof the requested property or a potentialaliasof the property.- Returns:
- the requested
WritablePropertyornullif no such property exists. - See Also:
-
getProperties
Collection<? extends ReadableProperty<?>> getProperties()- Returns:
- a
Collectionwith allpropertiesof this bean.
-
getPropertyCount
int getPropertyCount()- Returns:
- the number of
propertiesof thisReadableBean.
-
getRequiredProperty
- Parameters:
name- thenameof the requested property.- Returns:
- the requested
property. - Throws:
RuntimeException- if the requested property does not exist.
-
get
- Type Parameters:
V- type of theproperty value.- Parameters:
name- theproperty name.- Returns:
- the
valueof theproperty with the given name. Will benullif no such property exists or theproperty valueisnull.
-
getPropertyNameForAlias
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() -
isDynamic
boolean isDynamic()- Returns:
trueif thisBeanis dynamic meaning that is not strictly typed but allows to dynamically add properties,falseotherwise.- See Also:
-
isPrototype
boolean isPrototype() -
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 io.github.mmm.validation.ValidationResult validate()- Specified by:
validatein interfaceio.github.mmm.validation.Validatable
-
isEqualTo
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.
-
copy
- Parameters:
readOnly- -trueif the copy shall beread-only.- Returns:
- a copy of this
WritableBeanthat has the same values for allproperties.
-
newInstance
WritableBean newInstance()- Returns:
- a new instance of this
WritableBean.
-
mapPropertyIds
Defines how thepropertiesof thisReadableBeanare mapped to numeric IDs. This is only relevant in case you want to use binary protocols ProtoBuf/GRPC.- Parameters:
mapping- thePropertyIdCollectorused to receive the mapping.
-
doEquals
Method to implement#equals(Object)directly in a bean interface as default method. For simplification it is only called if the object to compare to is notnulland has the sametypeso you do not have to handle these cases in your custom implementation.
ATTENTION: It is rather discouraged to override this method. Simply useisEqualTo(ReadableBean)instead of#equals(Object)when you want comparison by value (e.g. to check for duplicates).- Parameters:
other- the object to compare to. Will not benulland has the sametypeas this bean.- Returns:
trueif this and the givenReadableBeanare considered equals,falseotherwise.- See Also:
-
#equals(Object)isEqualTo(ReadableBean)
-
doToString
Method to implement#toString()directly in a bean interface as default method.- Returns:
- the
string representation of this bean.
-
toString
- Parameters:
sb- theStringBuilderwhere to append the details (the properties) of thisBeanfor#toString()-Representation.
-
newInstance
- Type Parameters:
B- type of theWritableBean.- Parameters:
bean- theWritableBeanto create anew instanceof.- Returns:
- the
new instance.
-
copy
- Type Parameters:
B- type of theWritableBean.- Parameters:
bean- theWritableBeantocopy.readOnly- -trueif the copy shall beread-only.- Returns:
- the
copy.
-
copyReadOnly
- Type Parameters:
B- type of theWritableBean.- Parameters:
bean- theWritableBeantocopy.- Returns:
- the
read-onlycopy.
-