diff --git a/caching/src/main/java/com/iluwatar/caching/App.java b/caching/src/main/java/com/iluwatar/caching/App.java
index 631d23a2a..2c7cbebf7 100644
--- a/caching/src/main/java/com/iluwatar/caching/App.java
+++ b/caching/src/main/java/com/iluwatar/caching/App.java
@@ -54,155 +54,155 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
public class App {
- /**
- * Constant parameter name to use mongoDB.
- */
- private static final String USE_MONGO_DB = "--mongo";
- /**
- * Application manager.
- */
- private final AppManager appManager;
+ /**
+ * Constant parameter name to use mongoDB.
+ */
+ private static final String USE_MONGO_DB = "--mongo";
+ /**
+ * Application manager.
+ */
+ private final AppManager appManager;
- /**
- * Constructor of current App.
- * @param isMongo boolean
- */
- public App(final boolean isMongo) {
- DbManager dbManager = DbManagerFactory.initDb(isMongo);
- appManager = new AppManager(dbManager);
- appManager.initDb();
+ /**
+ * Constructor of current App.
+ * @param isMongo boolean
+ */
+ public App(final boolean isMongo) {
+ DbManager dbManager = DbManagerFactory.initDb(isMongo);
+ appManager = new AppManager(dbManager);
+ appManager.initDb();
+ }
+
+ /**
+ * Program entry point.
+ *
+ * @param args command line args
+ */
+ public static void main(final String[] args) {
+ // VirtualDB (instead of MongoDB) was used in running the JUnit tests
+ // and the App class to avoid Maven compilation errors. Set flag to
+ // true to run the tests with MongoDB (provided that MongoDB is
+ // installed and socket connection is open).
+ App app = new App(isDbMongo(args));
+ app.useReadAndWriteThroughStrategy();
+ String splitLine = "==============================================";
+ LOGGER.info(splitLine);
+ app.useReadThroughAndWriteAroundStrategy();
+ LOGGER.info(splitLine);
+ app.useReadThroughAndWriteBehindStrategy();
+ LOGGER.info(splitLine);
+ app.useCacheAsideStategy();
+ LOGGER.info(splitLine);
+ }
+
+ /**
+ * Check the input parameters. if
+ * @param args input params
+ * @return true if there is "--mongo" parameter in arguments
+ */
+ private static boolean isDbMongo(final String[] args) {
+ for (String arg : args) {
+ if (arg.equals(USE_MONGO_DB)) {
+ return true;
+ }
}
+ return false;
+ }
- /**
- * Program entry point.
- *
- * @param args command line args
- */
- public static void main(final String[] args) {
- // VirtualDB (instead of MongoDB) was used in running the JUnit tests
- // and the App class to avoid Maven compilation errors. Set flag to
- // true to run the tests with MongoDB (provided that MongoDB is
- // installed and socket connection is open).
- App app = new App(isDbMongo(args));
- app.useReadAndWriteThroughStrategy();
- String splitLine = "==============================================";
- LOGGER.info(splitLine);
- app.useReadThroughAndWriteAroundStrategy();
- LOGGER.info(splitLine);
- app.useReadThroughAndWriteBehindStrategy();
- LOGGER.info(splitLine);
- app.useCacheAsideStategy();
- LOGGER.info(splitLine);
- }
+ /**
+ * Read-through and write-through.
+ */
+ public void useReadAndWriteThroughStrategy() {
+ LOGGER.info("# CachingPolicy.THROUGH");
+ appManager.initCachingPolicy(CachingPolicy.THROUGH);
- /**
- * Check the input parameters. if
- * @param args input params
- * @return true if there is "--mongo" parameter in arguments
- */
- private static boolean isDbMongo(final String[] args) {
- for (String arg : args) {
- if (arg.equals(USE_MONGO_DB)) {
- return true;
- }
- }
- return false;
- }
+ var userAccount1 = new UserAccount("001", "John", "He is a boy.");
- /**
- * Read-through and write-through.
- */
- public void useReadAndWriteThroughStrategy() {
- LOGGER.info("# CachingPolicy.THROUGH");
- appManager.initCachingPolicy(CachingPolicy.THROUGH);
+ appManager.save(userAccount1);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("001");
+ appManager.find("001");
+ }
- var userAccount1 = new UserAccount("001", "John", "He is a boy.");
+ /**
+ * Read-through and write-around.
+ */
+ public void useReadThroughAndWriteAroundStrategy() {
+ LOGGER.info("# CachingPolicy.AROUND");
+ appManager.initCachingPolicy(CachingPolicy.AROUND);
- appManager.save(userAccount1);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("001");
- appManager.find("001");
- }
+ var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
- /**
- * Read-through and write-around.
- */
- public void useReadThroughAndWriteAroundStrategy() {
- LOGGER.info("# CachingPolicy.AROUND");
- appManager.initCachingPolicy(CachingPolicy.AROUND);
+ appManager.save(userAccount2);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ LOGGER.info(appManager.printCacheContent());
+ userAccount2 = appManager.find("002");
+ userAccount2.setUserName("Jane G.");
+ appManager.save(userAccount2);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ }
- var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
+ /**
+ * Read-through and write-behind.
+ */
+ public void useReadThroughAndWriteBehindStrategy() {
+ LOGGER.info("# CachingPolicy.BEHIND");
+ appManager.initCachingPolicy(CachingPolicy.BEHIND);
- appManager.save(userAccount2);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- LOGGER.info(appManager.printCacheContent());
- userAccount2 = appManager.find("002");
- userAccount2.setUserName("Jane G.");
- appManager.save(userAccount2);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- }
+ var userAccount3 = new UserAccount("003",
+ "Adam",
+ "He likes food.");
+ var userAccount4 = new UserAccount("004",
+ "Rita",
+ "She hates cats.");
+ var userAccount5 = new UserAccount("005",
+ "Isaac",
+ "He is allergic to mustard.");
- /**
- * Read-through and write-behind.
- */
- public void useReadThroughAndWriteBehindStrategy() {
- LOGGER.info("# CachingPolicy.BEHIND");
- appManager.initCachingPolicy(CachingPolicy.BEHIND);
+ appManager.save(userAccount3);
+ appManager.save(userAccount4);
+ appManager.save(userAccount5);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("003");
+ LOGGER.info(appManager.printCacheContent());
+ UserAccount userAccount6 = new UserAccount("006",
+ "Yasha",
+ "She is an only child.");
+ appManager.save(userAccount6);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("004");
+ LOGGER.info(appManager.printCacheContent());
+ }
- var userAccount3 = new UserAccount("003",
- "Adam",
- "He likes food.");
- var userAccount4 = new UserAccount("004",
- "Rita",
- "She hates cats.");
- var userAccount5 = new UserAccount("005",
- "Isaac",
- "He is allergic to mustard.");
+ /**
+ * Cache-Aside.
+ */
+ public void useCacheAsideStategy() {
+ LOGGER.info("# CachingPolicy.ASIDE");
+ appManager.initCachingPolicy(CachingPolicy.ASIDE);
+ LOGGER.info(appManager.printCacheContent());
- appManager.save(userAccount3);
- appManager.save(userAccount4);
- appManager.save(userAccount5);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("003");
- LOGGER.info(appManager.printCacheContent());
- UserAccount userAccount6 = new UserAccount("006",
- "Yasha",
- "She is an only child.");
- appManager.save(userAccount6);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("004");
- LOGGER.info(appManager.printCacheContent());
- }
+ var userAccount3 = new UserAccount("003",
+ "Adam",
+ "He likes food.");
+ var userAccount4 = new UserAccount("004",
+ "Rita",
+ "She hates cats.");
+ var userAccount5 = new UserAccount("005",
+ "Isaac",
+ "He is allergic to mustard.");
+ appManager.save(userAccount3);
+ appManager.save(userAccount4);
+ appManager.save(userAccount5);
- /**
- * Cache-Aside.
- */
- public void useCacheAsideStategy() {
- LOGGER.info("# CachingPolicy.ASIDE");
- appManager.initCachingPolicy(CachingPolicy.ASIDE);
- LOGGER.info(appManager.printCacheContent());
-
- var userAccount3 = new UserAccount("003",
- "Adam",
- "He likes food.");
- var userAccount4 = new UserAccount("004",
- "Rita",
- "She hates cats.");
- var userAccount5 = new UserAccount("005",
- "Isaac",
- "He is allergic to mustard.");
- appManager.save(userAccount3);
- appManager.save(userAccount4);
- appManager.save(userAccount5);
-
- LOGGER.info(appManager.printCacheContent());
- appManager.find("003");
- LOGGER.info(appManager.printCacheContent());
- appManager.find("004");
- LOGGER.info(appManager.printCacheContent());
- }
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("003");
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("004");
+ LOGGER.info(appManager.printCacheContent());
+ }
}
diff --git a/caching/src/main/java/com/iluwatar/caching/AppManager.java b/caching/src/main/java/com/iluwatar/caching/AppManager.java
index ac3100523..1bafa8ff9 100644
--- a/caching/src/main/java/com/iluwatar/caching/AppManager.java
+++ b/caching/src/main/java/com/iluwatar/caching/AppManager.java
@@ -23,9 +23,9 @@
package com.iluwatar.caching;
+import com.iluwatar.caching.database.DbManager;
import java.util.Optional;
-import com.iluwatar.caching.database.DbManager;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -68,7 +68,7 @@ public class AppManager {
* to (temporarily) store the data/objects during runtime.
*/
public void initDb() {
- dbManager.connect();
+ dbManager.connect();
}
/**
diff --git a/caching/src/main/java/com/iluwatar/caching/CacheStore.java b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
index ca67fd334..d9d2e900b 100644
--- a/caching/src/main/java/com/iluwatar/caching/CacheStore.java
+++ b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
@@ -23,11 +23,11 @@
package com.iluwatar.caching;
+import com.iluwatar.caching.database.DbManager;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
-import com.iluwatar.caching.database.DbManager;
import lombok.extern.slf4j.Slf4j;
/**
diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java
index f23eb79b4..a9a93a4f6 100644
--- a/caching/src/main/java/com/iluwatar/caching/LruCache.java
+++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
+
/**
* Data structure/implementation of the application's cache. The data structure
* consists of a hash table attached with a doubly linked-list. The linked-list
@@ -62,7 +63,8 @@ public class LruCache {
/**
* Node definition.
- * @param id String
+ *
+ * @param id String
* @param account {@link UserAccount}
*/
Node(final String id, final UserAccount account) {
@@ -90,6 +92,7 @@ public class LruCache {
/**
* Constructor.
+ *
* @param cap Integer.
*/
public LruCache(final int cap) {
@@ -98,6 +101,7 @@ public class LruCache {
/**
* Get user account.
+ *
* @param userId String
* @return {@link UserAccount}
*/
@@ -113,6 +117,7 @@ public class LruCache {
/**
* Remove node from linked list.
+ *
* @param node {@link Node}
*/
public void remove(final Node node) {
@@ -130,6 +135,7 @@ public class LruCache {
/**
* Move node to the front of the list.
+ *
* @param node {@link Node}
*/
public void setHead(final Node node) {
@@ -146,8 +152,9 @@ public class LruCache {
/**
* Set user account.
+ *
* @param userAccount {@link UserAccount}
- * @param userId {@link String}
+ * @param userId {@link String}
*/
public void set(final String userId, final UserAccount userAccount) {
if (cache.containsKey(userId)) {
@@ -169,19 +176,21 @@ public class LruCache {
}
}
- /**
- * Che if Cache cintains the userId.
- * @param userId {@link String}
- * @return boolean
- */
+ /**
+ * Che if Cache cintains the userId.
+ *
+ * @param userId {@link String}
+ * @return boolean
+ */
public boolean contains(final String userId) {
return cache.containsKey(userId);
}
- /**
- * Invalidate cache for user.
- * @param userId {@link String}
- */
+ /**
+ * Invalidate cache for user.
+ *
+ * @param userId {@link String}
+ */
public void invalidate(final String userId) {
var toBeRemoved = cache.remove(userId);
if (toBeRemoved != null) {
@@ -191,18 +200,19 @@ public class LruCache {
}
}
- /**
- * Is cache full?
- * @return boolean
- */
+ /**
+ * Check if the cache is full.
+ * @return boolean
+ */
public boolean isFull() {
return cache.size() >= capacity;
}
- /**
- * Get LRU data.
- * @return {@link UserAccount}
- */
+ /**
+ * Get LRU data.
+ *
+ * @return {@link UserAccount}
+ */
public UserAccount getLruData() {
return end.userAccount;
}
@@ -218,6 +228,7 @@ public class LruCache {
/**
* Returns cache data in list form.
+ *
* @return {@link List}
*/
public List