-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49.DesignAConfigurationManager.java
More file actions
154 lines (128 loc) · 4.73 KB
/
49.DesignAConfigurationManager.java
File metadata and controls
154 lines (128 loc) · 4.73 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
/*
============================= UML ================================
+---------------------------+
| ConfigurationManager |
+---------------------------+
| - instance |
| - configStore : Map |
| - versions : List<Map> |
| - lock : ReentrantLock |
+---------------------------+
| + get(key) |
| + set(key, value) |
| + delete(key) |
| + rollback(version) |
| + exportConfig() |
+---------------------------+
*/
class ConfigurationManager {
// Singleton instance (only one object)
private static ConfigurationManager instance;
// Stores current configuration key–value pairs
private Map<String, Object> configStore;
// Holds previous snapshots (mementos)
private List<Map<String, Object>> versions;
// Lock for thread-safety during writes
private ReentrantLock lock;
// Private constructor → ensures Singleton
private ConfigurationManager() {
configStore = new HashMap<>();
versions = new ArrayList<>();
lock = new ReentrantLock();
}
// Public accessor for Singleton object
public static synchronized ConfigurationManager getInstance() {
if (instance == null) {
instance = new ConfigurationManager(); // create instance once
}
return instance;
}
// Read a configuration value
public Object get(String key) {
return configStore.get(key);
}
// Set or update a configuration key
public void set(String key, Object value) {
lock.lock(); // lock for thread-safe write
try {
configStore.put(key, value);
saveVersion(); // create new version snapshot
} finally {
lock.unlock(); // release lock
}
}
// Delete a key from config
public void delete(String key) {
lock.lock();
try {
if (configStore.containsKey(key)) {
configStore.remove(key);
saveVersion();
}
} finally {
lock.unlock();
}
}
// Create a deep snapshot (memento)
private void saveVersion() {
versions.add(new HashMap<>(configStore)); // shallow copy is enough
}
// Rollback to a previous version index
public void rollback(int versionIndex) {
lock.lock();
try {
if (versionIndex >= 0 && versionIndex < versions.size()) {
configStore = new HashMap<>(versions.get(versionIndex));
saveVersion(); // save rollback state as new version
}
} finally {
lock.unlock();
}
}
// Export entire configuration as copy
public Map<String, Object> exportConfig() {
return new HashMap<>(configStore); // return snapshot
}
// Display all versions (for demo)
public void printVersions() {
for (int i = 0; i < versions.size(); i++) {
System.out.println("Version " + i + ": " + versions.get(i));
}
}
}
/* ======================= DEMO EXECUTION ========================== */
public class Main {
public static void main(String[] args) {
// Grab Singleton instance
ConfigurationManager mgr = ConfigurationManager.getInstance();
// Set initial configs
mgr.set("theme", "dark"); // version 0
mgr.set("timeout", 30); // version 1
mgr.set("volume", 80); // version 2
// Print all versions so far
mgr.printVersions();
// Update theme to create version 3
mgr.set("theme", "light");
System.out.println("\nAfter new theme:");
mgr.printVersions();
// Rollback to version 1
mgr.rollback(1);
System.out.println("\nAfter rollback to version 1:");
System.out.println(mgr.exportConfig());
}
}