code_junkie

Mandatory cloneable interface in Java

Nov 11th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. public interface Modifiable<T extends Modifiable<T>> extends Cloneable {
  2. T clone();
  3. }
  4.  
  5. public class Foo implements Modifiable<Foo> {
  6. public Foo clone() { //this is required
  7. return null; //todo: real work
  8. }
  9. }
  10.  
  11. public interface Modifiable<T extends Modifiable<T>> extends Cloneable {
  12. T clone();
  13. }
  14. public class Test implements Modifiable<Test> {
  15. @Override
  16. public Test clone() {
  17. System.out.println("clone");
  18. return null;
  19. }
  20. public static void main(String[] args) {
  21. Test t = new Test().clone();
  22. }
  23. }
  24.  
  25. public Object clone() throws CloneNotSupportedException {
  26. return super.clone();
  27. }
  28.  
  29. private LinkedList names = new LinkedList();
  30.  
  31.  
  32. public CloningExample() {
  33. names.add("Alex");
  34. names.add("Melody");
  35. names.add("Jeff");
  36. }
  37.  
  38.  
  39. public String toString() {
  40. StringBuffer sb = new StringBuffer();
  41. Iterator i = names.iterator();
  42. while (i.hasNext()) {
  43. sb.append("nt" + i.next());
  44. }
  45. return sb.toString();
  46. }
  47.  
  48.  
  49. public Object clone() {
  50. try {
  51. return super.clone();
  52. } catch (CloneNotSupportedException e) {
  53. throw new Error("This should not occur since we implement Cloneable");
  54. }
  55. }
  56.  
  57.  
  58. public Object deepClone() {
  59. try {
  60. CloningExample copy = (CloningExample)super.clone();
  61. copy.names = (LinkedList)names.clone();
  62. return copy;
  63. } catch (CloneNotSupportedException e) {
  64. throw new Error("This should not occur since we implement Cloneable");
  65. }
  66. }
  67.  
  68. public boolean equals(Object obj) {
  69.  
  70. /* is obj reference this object being compared */
  71. if (obj == this) {
  72. return true;
  73. }
  74.  
  75. /* is obj reference null */
  76. if (obj == null) {
  77. return false;
  78. }
  79.  
  80. /* Make sure references are of same type */
  81. if (!(this.getClass() == obj.getClass())) {
  82. return false;
  83. } else {
  84. CloningExample tmp = (CloningExample)obj;
  85. if (this.names == tmp.names) {
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91.  
  92. }
  93.  
  94.  
  95. public static void main(String[] args) {
  96.  
  97. CloningExample ce1 = new CloningExample();
  98. System.out.println("nCloningExample[1]n" +
  99. "-----------------" + ce1);
  100.  
  101. CloningExample ce2 = (CloningExample)ce1.clone();
  102. System.out.println("nCloningExample[2]n" +
  103. "-----------------" + ce2);
  104.  
  105. System.out.println("nCompare Shallow Copyn" +
  106. "--------------------n" +
  107. " ce1 == ce2 : " + (ce1 == ce2) + "n" +
  108. " ce1.equals(ce2) : " + ce1.equals(ce2));
  109.  
  110. CloningExample ce3 = (CloningExample)ce1.deepClone();
  111. System.out.println("nCompare Deep Copyn" +
  112. "--------------------n" +
  113. " ce1 == ce3 : " + (ce1 == ce3) + "n" +
  114. " ce1.equals(ce3) : " + ce1.equals(ce3));
  115.  
  116. System.out.println();
  117.  
  118. }
Add Comment
Please, Sign In to add comment