Event Dispatcher  v0.1.6-beta.1
Project that offers a small library that allows to subscribe and fire events.
ObservedValue.java
Go to the documentation of this file.
1 package com.fermod.observer;
2 
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.io.Serializable;
7 
10 
17 public class ObservedValue<T> extends EventPublisher<ValueChangeListener<T>> implements Serializable {
18 
19  private T value;
20 
27  public ObservedValue(T value) {
28  this.value = value;
29  }
30 
37  public void set(T value) {
38  set(value, true);
39  }
40 
49  public void set(T value, boolean notifyChange) {
50 
51  T oldValue = this.value;
52  this.value = value;
53 
54  if(notifyChange && oldValue != value) {
55  // Notify the list of registered listeners
56  this.notifyListeners(listener -> listener.onValueChanged(oldValue, value));
57  }
58 
59  }
60 
66  public T get() {
67  return value;
68  }
69 
70  @Override
71  public boolean equals(Object obj) {
72 
73  if (obj == null) {
74  return false;
75  }
76 
77  if (!ObservedValue.class.isAssignableFrom(obj.getClass())) {
78  return false;
79  }
80 
81  @SuppressWarnings("unchecked")
82  final ObservedValue<T> other = (ObservedValue<T>) obj;
83  if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
84  return false;
85  }
86 
87  return true;
88  }
89 
90  @Override
91  public int hashCode() {
92  int hash = 17;
93  hash = 31 * hash + (this.value != null ? this.value.hashCode() : 0);
94  return hash;
95  }
96 
115  private void writeObject(ObjectOutputStream out) throws IOException {
116  out.writeObject(value);
117  }
118 
144  @SuppressWarnings("unchecked")
145  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
146  this.value = (T) in.readObject();
147  }
148 
152  private static final long serialVersionUID = 8159940159964193507L;
153 
154 }
Class that wraps a value and gives the utility of registering listeners that will be called after any...
void notifyListeners(Consumer<? super T > action)
Notifies and executes on each of the registered listeners the provided function.
ObservedValue(T value)
Constructor of the class.
static final long serialVersionUID
Auto-generated serial version ID.
The listener interface for receiving value change events.
void writeObject(ObjectOutputStream out)
The writeObject method is responsible for writing the state of the object for its particular class so...
void readObject(ObjectInputStream in)
The readObject method is responsible for reading from the stream and restoring the classes fields.
Class that allows to register and notify event listeners.