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
@Getter
public enum CachingPolicy {
/**
* Through.
*/
THROUGH("through"),
/**
* AROUND.
*/
AROUND("around"),
/**
* BEHIND.
*/
BEHIND("behind"),
/**
* ASIDE.
*/
ASIDE("aside");
/**
* Policy value.
*/
private final String policy;
}

View File

@ -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<String, Node> cache = new HashMap<>();
Node head;
Node end;
/**
* Capacity of Cache.
*/
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.
* @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<UserAccount> getCacheDataInListForm() {
var listOfCacheData = new ArrayList<UserAccount>();
@ -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;

View File

@ -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;
}

View File

@ -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() {
}
}

View File

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

View File

@ -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();

View File

@ -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)
);

View File

@ -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<String, UserAccount> 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);
}
}
}

View File

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

View File

@ -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;
package com.iluwatar.caching;