Fix last errors in checkstyle.

This commit is contained in:
Victor Zalevskii 2021-08-23 12:37:24 +03:00
parent c150e5f38b
commit aff7ef8782
10 changed files with 248 additions and 64 deletions

View File

@ -32,10 +32,25 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum CachingPolicy { public enum CachingPolicy {
/**
* Through.
*/
THROUGH("through"), THROUGH("through"),
/**
* AROUND.
*/
AROUND("around"), AROUND("around"),
/**
* BEHIND.
*/
BEHIND("behind"), BEHIND("behind"),
/**
* ASIDE.
*/
ASIDE("aside"); ASIDE("aside");
/**
* Policy value.
*/
private final String policy; private final String policy;
} }

View File

@ -30,40 +30,78 @@ import java.util.Map;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
* Data structure/implementation of the application's cache. The data structure consists of a hash * Data structure/implementation of the application's cache. The data structure
* table attached with a doubly linked-list. The linked-list helps in capturing and maintaining the * consists of a hash table attached with a doubly linked-list. The linked-list
* LRU data in the cache. When a data is queried (from the cache), added (to the cache), or updated, * helps in capturing and maintaining the LRU data in the cache. When a data is
* the data is moved to the front of the list to depict itself as the most-recently-used data. The * queried (from the cache), added (to the cache), or updated, the data is
* LRU data is always at the end of the list. * 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 @Slf4j
public class LruCache { public class LruCache {
/**
* Static class Node.
*/
static class Node { static class Node {
String userId; /**
UserAccount userAccount; * user id.
Node previous; */
Node next; 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; * Node definition.
this.userAccount = userAccount; * @param id String
* @param account {@link UserAccount}
*/
Node(final String id, final UserAccount account) {
this.userId = id;
this.userAccount = account;
} }
} }
int capacity; /**
Map<String, Node> cache = new HashMap<>(); * Capacity of Cache.
Node head; */
Node end; private int capacity;
/**
* Cache {@link HashMap}.
*/
private Map<String, Node> 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. * Get user account.
* @param userId String
* @return {@link UserAccount}
*/ */
public UserAccount get(String userId) { public UserAccount get(final String userId) {
if (cache.containsKey(userId)) { if (cache.containsKey(userId)) {
var node = cache.get(userId); var node = cache.get(userId);
remove(node); remove(node);
@ -75,8 +113,9 @@ public class LruCache {
/** /**
* Remove node from linked list. * Remove node from linked list.
* @param node {@link Node}
*/ */
public void remove(Node node) { public void remove(final Node node) {
if (node.previous != null) { if (node.previous != null) {
node.previous.next = node.next; node.previous.next = node.next;
} else { } else {
@ -91,8 +130,9 @@ public class LruCache {
/** /**
* Move node to the front of the list. * 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.next = head;
node.previous = null; node.previous = null;
if (head != null) { if (head != null) {
@ -106,8 +146,10 @@ public class LruCache {
/** /**
* Set user account. * 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)) { if (cache.containsKey(userId)) {
var old = cache.get(userId); var old = cache.get(userId);
old.userAccount = userAccount; 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); return cache.containsKey(userId);
} }
/** /**
* Invalidate cache for user. * Invalidate cache for user.
* @param userId {@link String}
*/ */
public void invalidate(String userId) { public void invalidate(final String userId) {
var toBeRemoved = cache.remove(userId); var toBeRemoved = cache.remove(userId);
if (toBeRemoved != null) { 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); remove(toBeRemoved);
} }
} }
/**
* Is cache full?
* @return boolean
*/
public boolean isFull() { public boolean isFull() {
return cache.size() >= capacity; return cache.size() >= capacity;
} }
/**
* Get LRU data.
* @return {@link UserAccount}
*/
public UserAccount getLruData() { public UserAccount getLruData() {
return end.userAccount; return end.userAccount;
} }
@ -161,6 +218,7 @@ public class LruCache {
/** /**
* Returns cache data in list form. * Returns cache data in list form.
* @return {@link List}
*/ */
public List<UserAccount> getCacheDataInListForm() { public List<UserAccount> getCacheDataInListForm() {
var listOfCacheData = new ArrayList<UserAccount>(); var listOfCacheData = new ArrayList<UserAccount>();
@ -174,10 +232,13 @@ public class LruCache {
/** /**
* Set cache capacity. * Set cache capacity.
* @param newCapacity int
*/ */
public void setCapacity(int newCapacity) { public void setCapacity(final int newCapacity) {
if (capacity > 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. // just clear the cache.
} else { } else {
this.capacity = newCapacity; this.capacity = newCapacity;

View File

@ -36,7 +36,16 @@ import lombok.ToString;
@AllArgsConstructor @AllArgsConstructor
@ToString @ToString
public class UserAccount { public class UserAccount {
/**
* User Id.
*/
private String userId; private String userId;
/**
* User Name.
*/
private String userName; private String userName;
/**
* Additional Info.
*/
private String additionalInfo; private String additionalInfo;
} }

View File

@ -26,11 +26,27 @@ package com.iluwatar.caching.constants;
/** /**
* Constant class for defining constants. * Constant class for defining constants.
*/ */
public class CachingConstants { public final class CachingConstants {
/**
* User Account.
*/
public static final String USER_ACCOUNT = "user_accounts"; public static final String USER_ACCOUNT = "user_accounts";
/**
* User ID.
*/
public static final String USER_ID = "userID"; public static final String USER_ID = "userID";
/**
* User Name.
*/
public static final String USER_NAME = "userName"; public static final String USER_NAME = "userName";
/**
* Additional Info.
*/
public static final String ADD_INFO = "additionalInfo"; public static final String ADD_INFO = "additionalInfo";
/**
* Constructor.
*/
private CachingConstants() {
}
} }

View File

@ -0,0 +1,4 @@
/**
* Constants.
*/
package com.iluwatar.caching.constants;

View File

@ -1,8 +1,23 @@
package com.iluwatar.caching.database; package com.iluwatar.caching.database;
public class DbManagerFactory { /**
public static DbManager initDb(boolean isMongo){ * Creates the database connection accroding the input parameter.
if(isMongo){ */
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 MongoDb();
} }
return new VirtualDb(); return new VirtualDb();

View File

@ -7,72 +7,111 @@ import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.model.UpdateOptions;
import org.bson.Document; 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. * Implementation of DatabaseManager.
* implements base methods to work with MongoDb. * implements base methods to work with MongoDb.
*/ */
public class MongoDb implements DbManager { public class MongoDb implements DbManager {
/**
* Mongo db.
*/
private MongoDatabase db; private MongoDatabase db;
/**
* Connect to Db.
*/
@Override @Override
public void connect() { public void connect() {
MongoClient mongoClient = new MongoClient(); MongoClient mongoClient = new MongoClient();
db = mongoClient.getDatabase("test"); db = mongoClient.getDatabase("test");
} }
/**
* Read data from DB.
*
* @param userId {@link String}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount readFromDb(String userId) { public UserAccount readFromDb(final String userId) {
if (db == null) { if (db == null) {
connect(); connect();
} }
var iterable = db var iterable = db
.getCollection(CachingConstants.USER_ACCOUNT) .getCollection(CachingConstants.USER_ACCOUNT)
.find(new Document(CachingConstants.USER_ID, userId)); .find(new Document(USER_ID, userId));
if (iterable == null) { if (iterable == null) {
return null; return null;
} }
Document doc = iterable.first(); Document doc = iterable.first();
String userName = doc.getString(CachingConstants.USER_NAME); String userName = doc.getString(USER_NAME);
String appInfo = doc.getString(CachingConstants.ADD_INFO); String appInfo = doc.getString(ADD_INFO);
return new UserAccount(userId, userName, appInfo); return new UserAccount(userId, userName, appInfo);
} }
/**
* Write data to DB.
*
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount writeToDb(UserAccount userAccount) { public UserAccount writeToDb(final UserAccount userAccount) {
if (db == null) { if (db == null) {
connect(); connect();
} }
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne( db.getCollection(USER_ACCOUNT).insertOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()) new Document(USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName()) .append(USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()) .append(ADD_INFO, userAccount.getAdditionalInfo())
); );
return userAccount; return userAccount;
} }
/**
* Update DB.
*
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount updateDb(UserAccount userAccount) { public UserAccount updateDb(final UserAccount userAccount) {
if (db == null) { if (db == null) {
connect(); connect();
} }
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( Document id = new Document(USER_ID, userAccount.getUserId());
new Document(CachingConstants.USER_ID, userAccount.getUserId()), Document dataSet = new Document(USER_NAME, userAccount.getUserName())
new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName()) .append(ADD_INFO, userAccount.getAdditionalInfo());
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()))); db.getCollection(CachingConstants.USER_ACCOUNT)
.updateOne(id, new Document("$set", dataSet));
return userAccount; return userAccount;
} }
/**
* Update data if exists.
*
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount upsertDb(UserAccount userAccount) { public UserAccount upsertDb(final UserAccount userAccount) {
if (db == null) { if (db == null) {
connect(); connect();
} }
String userId = userAccount.getUserId();
String userName = userAccount.getUserName();
String additionalInfo = userAccount.getAdditionalInfo();
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne( db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()), new Document(USER_ID, userId),
new Document("$set", new Document("$set",
new Document(CachingConstants.USER_ID, userAccount.getUserId()) new Document(USER_ID, userId)
.append(CachingConstants.USER_NAME, userAccount.getUserName()) .append(USER_NAME, userName)
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()) .append(ADD_INFO, additionalInfo)
), ),
new UpdateOptions().upsert(true) new UpdateOptions().upsert(true)
); );

View File

@ -10,35 +10,61 @@ import java.util.Map;
* implements base methods to work with hashMap as database. * implements base methods to work with hashMap as database.
*/ */
public class VirtualDb implements DbManager { public class VirtualDb implements DbManager {
/**
* Virtual DataBase.
*/
private Map<String, UserAccount> virtualDB; private Map<String, UserAccount> virtualDB;
/**
* Creates new HashMap.
*/
@Override @Override
public void connect() { public void connect() {
virtualDB = new HashMap<>(); virtualDB = new HashMap<>();
} }
/**
* Read from Db.
* @param userId {@link String}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount readFromDb(String userId) { public UserAccount readFromDb(final String userId) {
if (virtualDB.containsKey(userId)) { if (virtualDB.containsKey(userId)) {
return virtualDB.get(userId); return virtualDB.get(userId);
} }
return null; return null;
} }
/**
* Write to DB.
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount writeToDb(UserAccount userAccount) { public UserAccount writeToDb(final UserAccount userAccount) {
virtualDB.put(userAccount.getUserId(), userAccount); virtualDB.put(userAccount.getUserId(), userAccount);
return userAccount; return userAccount;
} }
/**
* Update reecord in DB.
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount updateDb(UserAccount userAccount) { public UserAccount updateDb(final UserAccount userAccount) {
virtualDB.put(userAccount.getUserId(), userAccount); virtualDB.put(userAccount.getUserId(), userAccount);
return userAccount; return userAccount;
} }
/**
* Update.
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override @Override
public UserAccount upsertDb(UserAccount userAccount) { public UserAccount upsertDb(final UserAccount userAccount) {
return updateDb(userAccount); return updateDb(userAccount);
} }
} }

View File

@ -1,4 +1,4 @@
/** /**
* Database classes * Database classes.
*/ */
package com.iluwatar.caching.database; package com.iluwatar.caching.database;

View File

@ -1,4 +1,4 @@
/* /**
* The MIT License * The MIT License
* Copyright © 2014-2021 Ilkka Seppälä * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package com.iluwatar.caching; package com.iluwatar.caching;