Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 87b6de5

Browse files
committed
Added Value Change Listeners
1 parent 90fdd65 commit 87b6de5

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/main/java/clientapi/value/IValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public interface IValue<T> extends Nameable, Describable, Identifiable, IValueHo
4848
*/
4949
void setValue(T value);
5050

51+
/**
52+
* Adds a change listener to this value
53+
*
54+
* @param listener The change listener
55+
*/
56+
void addChangeListener(ValueChangeListener<T> listener);
57+
5158
/**
5259
* Intended to be used with flatMap() to flatten the IValue tree recursively
5360
* @param value the root node of the tree

src/main/java/clientapi/value/Value.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class Value<T> implements IValue<T> {
4747
*/
4848
private final Map<String, IValue> valueCache = new HashMap<>();
4949

50+
/**
51+
* A list of all of the {@code ValueChangeListeners} waiting for this value to change
52+
*/
53+
private final List<ValueChangeListener<T>> valueChangeListeners = new ArrayList<>();
54+
5055
/**
5156
* Name of the Value
5257
*/
@@ -117,9 +122,17 @@ public T getValue() {
117122

118123
@Override
119124
public void setValue(T value) {
125+
// Notify all of the change listeners of the new change of state
126+
this.valueChangeListeners.forEach(listener -> listener.onValueChanged(this, this.getValue(), value));
127+
// Pass the new value to the change function
120128
this.mutable.accept(value);
121129
}
122130

131+
@Override
132+
public void addChangeListener(ValueChangeListener<T> listener) {
133+
this.valueChangeListeners.add(listener);
134+
}
135+
123136
@Override
124137
public final String getName() {
125138
return this.name;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 ImpactDevelopment
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package clientapi.value;
18+
19+
/**
20+
* @author Brady
21+
* @since 5/31/2018 3:02 PM
22+
*/
23+
@FunctionalInterface
24+
public interface ValueChangeListener<T> {
25+
26+
void onValueChanged(Value<T> value, T oldValue, T newValue);
27+
}

0 commit comments

Comments
 (0)