Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Observable collections (set, list, map)

105 views
Skip to first unread message

Karsten Wutzke

unread,
Aug 9, 2007, 6:01:48 AM8/9/07
to
Hello!

Does anyone know of a collection package/API that extends
java.util.Observable collection classes such as Set, List and Map?

I also looked at Apache collections, but they don't seem to include
that.

I'd also like to get some input on whether I/you/someone need/s
Observable collections at all. For me its main use would be for GUI
updates (of course), for JList's, JTabbedPane's, that is higher level
models.

I've also looked at JGoodies binding for that matter, however that
package doesn't use java.util.Observable and (which is worse) doesn't
seem to be compatible with JTabbedPane's. So I came to the point
writing Observable collections.

An idea would be to build a set of collection classes as a Decorator,
adding allow/forbid null, allow/forbid duplicates, and Observable
functionality as needed. Of course, this will decrease the
performance, but this is not much of a matter (at least right now).

Since I couldn't find something like it, I suspect my approach might
not be the best and/or that there are different approaches.

Can you share your experience with me please? What do you think
(comments and suggestions welcome)?

Karsten

inquisitive

unread,
Aug 9, 2007, 6:23:26 AM8/9/07
to
Hi Karsten
I am not sure if i came accross any such collection.
just a thought ..
u can think of making a class that
extends java.util.Observable and has a pvt

inquisitive

unread,
Aug 9, 2007, 6:26:55 AM8/9/07
to
u can think of making a class that

- extends java.util.Observable
- has a pvt Collection member (set/list/map)
- public getter setter and other public methods that you need (to
add , modify collection )

regards
lavnish

> > Karsten- Hide quoted text -
>
> - Show quoted text -


Karsten Wutzke

unread,
Aug 9, 2007, 6:34:18 AM8/9/07
to

Sure something like:

public class ObservableList<E> extends Observable implements List<E>,
Queue<E>, Cloneable, Serializable
{
protected static final int UNRESTRICTED = -1;


//list instance delegate
private final List<E> list;

...

}

However, the thought was that something that trivial has already been
implemented and published by someone...

Karsten

inquisitive

unread,
Aug 9, 2007, 6:51:03 AM8/9/07
to

Thomas Hawtin

unread,
Aug 9, 2007, 7:14:10 AM8/9/07
to
Karsten Wutzke wrote:
>
> Does anyone know of a collection package/API that extends
> java.util.Observable collection classes such as Set, List and Map?

I don't know why you would want to use Observable. The event/listener
idiom is the standard way to go about these sorts of things.

Although I haven't used it myself, Glazed Lists seems to have attracted
positive comments.

http://publicobject.com/glazedlists/

Tom Hawtin

Karsten Wutzke

unread,
Aug 9, 2007, 8:36:23 AM8/9/07
to
On 9 Aug., 12:51, inquisitive <lavni...@gmail.com> wrote:
> hmmmmm
> tryhttp://commons.apache.org/sandbox/events/apidocs/org/apache/commons/e...
> but it has only listhttp://commons.apache.org/sandbox/events/apidocs/org/apache/commons/e...
> and Sethttp://commons.apache.org/sandbox/events/apidocs/org/apache/commons/e...
>
> cheerz !!
>
snip

> > - Show quoted text -

Well I've been that far myself (using google)... That package is out
of date with generics and its classes are not java.util.Observable
subclasses.

So no cheers yet...

Karsten

Karsten Wutzke

unread,
Aug 9, 2007, 9:12:47 AM8/9/07
to

Well I have written some code that attaches any custom Observer
interface to any java.util.Observable. The code will generate adapter
classes *on the fly* that will downcast from Observable to the custom
observer interface:

public class SelectionObserverAdapter implements java.util.Observer
{
private SelectionObserver so;

public SelectionObserverAdapter(SelectionObserver s)
{
if ( s==null )
{
throw new NullPointerException("Selection observer is null.");
}

so = s;
}

public void update(Observable obs, Object arg)
{
so.update((Selection)obs);
}

}

Any Observable will have some custom adapter attached to be notified
on update. I wanted to avoid the downcast for every Observer
implementation and use custom observers like

public interface SelectionObserver extends GenericObserver
{
public void update(Selection s);
}

public interface EmailConstraintsObserver extends GenericObserver
{
public void update(EmailConstraints ec);
}

...

The observing class then only has to implement the respective custom
interfaces. Note attaching the two is done via generating byte code,
if I would'nt do this, I'd not only have to create the concrete
Observer interface but also an adapter class, which is a maintenance
nightmare. Since it works so nicely without this listener stuff I was
looking for Observable compatible collections...

Side notes:

1. The adapter mainly acts as a "method multiplexer", it calls the
right method due to the downcast in the update method.
2. I know the object of interest in observer is Object arg, I wanted
to get around downcasting as much as possible.
3. The code will add n adapters for every n custom observers
implemented in the observing class, so Observable will do n updates.
As this is only intended for a few connections, the overhead of
calling notifyObserver n times is not (yet?) recognizable. This is
still much cleaner than using some if then else construct with
downcasts to determine the concrete object's class.
4. Of course, I used some naming conventions for the whole class
generation stuff...

Well..... this explanation has gotton much bigger than intended. I
hope that the basic idea has become clear. In any case this is getting
a little off-topic now - I just wanted to outline my motivation.

I'm not really a friend of Java's listener concept, can't explain why
really... ?-/

Karsten

0 new messages