J
Joris Bleys
I want to create a container (SetList) which is capable of containing all
possiblie elements, but I ran into some problems implementing clone(). I
believe it should be a deep cloner (causing me lots of headaches).
Snippet from Thinking in Java (controlling cloneability):
Support cloning conditionally. If your class holds handles to other
objects that might or might not be cloneable (an example of this is a
collection class), you can try to clone all of the objects that you have
handles to as part of your cloning, and if they throw exceptions just
pass them through. For example, consider a special sort of Vector that
tries to clone all the objects it holds. When you write such a Vector,
you donÿt know what sort of objects the client programmer might put
into your Vector, so you donÿt know whether they can be cloned.
Does anyone how someone can actually implement this ?
public SetList duplicate() {
SetList duplicateSetList = new SetList();
Iterator i = this.iterator();
while (i.hasNext()) {
Object o = i.next();
// downcast needed
duplicateSetList.add(o.clone());
}
return duplicateSetList;
}
Because Object.clone() is protected, a downcast is needed (which I believe
has to be quite dynamic).
Any ideas ?
possiblie elements, but I ran into some problems implementing clone(). I
believe it should be a deep cloner (causing me lots of headaches).
Snippet from Thinking in Java (controlling cloneability):
Support cloning conditionally. If your class holds handles to other
objects that might or might not be cloneable (an example of this is a
collection class), you can try to clone all of the objects that you have
handles to as part of your cloning, and if they throw exceptions just
pass them through. For example, consider a special sort of Vector that
tries to clone all the objects it holds. When you write such a Vector,
you donÿt know what sort of objects the client programmer might put
into your Vector, so you donÿt know whether they can be cloned.
Does anyone how someone can actually implement this ?
public SetList duplicate() {
SetList duplicateSetList = new SetList();
Iterator i = this.iterator();
while (i.hasNext()) {
Object o = i.next();
// downcast needed
duplicateSetList.add(o.clone());
}
return duplicateSetList;
}
Because Object.clone() is protected, a downcast is needed (which I believe
has to be quite dynamic).
Any ideas ?