Listeners<T>
Simple data structure for listeners and observers that allows adding/removing elements whilst iterating without copying underlying collection. Aimed for use in one thread (for example some UI events listeners)
interface MyListener {
void apply(@NonNull MyListenersStore store);
}
class MyListenersStore {
private final Listeners<MyListener> listeners = Listeners.create();
void register(@NonNull MyListener listener) {
listeners.add(listener);
}
void unregister(@NonNull MyListener listener) {
listeners.remove(listener);
}
void notifyListeners() {
for (MyListener listener : listeners.begin()) {
listener.apply(this);
}
}
}
Then, MyListener
can be implemented like this:
class MyListenerImpl implements MyListener {
@Override
public void apply(@NonNull MyListenersStore store) {
// do something here
doSomething();
// and unregister
store.unregister(this);
}
}
Limitations
Only one iteration can happen at a time
for (MyListener listener1 : listeners.begin()) {
for (MyListener listener2 : listeners.begin()) { // will throw
}
}
The same if listener itself triggers notification:
class MyListenerImpl implements MyListener {
@Override
public void apply(@NonNull MyListenersStore store) {
store.notifyListeners(); // will throw
}
}
If you plan to iterate on part of collection (for example with early break or some condition), explicit end()
must be called.
for (MyListener listener : listeners.begin()) {
if (!isValid()) {
break;
}
}
listeners.end();
License
Copyright 2017 Dimitry Ivanov ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.