Skip to content

Commit 380bc96

Browse files
author
nicolaiparlog
committed
Factory class 'ListenerHandles' creates handles for observable collections.
1 parent c6390c6 commit 380bc96

File tree

2 files changed

+187
-4
lines changed

2 files changed

+187
-4
lines changed

src/main/java/org/codefx/libfx/listener/handle/ListenerHandleBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* This implies that they can be stateless functions which simply add and remove the listener. The functions are called
1717
* with the observable and listener specified during construction.
1818
* <p>
19+
* The {@link ListenerHandle} returned by this builder is not yet attached.
20+
* <p>
1921
* <h2>Example</h2> A typical use looks like this:
2022
*
2123
* <pre>

src/main/java/org/codefx/libfx/listener/handle/ListenerHandles.java

Lines changed: 185 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
import javafx.beans.Observable;
55
import javafx.beans.value.ChangeListener;
66
import javafx.beans.value.ObservableValue;
7+
import javafx.collections.ArrayChangeListener;
8+
import javafx.collections.ListChangeListener;
9+
import javafx.collections.MapChangeListener;
10+
import javafx.collections.ObservableArray;
11+
import javafx.collections.ObservableList;
12+
import javafx.collections.ObservableMap;
13+
import javafx.collections.ObservableSet;
14+
import javafx.collections.SetChangeListener;
715

