SE450: Clone: Deep versus Shallow [4/32] Previous pageContentsNext page

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package clone.deepVshallow;
public class Main {
  public static void main(String[] argv) throws CloneNotSupportedException {
    Node w = new Node(1, new Node(2, new Node(3)));
    Node v = w;
    Node x = (Node) w.shallow_copy();
    Node y = (Node) w.deep_copy();
    Node z = (Node) w.shallow_clone();

    System.out.println("w==v: " + ((w == v) ? "true" : "false"));
    System.out.println("w==x: " + ((w == x) ? "true" : "false"));
    System.out.println("w==y: " + ((w == y) ? "true" : "false"));
    System.out.println("w==z: " + ((w == z) ? "true" : "false"));
    System.out.println("w.equals(v): " + ((w.equals(v)) ? "true" : "false"));
    System.out.println("w.equals(x): " + ((w.equals(x)) ? "true" : "false"));
    System.out.println("w.equals(y): " + ((w.equals(y)) ? "true" : "false"));
    System.out.println("w.equals(z): " + ((w.equals(z)) ? "true" : "false"));
    System.out.println("w.shallow_equals(v): " +
        ((w.shallow_equals(v)) ? "true" : "false"));
    System.out.println("w.shallow_equals(x): " +
        ((w.shallow_equals(x)) ? "true" : "false"));
    System.out.println("w.shallow_equals(y): " +
        ((w.shallow_equals(y)) ? "true" : "false"));
    System.out.println("w.shallow_equals(z): " +
        ((w.shallow_equals(z)) ? "true" : "false"));
    System.out.println("w.deep_equals(v): " +
        ((w.deep_equals(v)) ? "true" : "false"));
    System.out.println("w.deep_equals(x): " +
        ((w.deep_equals(x)) ? "true" : "false"));
    System.out.println("w.deep_equals(y): " +
        ((w.deep_equals(y)) ? "true" : "false"));
    System.out.println("w.deep_equals(z): " +
        ((w.deep_equals(z)) ? "true" : "false"));
  }
}

file:Node.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package clone.deepVshallow;
class Node implements Cloneable {
  private int i;
  private Node next;

  public Node(int i, Node next) { this.i = i; this.next = next; }
  public Node(int i)            { this(i,null); }

  public Object shallow_copy() {
    return new Node(i, next);
  }

  public Object shallow_clone() throws CloneNotSupportedException {
    return super.clone();
  }

  public boolean shallow_equals(Object o) {
    if (!(this.getClass().equals(o.getClass())))
      return false;
    Node that = (Node) o;
    return (i == that.i) && (next == that.next);
  }

  public Object deep_copy() {
    Node next_copy = (next==null) ? null : (Node) next.deep_copy();
    return new Node(i, next_copy);
  }

  public Object deep_clone() throws CloneNotSupportedException {
    Node result = (Node) super.clone();
    result.next = (next==null) ? null : (Node) next.deep_clone();
    return result;
  }

  public boolean deep_equals(Object o) {
    if (!(this.getClass().equals(o.getClass())))
      return false;
    Node that = (Node) o;
    return (i == that.i)
        && ((next==null) ? (that.next==null) : next.deep_equals(that.next));
  }
}

Previous pageContentsNext page