Listeners


License

License

GroupId

GroupId

ru.noties
ArtifactId

ArtifactId

listeners
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Listeners
Listeners
Project URL

Project URL

https://github.com/noties/Listeners
Source Code Management

Source Code Management

https://github.com/noties/Listeners

Download listeners

How to add to project

<!-- https://jarcasting.com/artifacts/ru.noties/listeners/ -->
<dependency>
    <groupId>ru.noties</groupId>
    <artifactId>listeners</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/ru.noties/listeners/
implementation 'ru.noties:listeners:1.0.1'
// https://jarcasting.com/artifacts/ru.noties/listeners/
implementation ("ru.noties:listeners:1.0.1")
'ru.noties:listeners:jar:1.0.1'
<dependency org="ru.noties" name="listeners" rev="1.0.1">
  <artifact name="listeners" type="jar" />
</dependency>
@Grapes(
@Grab(group='ru.noties', module='listeners', version='1.0.1')
)
libraryDependencies += "ru.noties" % "listeners" % "1.0.1"
[ru.noties/listeners "1.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
com.android.support » support-annotations jar 26.1.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Listeners<T>

listeners

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.

Versions

Version
1.0.1
1.0.0