Cloning Question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 9
Reputation: Metsfan147 is an unknown quantity at this point 
Solved Threads: 0
Metsfan147's Avatar
Metsfan147 Metsfan147 is offline Offline
Newbie Poster

Cloning Question

 
0
  #1
Dec 11th, 2006
I'm trying to figure out implementing the clone() method of Object. I have a class with a few data memebers that I thought I'd try it out on - a node class for a linked list.

So, here's the class:

  1. public class HashListNode implements Cloneable
  2. {
  3.  
  4. private String data;
  5. private HashListNode nextNode;
  6.  
  7. /* other methods and such */
  8.  
  9. public Object clone() throws CloneNotSupportedException
  10. {
  11. final HashListNode theClone = (HashListNode)super.clone();
  12. if( this.nextNode != null)
  13. {
  14. theClone.nextNode = (HashListNode)this.nextNode.clone();
  15. }
  16.  
  17. return theClone;
  18. }
  19. }

So, it seems that what's happening is when I call super.clone(), I'm calling Object's clone method, correct? What I don't understand, is will that method be able to deal with the String?

I can't seem to figure out if I have the hang of writing clone methods.

I am aware that it might not be too important to write one for this class, it just happened to be a simple class that was written recently.

Thanks for any help.
More systems have been wiped out by admins than any cracker could do in a lifetime.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Cloning Question

 
0
  #2
Dec 12th, 2006
In this particular case it probably wouldn't make any difference as String is immutable.
If a member isn't and doesn't implement Cloneable you'll have to explicitly make a deep copy yourself.

There might be more to it, I've not used Cloneable extensively myself, but that's how I understand it to work.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 9
Reputation: Metsfan147 is an unknown quantity at this point 
Solved Threads: 0
Metsfan147's Avatar
Metsfan147 Metsfan147 is offline Offline
Newbie Poster

Re: Cloning Question

 
0
  #3
Dec 13th, 2006
Originally Posted by jwenting View Post
In this particular case it probably wouldn't make any difference as String is immutable.
If a member isn't and doesn't implement Cloneable you'll have to explicitly make a deep copy yourself.

There might be more to it, I've not used Cloneable extensively myself, but that's how I understand it to work.
The code I posted is not thread safe, correct? Should it be made so and how would I go about doing that?
More systems have been wiped out by admins than any cracker could do in a lifetime.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Cloning Question

 
0
  #4
Dec 13th, 2006
Thread safety isn't something of a single method, it's something for the entire class.
Without knowing the code for the rest of the class, it's impossible to tell whether this one is threadsafe or not.
It might be, but most likely it isn't. Just putting a "synchronized" keyword here and there isn't going to change that however, you'll need to make sure no update to one data member can happen while you're working on another one.
For example you have to ensure that noone changes that String while you're cloning the HashListNode (which also has to have thread safety in mind).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 9
Reputation: Metsfan147 is an unknown quantity at this point 
Solved Threads: 0
Metsfan147's Avatar
Metsfan147 Metsfan147 is offline Offline
Newbie Poster

Re: Cloning Question

 
0
  #5
Dec 13th, 2006
This is the entire class. I do think that it is close to being thread safe. Not that this has to be. This is just me trying to learn about thread safety. How close am I?

  1. //GPL is here. waste of forum space though.
  2.  
  3. /**
  4. *A single element in a linked list specifically designed
  5. *for hash tables
  6. *of strings.
  7. *
  8. *@author Metsfan147
  9. *@version 1.0
  10. */
  11. public class HashListNode implements Cloneable
  12. {
  13.  
  14. private String data;
  15. private HashListNode nextNode;
  16.  
  17. /**
  18.   *Constructor taking a string argument, setting the
  19.   *node data to that string.
  20.   *@param x The string to set the node to represent.
  21.   */
  22. public HashListNode(String x)
  23. {
  24. this.data = x;
  25. this.nextNode = null;
  26. }
  27.  
  28. /**
  29.   *Sets the pointer nextNode to the next node in the linked list.
  30.   *@param next The HashListNode that this one should point to.
  31.   */
  32. public void setNext(HashListNode next)
  33. {
  34. synchronized( this )
  35. {
  36. this.nextNode = next;
  37. }
  38. }
  39.  
  40. /**
  41.   *Accessor method that returns a pointer to the next node in the
  42.   *linked list.
  43.   *@return nextElement The next node in the linked list;
  44.   */
  45. public HashListNode getNext()
  46. {
  47. return this.nextNode;
  48. }
  49.  
  50. /**
  51.   *Changes the value of the String that this node holds.
  52.   *@param newString The string that the node should now represent.
  53.   */
  54. public void setData(String newString)
  55. {
  56. synchronized( this )
  57. {
  58. this.data = newString;
  59. }
  60. }
  61.  
  62. /**
  63.   *Returns the string that this node represents.
  64.   *@return data The string contained in this node.
  65.   */
  66. public String getData()
  67. {
  68. return this.data;
  69. }
  70.  
  71. /**
  72.   *Indicates wheter another HashListNode is 'equal' to this one.
  73.   *@param object The object to check this one against.
  74.   *@return Whether this node is equal to node by their String data.
  75.   */
  76. public boolean equals( Object object )
  77. {
  78. boolean result = false;
  79.  
  80. if( this == object )
  81. {
  82. result = true;
  83. }
  84.  
  85. else if( object == null || this.getClass() != object.getClass() )
  86. {
  87. result = false;
  88. }
  89.  
  90. else
  91. {
  92. final HashListNode other = (HashListNode) object;
  93. result = ( this.getData().equals(other.getData() ) );
  94. }
  95.  
  96. return result;
  97. }
  98.  
  99. /**
  100.   * Returns a hash code value for the node.
  101.   *@return The object's hash code value.
  102.   */
  103. public int hashCode()
  104. {
  105. final int prime = 31;
  106. int result = 7;
  107. result = prime * result + ( null == data ? 0 : data.hashCode() );
  108. result = prime * result + ( null == nextNode ? 0 : nextNode.hashCode());
  109. return result;
  110. }
  111.  
  112. /**
  113.   *Creates and returns a copy of this object.
  114.   *@return A clone of this object.
  115.   *@throws CloneNotSupportedException if the clone interface
  116.   *is not supported.
  117.   */
  118. public Object clone() throws CloneNotSupportedException
  119. {
  120. final HashListNode theClone = (HashListNode) super.clone();
  121. if( this.nextNode != null)
  122. {
  123. theClone.nextNode = (HashListNode) this.data.clone();
  124. }
  125.  
  126. return theClone;
  127. }
  128.  
  129. }
Last edited by Metsfan147; Dec 13th, 2006 at 5:10 pm.
More systems have been wiped out by admins than any cracker could do in a lifetime.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Cloning Question

 
0
  #6
Dec 14th, 2006
Not even halfway there.
Different threads can still access data members at the same time, even if they can't change them at the same time.

One thread can call setData while another calls getData for example, potentially getting corrupted results.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 9
Reputation: Metsfan147 is an unknown quantity at this point 
Solved Threads: 0
Metsfan147's Avatar
Metsfan147 Metsfan147 is offline Offline
Newbie Poster

Re: Cloning Question

 
0
  #7
Dec 14th, 2006
I think it's time to pick up the book "Java Threads"
More systems have been wiped out by admins than any cracker could do in a lifetime.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC