944,087 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1509
  • Java RSS
Dec 11th, 2006
0

Cloning Question

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Metsfan147 is offline Offline
9 posts
since Dec 2006
Dec 12th, 2006
0

Re: Cloning Question

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 13th, 2006
0

Re: Cloning Question

Click to Expand / Collapse  Quote originally posted by jwenting ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Metsfan147 is offline Offline
9 posts
since Dec 2006
Dec 13th, 2006
0

Re: Cloning Question

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 13th, 2006
0

Re: Cloning Question

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?

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Metsfan147 is offline Offline
9 posts
since Dec 2006
Dec 14th, 2006
0

Re: Cloning Question

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 14th, 2006
0

Re: Cloning Question

I think it's time to pick up the book "Java Threads"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Metsfan147 is offline Offline
9 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Where to find COM libraries
Next Thread in Java Forum Timeline: EOL is not working and uncheck or unsafe operation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC