From aff7ef8782e8431eb30e2c906d62642dc4746380 Mon Sep 17 00:00:00 2001 From: Victor Zalevskii Date: Mon, 23 Aug 2021 12:37:24 +0300 Subject: [PATCH] Fix last errors in checkstyle. --- .../com/iluwatar/caching/CachingPolicy.java | 15 +++ .../java/com/iluwatar/caching/LruCache.java | 123 +++++++++++++----- .../com/iluwatar/caching/UserAccount.java | 9 ++ .../caching/constants/CachingConstants.java | 20 ++- .../caching/constants/package-info.java | 4 + .../caching/database/DbManagerFactory.java | 21 ++- .../iluwatar/caching/database/MongoDb.java | 77 ++++++++--- .../iluwatar/caching/database/VirtualDb.java | 36 ++++- .../caching/database/package-info.java | 2 +- .../com/iluwatar/caching/package-info.java | 5 +- 10 files changed, 248 insertions(+), 64 deletions(-) create mode 100644 caching/src/main/java/com/iluwatar/caching/constants/package-info.java diff --git a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java index 0a42cf812..d24ad0173 100644 --- a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java +++ b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java @@ -32,10 +32,25 @@ import lombok.Getter; @AllArgsConstructor @Getter public enum CachingPolicy { + /** + * Through. + */ THROUGH("through"), + /** + * AROUND. + */ AROUND("around"), + /** + * BEHIND. + */ BEHIND("behind"), + /** + * ASIDE. + */ ASIDE("aside"); + /** + * Policy value. + */ private final String policy; } diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java index a320e35dc..f23eb79b4 100644 --- a/caching/src/main/java/com/iluwatar/caching/LruCache.java +++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java @@ -30,40 +30,78 @@ 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 helps in capturing and maintaining the - * LRU data in the cache. When a data is queried (from the cache), added (to the cache), or updated, - * the data is moved to the front of the list to depict itself as the most-recently-used data. The - * LRU data is always at the end of the list. + * 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 + * helps in capturing and maintaining the LRU data in the cache. When a data is + * queried (from the cache), added (to the cache), or updated, the data is + * moved to the front of the list to depict itself as the most-recently-used + * data. The LRU data is always at the end of the list. */ @Slf4j public class LruCache { - + /** + * Static class Node. + */ static class Node { - String userId; - UserAccount userAccount; - Node previous; - Node next; + /** + * user id. + */ + private final String userId; + /** + * User Account. + */ + private UserAccount userAccount; + /** + * previous. + */ + private Node previous; + /** + * next. + */ + private Node next; - public Node(String userId, UserAccount userAccount) { - this.userId = userId; - this.userAccount = userAccount; + /** + * Node definition. + * @param id String + * @param account {@link UserAccount} + */ + Node(final String id, final UserAccount account) { + this.userId = id; + this.userAccount = account; } } - int capacity; - Map cache = new HashMap<>(); - Node head; - Node end; + /** + * Capacity of Cache. + */ + private int capacity; + /** + * Cache {@link HashMap}. + */ + private Map cache = new HashMap<>(); + /** + * Head. + */ + private Node head; + /** + * End. + */ + private Node end; - public LruCache(int capacity) { - this.capacity = capacity; + /** + * Constructor. + * @param cap Integer. + */ + public LruCache(final int cap) { + this.capacity = cap; } /** * Get user account. + * @param userId String + * @return {@link UserAccount} */ - public UserAccount get(String userId) { + public UserAccount get(final String userId) { if (cache.containsKey(userId)) { var node = cache.get(userId); remove(node); @@ -75,8 +113,9 @@ public class LruCache { /** * Remove node from linked list. + * @param node {@link Node} */ - public void remove(Node node) { + public void remove(final Node node) { if (node.previous != null) { node.previous.next = node.next; } else { @@ -91,8 +130,9 @@ public class LruCache { /** * Move node to the front of the list. + * @param node {@link Node} */ - public void setHead(Node node) { + public void setHead(final Node node) { node.next = head; node.previous = null; if (head != null) { @@ -106,8 +146,10 @@ public class LruCache { /** * Set user account. + * @param userAccount {@link UserAccount} + * @param userId {@link String} */ - public void set(String userId, UserAccount userAccount) { + public void set(final String userId, final UserAccount userAccount) { if (cache.containsKey(userId)) { var old = cache.get(userId); old.userAccount = userAccount; @@ -127,25 +169,40 @@ public class LruCache { } } - public boolean contains(String userId) { + /** + * 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. - */ - public void invalidate(String userId) { + /** + * Invalidate cache for user. + * @param userId {@link String} + */ + public void invalidate(final String userId) { var toBeRemoved = cache.remove(userId); if (toBeRemoved != null) { - LOGGER.info("# {} has been updated! Removing older version from cache...", userId); + LOGGER.info("# {} has been updated! " + + "Removing older version from cache...", userId); remove(toBeRemoved); } } + /** + * Is cache full? + * @return boolean + */ public boolean isFull() { return cache.size() >= capacity; } + /** + * Get LRU data. + * @return {@link UserAccount} + */ public UserAccount getLruData() { return end.userAccount; } @@ -161,6 +218,7 @@ public class LruCache { /** * Returns cache data in list form. + * @return {@link List} */ public List getCacheDataInListForm() { var listOfCacheData = new ArrayList(); @@ -174,10 +232,13 @@ public class LruCache { /** * Set cache capacity. + * @param newCapacity int */ - public void setCapacity(int newCapacity) { + public void setCapacity(final int newCapacity) { if (capacity > newCapacity) { - clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll + // Behavior can be modified to accommodate + // for decrease in cache size. For now, we'll + clear(); // just clear the cache. } else { this.capacity = newCapacity; diff --git a/caching/src/main/java/com/iluwatar/caching/UserAccount.java b/caching/src/main/java/com/iluwatar/caching/UserAccount.java index 3681a931b..800f14c9e 100644 --- a/caching/src/main/java/com/iluwatar/caching/UserAccount.java +++ b/caching/src/main/java/com/iluwatar/caching/UserAccount.java @@ -36,7 +36,16 @@ import lombok.ToString; @AllArgsConstructor @ToString public class UserAccount { + /** + * User Id. + */ private String userId; + /** + * User Name. + */ private String userName; + /** + * Additional Info. + */ private String additionalInfo; } diff --git a/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java b/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java index 797a561fd..69cd63fef 100644 --- a/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java +++ b/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java @@ -26,11 +26,27 @@ package com.iluwatar.caching.constants; /** * Constant class for defining constants. */ -public class CachingConstants { - +public final class CachingConstants { + /** + * User Account. + */ public static final String USER_ACCOUNT = "user_accounts"; + /** + * User ID. + */ public static final String USER_ID = "userID"; + /** + * User Name. + */ public static final String USER_NAME = "userName"; + /** + * Additional Info. + */ public static final String ADD_INFO = "additionalInfo"; + /** + * Constructor. + */ + private CachingConstants() { + } } diff --git a/caching/src/main/java/com/iluwatar/caching/constants/package-info.java b/caching/src/main/java/com/iluwatar/caching/constants/package-info.java new file mode 100644 index 000000000..8ae963658 --- /dev/null +++ b/caching/src/main/java/com/iluwatar/caching/constants/package-info.java @@ -0,0 +1,4 @@ +/** + * Constants. + */ +package com.iluwatar.caching.constants; diff --git a/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java b/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java index 0e6ed9fe1..f68177203 100644 --- a/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java +++ b/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java @@ -1,8 +1,23 @@ package com.iluwatar.caching.database; -public class DbManagerFactory { - public static DbManager initDb(boolean isMongo){ - if(isMongo){ +/** + * Creates the database connection accroding the input parameter. + */ +public final class DbManagerFactory { + /** + * Private constructor. + */ + private DbManagerFactory() { + } + + /** + * Init database. + * + * @param isMongo boolean + * @return {@link DbManager} + */ + public static DbManager initDb(final boolean isMongo) { + if (isMongo) { return new MongoDb(); } return new VirtualDb(); diff --git a/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java b/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java index 50a5d805d..81395223c 100644 --- a/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java +++ b/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java @@ -7,72 +7,111 @@ import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.UpdateOptions; import org.bson.Document; +import static com.iluwatar.caching.constants.CachingConstants.USER_NAME; +import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO; +import static com.iluwatar.caching.constants.CachingConstants.USER_ID; +import static com.iluwatar.caching.constants.CachingConstants.USER_ACCOUNT; + /** * Implementation of DatabaseManager. * implements base methods to work with MongoDb. */ public class MongoDb implements DbManager { + /** + * Mongo db. + */ private MongoDatabase db; + /** + * Connect to Db. + */ @Override public void connect() { MongoClient mongoClient = new MongoClient(); db = mongoClient.getDatabase("test"); } + /** + * Read data from DB. + * + * @param userId {@link String} + * @return {@link UserAccount} + */ @Override - public UserAccount readFromDb(String userId) { + public UserAccount readFromDb(final String userId) { if (db == null) { connect(); } var iterable = db .getCollection(CachingConstants.USER_ACCOUNT) - .find(new Document(CachingConstants.USER_ID, userId)); + .find(new Document(USER_ID, userId)); if (iterable == null) { return null; } Document doc = iterable.first(); - String userName = doc.getString(CachingConstants.USER_NAME); - String appInfo = doc.getString(CachingConstants.ADD_INFO); + String userName = doc.getString(USER_NAME); + String appInfo = doc.getString(ADD_INFO); return new UserAccount(userId, userName, appInfo); } + /** + * Write data to DB. + * + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount writeToDb(UserAccount userAccount) { + public UserAccount writeToDb(final UserAccount userAccount) { if (db == null) { connect(); } - db.getCollection(CachingConstants.USER_ACCOUNT).insertOne( - new Document(CachingConstants.USER_ID, userAccount.getUserId()) - .append(CachingConstants.USER_NAME, userAccount.getUserName()) - .append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()) + db.getCollection(USER_ACCOUNT).insertOne( + new Document(USER_ID, userAccount.getUserId()) + .append(USER_NAME, userAccount.getUserName()) + .append(ADD_INFO, userAccount.getAdditionalInfo()) ); return userAccount; } + /** + * Update DB. + * + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount updateDb(UserAccount userAccount) { + public UserAccount updateDb(final UserAccount userAccount) { if (db == null) { connect(); } - db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( - new Document(CachingConstants.USER_ID, userAccount.getUserId()), - new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName()) - .append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()))); + Document id = new Document(USER_ID, userAccount.getUserId()); + Document dataSet = new Document(USER_NAME, userAccount.getUserName()) + .append(ADD_INFO, userAccount.getAdditionalInfo()); + db.getCollection(CachingConstants.USER_ACCOUNT) + .updateOne(id, new Document("$set", dataSet)); return userAccount; } + /** + * Update data if exists. + * + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount upsertDb(UserAccount userAccount) { + public UserAccount upsertDb(final UserAccount userAccount) { if (db == null) { connect(); } + String userId = userAccount.getUserId(); + String userName = userAccount.getUserName(); + String additionalInfo = userAccount.getAdditionalInfo(); db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( - new Document(CachingConstants.USER_ID, userAccount.getUserId()), + new Document(USER_ID, userId), new Document("$set", - new Document(CachingConstants.USER_ID, userAccount.getUserId()) - .append(CachingConstants.USER_NAME, userAccount.getUserName()) - .append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()) + new Document(USER_ID, userId) + .append(USER_NAME, userName) + .append(ADD_INFO, additionalInfo) ), new UpdateOptions().upsert(true) ); diff --git a/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java b/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java index af67225ad..22e091b83 100644 --- a/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java +++ b/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java @@ -10,35 +10,61 @@ import java.util.Map; * implements base methods to work with hashMap as database. */ public class VirtualDb implements DbManager { + /** + * Virtual DataBase. + */ private Map virtualDB; + /** + * Creates new HashMap. + */ @Override public void connect() { virtualDB = new HashMap<>(); } + /** + * Read from Db. + * @param userId {@link String} + * @return {@link UserAccount} + */ @Override - public UserAccount readFromDb(String userId) { + public UserAccount readFromDb(final String userId) { if (virtualDB.containsKey(userId)) { return virtualDB.get(userId); } return null; } + /** + * Write to DB. + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount writeToDb(UserAccount userAccount) { + public UserAccount writeToDb(final UserAccount userAccount) { virtualDB.put(userAccount.getUserId(), userAccount); return userAccount; } + /** + * Update reecord in DB. + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount updateDb(UserAccount userAccount) { + public UserAccount updateDb(final UserAccount userAccount) { virtualDB.put(userAccount.getUserId(), userAccount); return userAccount; } + /** + * Update. + * @param userAccount {@link UserAccount} + * @return {@link UserAccount} + */ @Override - public UserAccount upsertDb(UserAccount userAccount) { + public UserAccount upsertDb(final UserAccount userAccount) { return updateDb(userAccount); } -} \ No newline at end of file +} diff --git a/caching/src/main/java/com/iluwatar/caching/database/package-info.java b/caching/src/main/java/com/iluwatar/caching/database/package-info.java index f81386688..56deee71d 100644 --- a/caching/src/main/java/com/iluwatar/caching/database/package-info.java +++ b/caching/src/main/java/com/iluwatar/caching/database/package-info.java @@ -1,4 +1,4 @@ /** - * Database classes + * Database classes. */ package com.iluwatar.caching.database; diff --git a/caching/src/main/java/com/iluwatar/caching/package-info.java b/caching/src/main/java/com/iluwatar/caching/package-info.java index d670c1bd0..fed1ab2a8 100644 --- a/caching/src/main/java/com/iluwatar/caching/package-info.java +++ b/caching/src/main/java/com/iluwatar/caching/package-info.java @@ -1,4 +1,4 @@ -/* +/** * The MIT License * Copyright © 2014-2021 Ilkka Seppälä * @@ -20,5 +20,4 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - -package com.iluwatar.caching; \ No newline at end of file +package com.iluwatar.caching;