Listeners


Лицензия

Лицензия

Группа

Группа

ru.noties
Идентификатор

Идентификатор

listeners
Последняя версия

Последняя версия

1.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

Listeners
Listeners
Ссылка на сайт

Ссылка на сайт

https://github.com/noties/Listeners
Система контроля версий

Система контроля версий

https://github.com/noties/Listeners

Скачать listeners

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
com.android.support » support-annotations jar 26.1.0

test (1)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12

Модули Проекта

Данный проект не имеет модулей.

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 (mail@dimitryivanov.ru)

  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.

Версии библиотеки

Версия
1.0.1
1.0.0