Skip to content

Commit 725c609

Browse files
committed
Add usage example to README
1 parent a2a947b commit 725c609

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
# scijava-listeners
1+
# scijava-listeners
2+
3+
Helper class for maintaining lists of listeners.
4+
5+
Usage example:
6+
```java
7+
public interface MyListener
8+
{
9+
void somethingChanged();
10+
}
11+
12+
public class Listenable
13+
{
14+
// Create a variant of Listeners.List
15+
private final Listeners.List< MyListener > listeners = new Listeners.SynchronizedList<>();
16+
17+
// Use Listeners.List.list to call registered listeners
18+
private void notifyListeners() {
19+
listeners.list.forEach( MyListener::somethingChanged );
20+
}
21+
22+
// expose only Listeners (not Listeners.List) to allow un/registering listeners
23+
public Listeners< MyListener > myListeners() {
24+
return listeners;
25+
}
26+
}
27+
28+
public class Listening
29+
{
30+
public Listening( Listenable l ) {
31+
l.myListeners().add( this::notifyMe );
32+
}
33+
34+
void notifyMe() {
35+
System.out.println( "something changed!");
36+
}
37+
}
38+
```

0 commit comments

Comments
 (0)