Skip to content

Instantly share code, notes, and snippets.

@SumitJainUTD
Created May 20, 2020 03:52
Show Gist options
  • Save SumitJainUTD/a9db8663d9a39deb2aaebe097501020a to your computer and use it in GitHub Desktop.
Save SumitJainUTD/a9db8663d9a39deb2aaebe097501020a to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
public class VersionControlMapSystem {
private static Integer currentVersion = null;
private static VersionControlMapSystem versionControlMapSystem;
private Map<Integer, Map<String, String>> controlMap = new HashMap<>();
private VersionControlMapSystem(){
System.out.println("Initializing Version Control Map System");
}
public static VersionControlMapSystem getInstance(){
if(versionControlMapSystem==null) {
versionControlMapSystem = new VersionControlMapSystem();
}
return versionControlMapSystem;
}
public void put(String key, String value){
if(currentVersion==null) {
currentVersion = 0;
Map<String, String> valueMap = new HashMap<>();
controlMap.put(currentVersion, valueMap);
}
controlMap.get(currentVersion).put(key, value);
}
public String get(String key){
return controlMap.get(currentVersion).get(key);
}
public int getCurrentVersion(){
return currentVersion;
}
public void snapshot(){
int oldVersion = currentVersion;
currentVersion++;
//copy the valueMap to the new version
Map<String, String> old_map = controlMap.get(oldVersion);
Map<String, String> newMap = new HashMap<>();
for(String key: old_map.keySet())
newMap.put(key, old_map.get(key));
controlMap.put(currentVersion, newMap);
}
public String getValVersion(int version, String key){
if(controlMap.containsKey(version)){
return controlMap.get(version).get(key);
} else {
System.out.println("Invalid version");
return null;
}
}
public static void main(String[] args) {
VersionControlMapSystem vcm = VersionControlMapSystem.getInstance();
vcm.put("memory", "16GB");
vcm.put("os_version", "10");
vcm.put("graphics", "intel_10_153");
vcm.snapshot();
vcm.put("os_version", "11");
vcm.put("graphics", "intel_11_153");
System.out.println("graphics in current version: " + vcm.get("graphics"));
System.out.println("Current Control Map version is : " + vcm.getCurrentVersion());
System.out.println("graphics in version 0 : " + vcm.getValVersion(0, "graphics"));
System.out.println("os_version in current version: " + vcm.get("os_version"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment