Observable collections (set, list, map)

K

Karsten Wutzke

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
 
I

inquisitive

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
 
I

inquisitive

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
 
K

Karsten Wutzke

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

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
 
T

Thomas Hawtin

Karsten said:
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
 
K

Karsten Wutzke

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

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
 
K

Karsten Wutzke

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top