-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Immutable-Example.txt
More file actions
120 lines (91 loc) · 3.56 KB
/
Java_Immutable-Example.txt
File metadata and controls
120 lines (91 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Why Date object is mutable - java.util.Date is a lagecy class which is of mutable type by default. java.time, Instant and LocalDateTime guiranties in there spacification that they are immutable(Feature of java_8 Time API)
Why HeshMap Object is mutable - The Itrator interface of Collection is responsible for mutation thats why every class which gets itrated will be mutable
create immutable class :-
1. Make the class as final
2. Make all instance variable as private final
3. Remove Setter Method from class which one decide to be immutable
3. Create the Constructor as Private - to avoide unplaned construction of class
4. for mutable objects (Date, HeshMap etc.) use Deep Copying for Object initialization
5. Always return Object copy(Clone) via getter method to ensure original object should not be return
//create immutable class using mutable type of objects
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
public class FinalClassExample {
private final int id;
private final String name;
private final HashMap<String,String> testMap;
private final Date datecheck;
public int getId() {
return id;
}
public String getName() {
return name;
}
public HashMap<String, String> getTestMap() {
//Return the object copy
return (HashMap<String, String>) testMap.clone();
}
public Date getDate(){
//Return the object copy
return new Date(datecheck.getTime());
}
// Constructor performing Deep Copying
private FinalClassExample(int i, String n, HashMap<String,String> hm, Date date){
System.out.println("Performing Deep for Object initialization");
this.id=i;
this.name=n;
HashMap<String,String> tempMap=new HashMap<String,String>();
String key;
Iterator<String> it = hm.keySet().iterator();
while(it.hasNext()){
key=it.next();
tempMap.put(key, hm.get(key));
}
this.testMap=tempMap;
this.datecheck= new Date(date.getTime());
}
/**
* Constructor performing Shallow
*/
/**
public FinalClassExample(int i, String n, HashMap<String,String> hm){
System.out.println("Performing Shallow for Object initialization");
this.id=i;
this.name=n;
this.testMap=hm;
}
*/
/**
* To test the consequences of Shallow and how to avoid it with Deep for creating immutable classes
*/
public static void main(String[] args) {
HashMap<String, String> h1 = new HashMap<String,String>();
h1.put("1", "first");
h1.put("2", "second");
String s = "original";
int i=10;
FinalClassExample ce = new FinalClassExample(i,s,h1, new Date());
//Lets see whether its copy by field or reference
System.out.println(s==ce.getName());
System.out.println(h1 == ce.getTestMap());
//print the ce values
System.out.println("ce id:"+ce.getId());
System.out.println("ce name:"+ce.getName());
System.out.println("ce testMap:"+ce.getTestMap());
System.out.println("ce date:"+ce.getDate());
//change the local variable values
i=20;
s="modified";
h1.put("3", "third");
//print the values again
System.out.println("ce id after local variable change:"+ce.getId());
System.out.println("ce name after local variable change:"+ce.getName());
System.out.println("ce testMap after local variable change:"+ce.getTestMap());
HashMap<String, String> hmTest = ce.getTestMap();
hmTest.put("4", "new");
System.out.println("ce testMap after changing variable from accessor methods:"+ce.getTestMap());
}
}
Spring MVC with iText Library for invoice generation
Multithreading concept in depth