Module io.github.mmm.bean
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
PackageDescriptionProvides the API for generic java beans defined as simple class or interface avoiding lots of boilerplate code.ContainsBeanProperty
to allow a bean to contain another bean.