You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gary Gregory <ga...@gmail.com> on 2018/07/10 15:51:49 UTC

[ALL] Where should an OrderedObservable live in Commons?

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

Re: [ALL] Where should an OrderedObservable live in Commons?

Posted by Oliver Heger <ol...@oliver-heger.de>.

Am 10.07.2018 um 19:13 schrieb Gary Gregory:
> Yeah, I dislike *Util names as well, it's not only a co-out but an
> encouragement to make such classes dumping-grounds/kitchen-sinks.
> 
> I was thinking [lang] or maybe [collections] but it's not a collection
> really.

As the implementation is thread-safe, I think it may belong in a similar
category as the circuit breaker classes and other concurrency utils we
have currently in [lang].

Oliver

> 
> Gary
> 
> On Tue, Jul 10, 2018 at 10:14 AM Rob Tompkins <ch...@gmail.com> wrote:
> 
>> Wouldn’t this be an argument for commons-util (specifically for extensions
>> of java.util)? Now, I whole heartedly disagree with naming things
>> “XxxxUtils” because I feel like the whole idea of naming a piece of a
>> machine a utility is a cop out and bad practice in semantics, so maybe this
>> goes with [pool] or [lang]??
>>
>>> On Jul 10, 2018, at 11:51 AM, Gary Gregory <ga...@gmail.com>
>> wrote:
>>>
>>> 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
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [ALL] Where should an OrderedObservable live in Commons?

Posted by Gary Gregory <ga...@gmail.com>.
On Tue, Jul 10, 2018 at 12:05 PM sebb <se...@gmail.com> wrote:

> What are the common use cases for this?
> Do they come from a particular field, and if so, where would you
> expect to find related code in Commons?
>
> Just a thought.
>
> If there are likely to be more classes in the same general area, maybe
> you have found a new component.
>

It does not feel like a new component. I currently extend and use such a
class in a LifeCycle class where call sites register to be notified of life
cycle state changes.

Gary


>
> On 10 July 2018 at 18:13, Gary Gregory <ga...@gmail.com> wrote:
> > Yeah, I dislike *Util names as well, it's not only a co-out but an
> > encouragement to make such classes dumping-grounds/kitchen-sinks.
> >
> > I was thinking [lang] or maybe [collections] but it's not a collection
> > really.
> >
> > Gary
> >
> > On Tue, Jul 10, 2018 at 10:14 AM Rob Tompkins <ch...@gmail.com>
> wrote:
> >
> >> Wouldn’t this be an argument for commons-util (specifically for
> extensions
> >> of java.util)? Now, I whole heartedly disagree with naming things
> >> “XxxxUtils” because I feel like the whole idea of naming a piece of a
> >> machine a utility is a cop out and bad practice in semantics, so maybe
> this
> >> goes with [pool] or [lang]??
> >>
> >> > On Jul 10, 2018, at 11:51 AM, Gary Gregory <ga...@gmail.com>
> >> wrote:
> >> >
> >> > 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
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [ALL] Where should an OrderedObservable live in Commons?

Posted by sebb <se...@gmail.com>.
What are the common use cases for this?
Do they come from a particular field, and if so, where would you
expect to find related code in Commons?

Just a thought.

If there are likely to be more classes in the same general area, maybe
you have found a new component.

On 10 July 2018 at 18:13, Gary Gregory <ga...@gmail.com> wrote:
> Yeah, I dislike *Util names as well, it's not only a co-out but an
> encouragement to make such classes dumping-grounds/kitchen-sinks.
>
> I was thinking [lang] or maybe [collections] but it's not a collection
> really.
>
> Gary
>
> On Tue, Jul 10, 2018 at 10:14 AM Rob Tompkins <ch...@gmail.com> wrote:
>
>> Wouldn’t this be an argument for commons-util (specifically for extensions
>> of java.util)? Now, I whole heartedly disagree with naming things
>> “XxxxUtils” because I feel like the whole idea of naming a piece of a
>> machine a utility is a cop out and bad practice in semantics, so maybe this
>> goes with [pool] or [lang]??
>>
>> > On Jul 10, 2018, at 11:51 AM, Gary Gregory <ga...@gmail.com>
>> wrote:
>> >
>> > 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
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [ALL] Where should an OrderedObservable live in Commons?

Posted by Gary Gregory <ga...@gmail.com>.
Yeah, I dislike *Util names as well, it's not only a co-out but an
encouragement to make such classes dumping-grounds/kitchen-sinks.

I was thinking [lang] or maybe [collections] but it's not a collection
really.

Gary

On Tue, Jul 10, 2018 at 10:14 AM Rob Tompkins <ch...@gmail.com> wrote:

> Wouldn’t this be an argument for commons-util (specifically for extensions
> of java.util)? Now, I whole heartedly disagree with naming things
> “XxxxUtils” because I feel like the whole idea of naming a piece of a
> machine a utility is a cop out and bad practice in semantics, so maybe this
> goes with [pool] or [lang]??
>
> > On Jul 10, 2018, at 11:51 AM, Gary Gregory <ga...@gmail.com>
> wrote:
> >
> > 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [ALL] Where should an OrderedObservable live in Commons?

Posted by Rob Tompkins <ch...@gmail.com>.
Wouldn’t this be an argument for commons-util (specifically for extensions of java.util)? Now, I whole heartedly disagree with naming things “XxxxUtils” because I feel like the whole idea of naming a piece of a machine a utility is a cop out and bad practice in semantics, so maybe this goes with [pool] or [lang]??

> On Jul 10, 2018, at 11:51 AM, Gary Gregory <ga...@gmail.com> wrote:
> 
> 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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org