commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject [ALL] Where should an OrderedObservable live in Commons?
Date Tue, 10 Jul 2018 15:51:49 GMT
Does anyone have any thoughts on where an OrderedObservable should live in
Commons if at all?

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;

public class OrderedObservable<L> {

    private final ReadWriteLock readWriteLock = new
ReentrantReadWriteLock(true);
    protected final Lock readLock = readWriteLock.readLock();
    protected final Lock writeLock = readWriteLock.writeLock();
    private final List<L> listeners = new ArrayList<>();

    public void clearListeners() {
        listeners.clear();
    }

    public void notifyListeners(final Consumer<? super L> algorithm) {
        this.readLock.lock();
        try {
            this.listeners.forEach(algorithm);
        } finally {
            this.readLock.unlock();
        }
    }

    public L register(final L listener) {
        this.writeLock.lock();
        try {
            this.listeners.add(listener);
        } finally {
            this.writeLock.unlock();
        }
        return listener;
    }

    public void unregister(final L listener) {
        this.writeLock.lock();
        try {
            this.listeners.remove(listener);
        } finally {
            this.writeLock.unlock();
        }
    }

}

Thank you,
Gary

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message