项目作者: borisskert

项目描述 :
Observable properties which are notifying listeners about changes
高级语言: Java
项目地址: git://github.com/borisskert/java-observable-properties.git
创建时间: 2020-02-06T21:23:44Z
项目社区:https://github.com/borisskert/java-observable-properties

开源协议:Apache License 2.0

下载


Java Observable Properties

This project contains properties which are able to notify registered listeners about changes.

Usage

Insert dependency into your maven pom.xml:

  1. <dependency>
  2. <groupId>de.borisskert</groupId>
  3. <artifactId>java-observable-properties</artifactId>
  4. <version>0.1.0-17</version>
  5. </dependency>

Simple object properties

Simple properties are forced to contain a value. For optional values consider to use an OptionalProperty instance.

Generic object property

Instance creation

  1. Property<Object> property = new SimpleObjectProperty<>(new Object());

Retrieve the value

  1. Object object = property.get();

Update the value

  1. property.set(new Object());

Register a listener

“Classical style”
  1. public class MyChangeListener implements ChangeListener<Object> {
  2. @Override
  3. public void onChange(ReadonlyProperty<Object> property, Object oldValue, Object newValue) {
  4. // property: the property instance which is currently notifying this listener
  5. // oldValue: the value before this change occurred
  6. // newValue: the new value
  7. }
  8. }
  9. ChangeListener<Object> listener = new MyChangeListener();
  10. property.addListener(listener);
Anonymous class
  1. property.addListener(new ChangeListener<TestObject>() {
  2. @Override
  3. public void onChange(ReadonlyProperty<TestObject> property, TestObject oldValue, TestObject newValue) {
  4. // property: the property instance which is currently notifying this listener
  5. // oldValue: the value before this change occurred
  6. // newValue: the new value
  7. }
  8. });
Lambda style
  1. property.addListener((property, oldValue, newValue) -> {
  2. // property: the property instance which is currently notifying this listener
  3. // oldValue: the value before this change occurred
  4. // newValue: the new value
  5. });

Unregister a listener

  1. property.removeListener(listener);

Bind a Property

  1. property.bind(anotherProperty);

Unbind a Property

  1. property.unbind(boundProperty);

Typed properties

String

  1. Property<String> property = new StringProperty<>("your property value");

Integer

  1. Property<Integer> property = new IntegerProperty<>(123);

Double

  1. Property<Double> property = new DoubleProperty<>(1.23);

Boolean

  1. Property<Boolean> property = new BooleanProperty<>(true);

Long

  1. Property<Long> property = new LongProperty<>(123L);

Float

  1. Property<Float> property = new FloatProperty<>(1.23f);

Short

  1. short myShort = 123;
  2. Property<Short> property = new ShortProperty<>(myShort);

Optional properties

Optional properties are able to be empty. Technically they contain a null value.

Generic optional property

Instance creation

  1. OptionalProperty<Object> property = new SimpleOptionalProperty<>(new Object()); // this property is filled
  2. OptionalProperty<Object> empty = new SimpleOptionalProperty<>(); // this one is empty

Retrieve the optional value

  1. Optional<Object> maybeObject = property.asOptional();

Typed optional properties

String

  1. OptionalProperty<String> property = new OptionalStringProperty<>("your property value");
  2. OptionalProperty<String> empty = new OptionalStringProperty<>();

Integer

  1. OptionalProperty<Integer> property = new OptionalIntegerProperty<>(123);
  2. OptionalProperty<Integer> empty = new OptionalIntegerProperty<>();

Double

  1. OptionalProperty<Double> property = new OptionalDoubleProperty<>(1.23);
  2. OptionalProperty<Double> empty = new OptionalDoubleProperty<>();

Boolean

  1. OptionalProperty<Boolean> property = new OptionalBooleanProperty<>(true);
  2. OptionalProperty<Boolean> empty = new OptionalBooleanProperty<>();

Long

  1. OptionalProperty<Long> property = new OptionalLongProperty<>(123L);
  2. OptionalProperty<Long> empty = new OptionalLongProperty<>();

Float

  1. OptionalProperty<Float> property = new OptionalFloatProperty<>(1.23f);
  2. OptionalProperty<Float> empty = new OptionalFloatProperty<>();

Short

  1. short myShort = 123;
  2. OptionalProperty<Short> property = new OptionalShortProperty<>(myShort);
  3. OptionalProperty<Short> empty = new OptionalShortProperty<>();