Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I genericly clone() objects?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I'm trying to write a function that takes a collection of generic types and copies (i.e. clones) each element of the collection into a new collection. The problem is, clone() is protected and the Cloneable interface doesn't even make it public... I was thinking of using something like this, but of course the compiler doesn't like the call to clone().
Code:
public static <T extends Cloneable>
void Copy( Collection<T> lhs, Collection<T> rhs )
{
   lhs.clear();
   Iterator<T> it = rhs.iterator();

   while ( it.hasNext() == true )
   {
      lhs.add( it.next().[b]clone()[/b] );
   }
}
How can I create a generic function that only accepts collections where T.clone() is public?

Also, what's the point of Cloneable? If it doesn't define clone() as public, what's the point of implementing it?
 
I don't think the final keyword really helps much on function parameters. If they ever implemented the const keyword, that would be worth using in many places.
final just means that the reference can't change, but the object itself can change state using its Set() methods...

That's really what I was referring to - the strings and other variables inside my User class can get changed by calling some of the User.Set() functions.
 
final is crucial (and required) on method parameters if they are then used within anonymous implementations in the method.

Tim
 
Strings are immutable, you cannot change its state with set methods.

You should desing immutable objects if you don't want its state to be changed: what you're trying to do with clone should be done with OOP desing

Java immutable objects

Cheers,
Dian
 
String is immutable because it has no 'set' methods, or any other accessible methods (overridable or otherwise) which will alter its internal state. There is no magic keyword to make instances of a class immutable.

cpjust, where are we at with all this? Are you any nearer to your requirement?

Tim
 
Diancecht said:
Strings are immutable, you cannot change its state with set methods.
I meant my own Set methods:
Code:
public class User
{
   private String  m_UserID;

   public void SetUserID( String user )
   {
      m_UserID = user;
   }

   public String GetUserID()
   {
      return m_UserID;
   }
}
I can't make my object immutable because I need to be able to change its state during my tests (this is for a QA test program BTW) and part of the state that I'm trying to make sure hasn't changed is the user account on the mainframe which could become Locked out/Unlocked, Revoked/Resumed, Password changed/Expired... by the operations that I'm performing. I'm pretty sure the state shouldn't change, but I'd rather be safe than sorry. Plus if someone else modifies the code in the future, this will tell them if they added a bug.

timw said:
where are we at with all this? Are you any nearer to your requirement?
Oh, I fixed it a while ago, but then we got onto this immutable String thing... ;-)
 
I personally don't like the idea of adding code to test or to make tests. If you just want to perform tests, save the state to a file or any other structure and then compare the results.

Cheers,
Dian
 
That's basically doing the same thing (i.e. making a copy of the object), except it's saving the data in a different data structure.
If I already have a data structure with all the attributes I need, why create a new one which could get out of sync with the rest of the code if it isn't maintained properly?
I prefer code reuse rather than code duplication.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top