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 / October 2005

Tip: Looking for answers? Try searching our database.

Cloning a Cloneable Map

Thread view: 
Ian Pilcher - 06 Oct 2005 21:23 GMT
Given a Map that is Cloneable, is it possible to actually clone the damn
thing without resorting to reflection?

TIA

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Thomas Hawtin - 06 Oct 2005 21:47 GMT
> Given a Map that is Cloneable, is it possible to actually clone the damn
> thing without resorting to reflection?

In general, no. Cloneable has no public methods. However a specific
subtype (say java.util.HashMap), may make the clone method public.

Alternatively, you could just copy it.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Eric Sosman - 06 Oct 2005 21:57 GMT
Ian Pilcher wrote On 10/06/05 16:23,:
> Given a Map that is Cloneable, is it possible to actually clone the damn
> thing without resorting to reflection?

   Um, er, have you tried the clone() method?  ;-)

   You can't invoke clone() through a reference of type
Map or AbstractMap, because they don't implement Cloneable.
But if you've got a HashMap reference, say, you can use
clone() on it with no trouble.

   (Ian surely knows all this, so what's his difficulty?
Hmmm...)

   Maybe you've got a Map or AbstractMap reference and
you want to duplicate it.  You probably use instanceof
to discover that it implements Cloneable, and then you
run into the problem of needing to discover a more specific
class you can downcast it to in order to make the clone()
call.  Okay, the only way I can imagine doing that in full
generality is to use reflection.

   But you probably have some kind of fallback procedure
in case the Map turns out not to be Cloneable, and unless
that procedure is just "I can't do that, Dave" it would
presumably work on a Cloneable Map just as well as on a
non-Cloneable one.  Why not just use the fallback for all
Maps, and not bother with clone() at all?  Efficiency,
maybe?  If so, have you measured the performance hit?  If
you *have* measured an unacceptable performance hit and
you're dead-set on using clone() when possible, why so
reluctant to use reflection if that's what's needed?

   Maybe a little more background on what you're trying
to do with this Map would elicit some better ideas ...

Signature

Eric.Sosman@sun.com

Ian Pilcher - 06 Oct 2005 22:13 GMT
>     Maybe you've got a Map or AbstractMap reference and
> you want to duplicate it.  You probably use instanceof
[quoted text clipped - 3 lines]
> call.  Okay, the only way I can imagine doing that in full
> generality is to use reflection.

You are correct.

I am working on a "TransactionalMap" that will wrap a Map and allow
it to be "rolled back" to a previous state.  In the future, I may
track invididual updates, but it seemed like it would be simpler to
simply clone the underlying map the first time it is modified during a
transaction.

I'd very much like to create a general wrapper, rather than a
TransactionalHashMap, TransactionalTreeMap, TransactionalLinkedHashMap,
etc., etc.

>     But you probably have some kind of fallback procedure
> in case the Map turns out not to be Cloneable, and unless
[quoted text clipped - 6 lines]
> you're dead-set on using clone() when possible, why so
> reluctant to use reflection if that's what's needed?

No fallback procedure.  I check the underlying map in my constructor and
throw an IllegalArgumentException if it's not Cloneable.

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================



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



©2008 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.