Module io.github.mmm.bean
Provides advanced Java beans based on
Writing regular Java Beans is painful. You have to write a lot of boilerplate code and implement getters, setters, equals, hashCode, and toString.
The
To implement a bean you only need to extend from
If you want to have no boilerplate code at all, like to have multi-inheritance and do not fear magic, you can even define your beans as interfaces only. Then you can instantiate them using
mmm-property
.Bean
Writing regular Java Beans is painful. You have to write a lot of boilerplate code and implement getters, setters, equals, hashCode, and toString.
The
WritableBean
API provided here saves you from all this pain and makes your life a lot
easier.
Class implementation
To implement a bean you only need to extend from
Bean
:
public class TestBean extends Bean { public final StringProperty Name; public final IntegerProperty Age; public TestBean() { this(null, true); } public TestBean(AbstractBean writable, boolean dynamic) { super(writable, dynamic); this.Name = add(new StringProperty("Name")); this.Age = add(new IntegerProperty("Age")); } }Now you can do things like this:
TestBean bean = new TestBean(); bean.Name.set("John Doe"); bean.Age.set(42); // Read-Only views TestBean readonly = WritableBean.getReadOnly(bean); assertThat(readonly.Age.get()).isEqualTo(42); bean.Age.set(43); assertThat(readonly.Age.get()).isEqualTo(43); try { readonly.Age.set(44); fail("Exception expected"); } catch (IllegalStateException e) { } // Change listener... bean.Age.addListener((e) -> { System.out.println(e.getOldValue() + "-->" + e.getValue()); }); bean.Age.set(44); // prints: 43 --> 44 // Copy and compare TestBean bean2 = new TestBean(); for (WritableProperty<?> property : bean.getProperties()) { bean2.set(property.getName(), property.getValue()); } assertThat(bean.isEqualTo(bean2)).isTrue();
Interface only
If you want to have no boilerplate code at all, like to have multi-inheritance and do not fear magic, you can even define your beans as interfaces only. Then you can instantiate them using
BeanFactory
from the module
io.github.mmm.bean.factory
of mmm-bean-factory
.-
-
Packages
Exports Package Description io.github.mmm.bean Provides the API for generic java beans defined as simple class or interface avoiding lots of boilerplate code.io.github.mmm.bean.property ContainsBeanProperty
to allow a bean to contain another bean.
-
Modules
Requires Modifier Module Description transitive io.github.mmm.property Provides advanced properties with support for change-listeners, bindings, validation, and marshalling.transitive io.github.mmm.property.builder Provides advanced properties with support for change-listeners, bindings, validation, and marshalling.Indirect Requires Modifier Module Description transitive io.github.mmm.base Provides fundamental APIs and helper classes.transitive io.github.mmm.event Provides generic, reusable infrastructure to define, send and receive events.transitive io.github.mmm.marshall Provides the API for mashalling and unmarshalling data to structured formats.transitive io.github.mmm.nls Provides advanced native language support.transitive io.github.mmm.scanner Provides scanners that help to parse character sequences efficient and easily.transitive io.github.mmm.validation Provides validation API and infrastructure.transitive io.github.mmm.validation.main Provides standard validators with NLS and typed builders.transitive io.github.mmm.value Provides a minimalistic but powerful API for value containers.transitive io.github.mmm.value.observable Provides observable value API and implementation for standard Java types.
-