Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Observable collections (set, list, map)

Thread view: 
Karsten Wutzke - 09 Aug 2007 11:01 GMT
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 - 09 Aug 2007 11:23 GMT
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

> Hello!
>
[quoted text clipped - 26 lines]
>
> Karsten
inquisitive - 09 Aug 2007 11:26 GMT
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

> Hi Karsten
> I am not sure if i came accross any such collection.
[quoted text clipped - 34 lines]
>
> - Show quoted text -
Karsten Wutzke - 09 Aug 2007 11:34 GMT
> u can think of making a class that
>
[quoted text clipped - 44 lines]
>
> > - Show quoted text -

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 - 09 Aug 2007 11:51 GMT
hmmmmm
try
http://commons.apache.org/sandbox/events/apidocs/org/apache/commons/events/obser
vable/ObservableCollection.html

but it has only list
http://commons.apache.org/sandbox/events/apidocs/org/apache/commons/events/obser
vable/ObservableList.html

and Set
http://commons.apache.org/sandbox/events/apidocs/org/apache/commons/events/obser
vable/ObservableSet.html


cheerz !!

> > u can think of making a class that
>
[quoted text clipped - 65 lines]
>
> - Show quoted text -
Karsten Wutzke - 09 Aug 2007 13:36 GMT
> 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
Thomas Hawtin - 09 Aug 2007 12:08 GMT
> 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 - 09 Aug 2007 14:12 GMT
> > Does anyone know of a collection package/API that extends
> > java.util.Observable collection classes such as Set, List and Map?
[quoted text clipped - 8 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.