diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..1b673a0 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ab30aaa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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..b4e2350 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,78 @@ package com.scaler.lld.design.assignments.builder; +import lombok.Getter; + @WithBuilder +@Getter public class DatabaseConfigurationBuilder { + private String databaseUrl; + private String username; + private String password; + private int maxConnections; + private boolean enableCache; + private boolean isReadOnly; + + private DatabaseConfigurationBuilder(){ + } + + public static DBConfigBuilder builder(){ + return new DBConfigBuilder(); + } + + public static class DBConfigBuilder { + public String databaseUrl; + private String username; + private String password; + private int maxConnections; + private boolean enableCache; + private boolean isReadOnly; + + // Creating fluent interfaces for setter + public DBConfigBuilder setDatabaseUrl(String databaseUrl){ + this.databaseUrl = databaseUrl; + return this; + } + public DBConfigBuilder setUsername(String username){ + this.username = username; + return this; + } + public DBConfigBuilder setPassword(String password){ + this.password = password; + return this; + } + public DBConfigBuilder setMaxConnections(int maxConnections){ + this.maxConnections = maxConnections; + return this; + } + public DBConfigBuilder setEnableCache(boolean enableCache){ + this.enableCache = enableCache; + return this; + } + public DBConfigBuilder setIsReadOnly(boolean isReadOnly){ + this.isReadOnly = isReadOnly; + return this; + } + + public DatabaseConfigurationBuilder build(){ +// if (!isValid()){ +// throw new IllegalArgumentException("Ivalid Database Configuration"); +// } + DatabaseConfigurationBuilder databaseConfigurationBuilder = new DatabaseConfigurationBuilder(); + databaseConfigurationBuilder.databaseUrl = this.databaseUrl; + databaseConfigurationBuilder.username = this.username; + databaseConfigurationBuilder.password = this.password; + databaseConfigurationBuilder.maxConnections = this.maxConnections; + databaseConfigurationBuilder.enableCache = this.enableCache; + databaseConfigurationBuilder.isReadOnly = this.isReadOnly; + return databaseConfigurationBuilder; + } + +// public Boolean isValid(){ +// if (this.databaseUrl == null){ +// return false; +// } +// return true; +// } + } } \ 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..92a9880 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; @@ -46,4 +46,10 @@ public String getFontFamily() { public ConfigurationType getType() { return type; } + + @Override + public Object cloneObject() { + Configuration configuration = new Configuration(this.themeColor, this.autoSave, this.language, this.darkMode, this.fontSize, this.fontFamily, this.type); + return configuration; + } } \ No newline at end of file diff --git a/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationRegistry.java b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationRegistry.java new file mode 100644 index 0000000..c594e3a --- /dev/null +++ b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationRegistry.java @@ -0,0 +1,23 @@ +package com.scaler.lld.design.assignments.prototype; + +import java.util.HashMap; +import java.util.Map; + +public class ConfigurationRegistry implements ConfigurationPrototypeRegistry{ + private Map registy = new HashMap<>(); + @Override + public void addPrototype(Configuration user) { + registy.put(user.getType(), user); + } + + @Override + public Configuration getPrototype(ConfigurationType type) { + return registy.get(type); + } + + @Override + public Configuration clone(ConfigurationType type) { + Configuration config = registy.get(type); + return (Configuration) config.cloneObject(); + } +} 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..50d13ff 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 @@ -1,50 +1,80 @@ package com.scaler.lld.design.assignments.singleton; +import java.util.HashMap; +import java.util.Map; + public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager { + private static FileBasedConfigurationManager INSTANCE = null; +// public Map config = new HashMap<>(); +// public Map configDiffType = new HashMap<>(); + + private FileBasedConfigurationManagerImpl(){} + @Override public String getConfiguration(String key) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); +// // TODO Auto-generated method stub +// throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + String rvalue = null; + if (properties.containsKey(key)){ + rvalue = (String) properties.get(key); + } + return rvalue; } @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 rValue = null; + if (properties.containsKey(key)){ + String value = (String) properties.get(key); + rValue = (T) convert(value, type); + } + return rValue; } @Override public void setConfiguration(String key, String value) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); +// // TODO Auto-generated method stub +// throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + properties.put(key, value); } @Override public void setConfiguration(String key, T value) { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + } @Override public void removeConfiguration(String key) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'"); +// // TODO Auto-generated method stub +// throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'"); + properties.remove(key); } @Override public void clear() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'clear'"); +// // TODO Auto-generated method stub +// throw new UnsupportedOperationException("Unimplemented method 'clear'"); + properties.clear(); } public static FileBasedConfigurationManager getInstance() { - // TODO Auto-generated method stub - return null; + if (INSTANCE == null){ + synchronized (FileBasedConfigurationManagerImpl.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