816
/**
917
* Factory class for functionality surrounding {@link ListenerHandle}s.
@@ -30,15 +38,16 @@ private ListenerHandles() {
3038
* @param listener
3139
* the listener which will be added to the {@code observable}
3240
* @return a {@link ListenerHandleBuilder} for a {@code ListenerHandle}.
41+
* @see ListenerHandleBuilder
3342
*/
34-
public static <O, L> ListenerHandleBuilder<O, L> buildFor(O observable, L listener) {
43+
public static <O, L> ListenerHandleBuilder<O, L> createFor(O observable, L listener) {
3544
return ListenerHandleBuilder.from(observable, listener);
3645
}
3746

3847
// Observable + InvalidationListener
3948

4049
/**
41-
* Ands the specified listener to the specified observable and returns a handle for the combination.
50+
* Adds the specified listener to the specified observable and returns a handle for the combination.
4251
*
4352
* @param observable
4453
* the {@link Observable} to which the {@code invalidationListener} will be added
@@ -72,7 +81,7 @@ public static ListenerHandle createDetached(Observable observable, InvalidationL
7281
// ObservableValue + ChangeListener
7382

7483
/**
75-
* Ands the specified listener to the specified observable and returns a handle for the combination.
84+
* Adds the specified listener to the specified observable value and returns a handle for the combination.
7685
*
7786
* @param <T>
7887
* the type of the value wrapped by the observable
@@ -91,7 +100,7 @@ public static <T> ListenerHandle create(
91100
}
92101

93102
/**
94-
* Creates a listener handle for the specified observable and listener. The listener is not yet attached!
103+
* Creates a listener handle for the specified observable value and listener. The listener is not yet attached!
95104
*
96105
* @param <T>
97106
* the type of the value wrapped by the observable
@@ -111,4 +120,176 @@ public static <T> ListenerHandle createDetached(
111120
.build();
112121
}
113122

123+
// ObservableArray + ArrayChangeListener
124+
125+
/**
126+
* Adds the specified listener to the specified observable array and returns a handle for the combination.
127+
*
128+
* @param <T>
129+
* the type of the array wrapped by the observable
130+
* @param observableArray
131+
* the {@link ObservableArray} to which the {@code changeListener} will be added
132+
* @param changeListener
133+
* the {@link ArrayChangeListener} which will be added to the {@code observableArray}
134+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
135+
*/
136+
public static <T extends ObservableArray<T>> ListenerHandle create(
137+
ObservableArray<T> observableArray, ArrayChangeListener<T> changeListener) {
138+
139+
ListenerHandle handle = createDetached(observableArray, changeListener);
140+
handle.attach();
141+
return handle;
142+
}
143+
144+
/**
145+
* Creates a listener handle for the specified observable array and listener. The listener is not yet attached!
146+
*
147+
* @param <T>
148+
* the type of the array wrapped by the observable
149+
* @param observableArray
150+
* the {@link ObservableArray} to which the {@code changeListener} will be added
151+
* @param changeListener
152+
* the {@link ArrayChangeListener} which will be added to the {@code observableArray}
153+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
154+
*/
155+
public static <T extends ObservableArray<T>> ListenerHandle createDetached(
156+
ObservableArray<T> observableArray, ArrayChangeListener<T> changeListener) {
157+
158+
return ListenerHandleBuilder
159+
.from(observableArray, changeListener)
160+
.onAttach((observable, listener) -> observable.addListener(listener))
161+
.onDetach((observable, listener) -> observable.removeListener(listener))
162+
.build();
163+
}
164+
165+
// ObservableList + ListChangeListener
166+
167+
/**
168+
* Adds the specified listener to the specified observable list and returns a handle for the combination.
169+
*
170+
* @param <E>
171+
* the list element type
172+
* @param observableList
173+
* the {@link ObservableList} to which the {@code changeListener} will be added
174+
* @param changeListener
175+
* the {@link ListChangeListener} which will be added to the {@code observableList}
176+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
177+
*/
178+
public static <E> ListenerHandle create(
179+
ObservableList<E> observableList, ListChangeListener<? super E> changeListener) {
180+
181+
ListenerHandle handle = createDetached(observableList, changeListener);
182+
handle.attach();
183+
return handle;
184+
}
185+
186+
/**
187+
* Creates a listener handle for the specified observable list and listener. The listener is not yet attached!
188+
*
189+
* @param <E>
190+
* the list element type
191+
* @param observableList
192+
* the {@link ObservableList} to which the {@code changeListener} will be added
193+
* @param changeListener
194+
* the {@link ListChangeListener} which will be added to the {@code observableList}
195+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
196+
*/
197+
public static <E> ListenerHandle createDetached(
198+
ObservableList<E> observableList, ListChangeListener<? super E> changeListener) {
199+
200+
return ListenerHandleBuilder
201+
.from(observableList, changeListener)
202+
.onAttach((observable, listener) -> observable.addListener(listener))
203+
.onDetach((observable, listener) -> observable.removeListener(listener))
204+
.build();
205+
}
206+
207+
// ObservableSet + SetChangeListener
208+
209+
/**
210+
* Adds the specified listener to the specified observable set and returns a handle for the combination.
211+
*
212+
* @param <E>
213+
* the set element type
214+
* @param observableSet
215+
* the {@link ObservableSet} to which the {@code changeListener} will be added
216+
* @param changeListener
217+
* the {@link SetChangeListener} which will be added to the {@code observableSet}
218+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
219+
*/
220+
public static <E> ListenerHandle create(
221+
ObservableSet<E> observableSet, SetChangeListener<? super E> changeListener) {
222+
223+
ListenerHandle handle = createDetached(observableSet, changeListener);
224+
handle.attach();
225+
return handle;
226+
}
227+
228+
/**
229+
* Creates a listener handle for the specified observable set and listener. The listener is not yet attached!
230+
*
231+
* @param <E>
232+
* the set element type
233+
* @param observableSet
234+
* the {@link ObservableSet} to which the {@code changeListener} will be added
235+
* @param changeListener
236+
* the {@link SetChangeListener} which will be added to the {@code observableSet}
237+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
238+
*/
239+
public static <E> ListenerHandle createDetached(
240+
ObservableSet<E> observableSet, SetChangeListener<? super E> changeListener) {
241+
242+
return ListenerHandleBuilder
243+
.from(observableSet, changeListener)
244+
.onAttach((observable, listener) -> observable.addListener(listener))
245+
.onDetach((observable, listener) -> observable.removeListener(listener))
246+
.build();
247+
}
248+
249+
// ObservableMap + MapChangeListener
250+
251+
/**
252+
* Adds the specified listener to the specified observable map and returns a handle for the combination.
253+
*
254+
* @param <K>
255+
* the map key element type
256+
* @param <V>
257+
* the map value element type
258+
* @param observableMap
259+
* the {@link ObservableMap} to which the {@code changeListener} will be added
260+
* @param changeListener
261+
* the {@link MapChangeListener} which will be added to the {@code observableMap}
262+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
263+
*/
264+
public static <K, V> ListenerHandle create(
265+
ObservableMap<K, V> observableMap, MapChangeListener<? super K, ? super V> changeListener) {
266+
267+
ListenerHandle handle = createDetached(observableMap, changeListener);
268+
handle.attach();
269+
return handle;
270+
}
271+
272+
/**
273+
* Creates a listener handle for the specified observable map and listener. The listener is not yet attached!
274+
*
275+
* @param <K>
276+
* the map key element type
277+
* @param <V>
278+
* the map value element type
279+
* @param observableMap
280+
* the {@link ObservableMap} to which the {@code changeListener} will be added
281+
* @param changeListener
282+
* the {@link MapChangeListener} which will be added to the {@code observableMap}
283+
* @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
284+
*/
285+
public static <K, V> ListenerHandle createDetached(
286+
ObservableMap<K, V> observableMap, MapChangeListener<? super K, ? super V> changeListener) {
287+
288+
return ListenerHandleBuilder
289+
.from(observableMap, changeListener)
290+
.onAttach((observable, listener) -> observable.addListener(listener))
291+
.onDetach((observable, listener) -> observable.removeListener(listener))
292+
.build();
293+
}
294+
114295
}

0 commit comments

Comments
 (0)