diff --git a/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java b/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java index 592aa32..807f7b9 100644 --- a/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java +++ b/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java @@ -1,6 +1,105 @@ package com.scaler.lld.design.assignments.builder; @WithBuilder -public class DatabaseConfigurationBuilder { +public class DatabaseConfigurationBuilder { + + // NO SETTER IN OUTER CLASS + + private String databaseUrl; + private String username; + private String password; + private int maxConnections; + private boolean enableCache; + private boolean isReadOnly; + + public String getDatabaseUrl() { + return databaseUrl; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public int getMaxConnections() { + return maxConnections; + } + + public boolean isEnableCache() { + return enableCache; + } + + public boolean isReadOnly() { + return isReadOnly; + } + + public static Builder builder() + { + return new Builder(); + } + + + + + public static class Builder + { + private DatabaseConfigurationBuilder obj; + + public Builder() { + obj = new DatabaseConfigurationBuilder(); + } + + public Builder setDatabaseUrl(String databaseUrl) { + this.obj.databaseUrl = databaseUrl; + return this; + } + + public Builder setUsername(String username) { + this.obj.username = username; + return this; + } + + public Builder setPassword(String password) { + this.obj.password = password; + return this; + + } + + public Builder setMaxConnections(int maxConnections) { + this.obj.maxConnections = maxConnections; + return this; + } + + public Builder setEnableCache(boolean enableCache) { + this.obj.enableCache = enableCache; + return this; + + } + + public Builder setReadOnly(boolean isReadOnly) { + this.obj.isReadOnly = isReadOnly; + return this; + } + + public DatabaseConfigurationBuilder build(){ + DatabaseConfigurationBuilder db=new DatabaseConfigurationBuilder(); + db.databaseUrl=this.obj.databaseUrl; + db.enableCache=this.obj.enableCache; + db.isReadOnly=this.obj.isReadOnly; + db.maxConnections=this.obj.maxConnections; + db.password=this.obj.password; + db.username=this.obj.username; + return db; + } + + + + + + + } } \ No newline at end of file diff --git a/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java b/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java index 597f36e..3234452 100644 --- a/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java +++ b/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java @@ -1,6 +1,6 @@ package com.scaler.lld.design.assignments.prototype; -public class Configuration { +public class Configuration implements ClonableObject{ private String themeColor; private Boolean autoSave; private String language; @@ -19,6 +19,27 @@ public Configuration(String themeColor, Boolean autoSave, String language, Boole this.type = type; } + public Configuration() + { + + } + public Configuration(Configuration c) + { + + this.themeColor=c.themeColor; + this.autoSave = c.autoSave; + this.language = c.language; + this.darkMode = c.darkMode; + this.fontSize = c.fontSize; + this.fontFamily = c.fontFamily; + this.type = c.type; + + } + @Override + public T cloneObject() + { + return (T)(new Configuration(this)); + } public String getThemeColor() { return themeColor; } diff --git a/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java new file mode 100644 index 0000000..f8ad6f4 --- /dev/null +++ b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java @@ -0,0 +1,35 @@ +package com.scaler.lld.design.assignments.prototype; +import java.util.HashMap; +import java.util.Map; +public class ConfigurationPrototypeRegistryImpl implements ConfigurationPrototypeRegistry{ + + Map map; + public ConfigurationPrototypeRegistryImpl() + { + map=new HashMap<>(); + } + @Override + public void addPrototype(Configuration user) { + + map.put(user.getType(),user); + + + } + + @Override + public Configuration getPrototype(ConfigurationType type) { + return map.get(type); + } + + @Override + public Configuration clone(ConfigurationType type) { + + Configuration obj=map.getOrDefault(type,null); + + if(obj!=null) + return (Configuration) obj.cloneObject(); + + return null; + + } +} diff --git a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java index 15470fc..564143c 100644 --- a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java +++ b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java @@ -2,49 +2,78 @@ public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager { + private static FileBasedConfigurationManager INSTANCE=null; + + private FileBasedConfigurationManagerImpl() + { + + } + @Override public String getConfiguration(String key) { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + //throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + return this.properties.getProperty(key); } @Override public T getConfiguration(String key, Class type) { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + //throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + T ans=null; + String val=this.properties.getProperty(key); + if(val!=null) + ans=convert(val,type); + return ans; } @Override public void setConfiguration(String key, String value) { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + //throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + this.properties.setProperty(key,value); } @Override public void setConfiguration(String key, T value) { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + //throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + this.properties.setProperty(key,(value+"")); } @Override public void removeConfiguration(String key) { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'"); + //throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'"); + + this.properties.remove(key); } @Override public void clear() { // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'clear'"); + //throw new UnsupportedOperationException("Unimplemented method 'clear'"); + this.properties.clear(); } public static FileBasedConfigurationManager getInstance() { // TODO Auto-generated method stub - return null; + if(INSTANCE==null) + { + synchronized(FileBasedConfigurationManager.class) + { + if(INSTANCE==null) + { + INSTANCE=new FileBasedConfigurationManagerImpl(); + } + } + } + return INSTANCE; } public static void resetInstance() { // TODO Auto-generated method stub + INSTANCE=null; } } \ No newline at end of file