stasgora / observetree

Observetree is a library extending the Observable pattern by integrating it in a tree structure with propagating events

Home Page:https://stasgora.github.io/observetree/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central Build Status Last Release Date Sonar Tests Code Coverage License Sonar Quality Gate

Observetree library

Observetree is a library that extends the classic Observable pattern by integrating it in a tree structure. Change events are propagated through the Observable tree. Listeners can be assigned a priority to control the order at which they are called.

Getting started

Documentation Javadocs

Javadoc also available at sgora.dev/observetree/

For detailed technical description see Observable class

Installation

Maven Maven Central

<dependency>
  <groupId>dev.sgora</groupId>
  <artifactId>observetree</artifactId>
  <version>${observetree-version}</version>
</dependency>

(Earlier versions released under io.github.stasgora groupId)

SNAPSHOT versions are not synchronized to Central. If you want to use a snapshot version you need to add the https://oss.sonatype.org/content/repositories/snapshots/ repository to your pom.xml.

Manual

See the release page for jar downloads. Follow your IDE instructions for detailed steps on adding external library dependencies to your project.

Basic usage

  • Create model class extending Observable:
class Point extends Observable {
  public int x, y;
}
  • Internally call onValueChanged():
private void set(int x, int y) {
  this.x = x;
  this.y = y;
  onValueChanged();
}
  • Create instance and register a listener:
Point p = new Point();
p.addListener(() -> {...});
  • Change model object and allow it to call it's listeners:
p.set(1, 1);
p.notifyListeners();

Advanced functions

Using Observable trees

  • Build model structure:
class Model extends Observable {
  public Point point;
  ...
  public Model() {
    addSubObservable(point);
    ...
  }
}
  • You can subscribe to changes of objects on different levels:
model.addListener(...); // Changes to model and all it's sub-observables
model.point.addListener(...);
  • After changes to potentially many objects notify listeners of all changed objects in tree:
model.notifyListeners();

(For detailed behaviour description see the documentation)

Specifying listener priority

p.addListener(() -> {...}, ListenerPriority.HIGH);

Creating Settables out of external objects

  • Declare:
SettableProperty<Integer> size = new SettableProperty<>(1);
  • Get notified when the value is set:
size.set(2);

Using Settable Observables with persistent listeners

  • Declare:
SettableObservable<Point> point = new SettableObservable<>(new Point());
  • Register static listener:
point.addStaticListener(() -> {...});
  • After replacing the object the listeners will remain attached:
point.set(new Point());

Author

Stanisław Góra

License

MIT License

About

Observetree is a library extending the Observable pattern by integrating it in a tree structure with propagating events

https://stasgora.github.io/observetree/

License:MIT License


Languages

Language:Java 100.0%