Resolves checkstyle errors for business-delegate, bytecode, caching (#1059)

* Reduces checkstyle errors in business-delegate

* Reduces checkstyle errors in bytecode

* Reduces checkstyle errors in caching
This commit is contained in:
Anurag Agarwal
2019-11-10 00:53:12 +05:30
committed by Ilkka Seppälä
parent 6d1c0b1563
commit efc17fcc70
21 changed files with 119 additions and 134 deletions

View File

@ -27,7 +27,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
* the resources immediately after their use. The resources retain their identity, are kept in some
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
@ -43,8 +42,8 @@ import org.slf4j.LoggerFactory;
* should be written back to the backing store (i.e. Database) and help keep both data sources
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
* consistency between data held in the cache and the data in the underlying data store.
* <p>
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
*
* <p>In this example, the user account ({@link UserAccount}) entity is used as the underlying
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
* strategies are individually tested. The testing of the cache is restricted towards saving and
@ -60,7 +59,6 @@ import org.slf4j.LoggerFactory;
* @see CacheStore
* @see LruCache
* @see CachingPolicy
*
*/
public class App {
@ -68,15 +66,15 @@ public class App {
/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
AppManager.initDb(false); // 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).
// 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).
AppManager.initCacheCapacity(3);
App app = new App();
app.useReadAndWriteThroughStrategy();
@ -86,7 +84,7 @@ public class App {
}
/**
* Read-through and write-through
* Read-through and write-through.
*/
public void useReadAndWriteThroughStrategy() {
LOGGER.info("# CachingPolicy.THROUGH");
@ -101,7 +99,7 @@ public class App {
}
/**
* Read-through and write-around
* Read-through and write-around.
*/
public void useReadThroughAndWriteAroundStrategy() {
LOGGER.info("# CachingPolicy.AROUND");
@ -123,7 +121,7 @@ public class App {
}
/**
* Read-through and write-behind
* Read-through and write-behind.
*/
public void useReadThroughAndWriteBehindStrategy() {
LOGGER.info("# CachingPolicy.BEHIND");
@ -147,7 +145,7 @@ public class App {
}
/**
* Cache-Aside
* Cache-Aside.
*/
public void useCacheAsideStategy() {
LOGGER.info("# CachingPolicy.ASIDE");

View File

@ -26,13 +26,11 @@ package com.iluwatar.caching;
import java.text.ParseException;
/**
*
* AppManager helps to bridge the gap in communication between the main class and the application's
* back-end. DB connection is initialized through this class. The chosen caching strategy/policy is
* also initialized here. Before the cache can be used, the size of the cache has to be set.
* Depending on the chosen caching policy, AppManager will call the appropriate function in the
* CacheStore class.
*
*/
public final class AppManager {
@ -42,7 +40,6 @@ public final class AppManager {
}
/**
*
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
* data storage or a simple Java data structure to (temporarily) store the data/objects during
* runtime.
@ -60,7 +57,7 @@ public final class AppManager {
}
/**
* Initialize caching policy
* Initialize caching policy.
*/
public static void initCachingPolicy(CachingPolicy policy) {
cachingPolicy = policy;
@ -75,7 +72,7 @@ public final class AppManager {
}
/**
* Find user account
* Find user account.
*/
public static UserAccount find(String userId) {
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
@ -89,7 +86,7 @@ public final class AppManager {
}
/**
* Save user account
* Save user account.
*/
public static void save(UserAccount userAccount) {
if (cachingPolicy == CachingPolicy.THROUGH) {
@ -108,7 +105,7 @@ public final class AppManager {
}
/**
* Cache-Aside save user account helper
* Cache-Aside save user account helper.
*/
private static void saveAside(UserAccount userAccount) {
DbManager.updateDb(userAccount);
@ -116,7 +113,7 @@ public final class AppManager {
}
/**
* Cache-Aside find user account helper
* Cache-Aside find user account helper.
*/
private static UserAccount findAside(String userId) {
UserAccount userAccount = CacheStore.get(userId);

View File

@ -23,15 +23,12 @@
package com.iluwatar.caching;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
*
* The caching strategies are implemented in this class.
*
*/
public class CacheStore {
@ -43,7 +40,7 @@ public class CacheStore {
}
/**
* Init cache capacity
* Init cache capacity.
*/
public static void initCapacity(int capacity) {
if (cache == null) {
@ -54,7 +51,7 @@ public class CacheStore {
}
/**
* Get user account using read-through cache
* Get user account using read-through cache.
*/
public static UserAccount readThrough(String userId) {
if (cache.contains(userId)) {
@ -68,7 +65,7 @@ public class CacheStore {
}
/**
* Get user account using write-through cache
* Get user account using write-through cache.
*/
public static void writeThrough(UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) {
@ -80,20 +77,20 @@ public class CacheStore {
}
/**
* Get user account using write-around cache
* Get user account using write-around cache.
*/
public static void writeAround(UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) {
DbManager.updateDb(userAccount);
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
// version from cache.
// version from cache.
} else {
DbManager.writeToDb(userAccount);
}
}
/**
* Get user account using read-through cache with write-back policy
* Get user account using read-through cache with write-back policy.
*/
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
if (cache.contains(userId)) {
@ -112,7 +109,7 @@ public class CacheStore {
}
/**
* Set user account
* Set user account.
*/
public static void writeBehind(UserAccount userAccount) {
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
@ -124,7 +121,7 @@ public class CacheStore {
}
/**
* Clears cache
* Clears cache.
*/
public static void clearCache() {
if (cache != null) {
@ -147,7 +144,7 @@ public class CacheStore {
}
/**
* Print user accounts
* Print user accounts.
*/
public static String print() {
List<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
@ -161,21 +158,21 @@ public class CacheStore {
}
/**
* Delegate to backing cache store
* Delegate to backing cache store.
*/
public static UserAccount get(String userId) {
return cache.get(userId);
}
/**
* Delegate to backing cache store
* Delegate to backing cache store.
*/
public static void set(String userId, UserAccount userAccount) {
cache.set(userId, userAccount);
}
/**
* Delegate to backing cache store
* Delegate to backing cache store.
*/
public static void invalidate(String userId) {
cache.invalidate(userId);

View File

@ -24,9 +24,7 @@
package com.iluwatar.caching;
/**
*
* Enum class containing the four caching strategies implemented in the pattern.
*
*/
public enum CachingPolicy {
THROUGH("through"), AROUND("around"), BEHIND("behind"), ASIDE("aside");

View File

@ -23,28 +23,24 @@
package com.iluwatar.caching;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.bson.Document;
import com.iluwatar.caching.constants.CachingConstants;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.bson.Document;
/**
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains
* the implemented methods for querying, inserting, and updating data. MongoDB was used as the
* database for the application.</p>
*
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains the
* implemented methods for querying, inserting, and updating data. MongoDB was used as the database
* for the application.</p>
*
* <p>Developer/Tester is able to choose whether the application should use MongoDB as its underlying
* data storage (connect()) or a simple Java data structure to (temporarily) store the data/objects
* during runtime (createVirtualDB()).</p>
*
* <p>Developer/Tester is able to choose whether the application should use MongoDB as its
* underlying data storage (connect()) or a simple Java data structure to (temporarily) store the
* data/objects during runtime (createVirtualDB()).</p>
*/
public final class DbManager {
@ -58,7 +54,7 @@ public final class DbManager {
}
/**
* Create DB
* Create DB.
*/
public static void createVirtualDb() {
useMongoDB = false;
@ -66,7 +62,7 @@ public final class DbManager {
}
/**
* Connect to DB
* Connect to DB.
*/
public static void connect() throws ParseException {
useMongoDB = true;
@ -75,7 +71,7 @@ public final class DbManager {
}
/**
* Read user account from DB
* Read user account from DB.
*/
public static UserAccount readFromDb(String userId) {
if (!useMongoDB) {
@ -91,17 +87,20 @@ public final class DbManager {
e.printStackTrace();
}
}
FindIterable<Document> iterable =
db.getCollection(CachingConstants.USER_ACCOUNT).find(new Document(CachingConstants.USER_ID, userId));
FindIterable<Document> iterable = db
.getCollection(CachingConstants.USER_ACCOUNT)
.find(new Document(CachingConstants.USER_ID, userId));
if (iterable == null) {
return null;
}
Document doc = iterable.first();
return new UserAccount(userId, doc.getString(CachingConstants.USER_NAME), doc.getString(CachingConstants.ADD_INFO));
String userName = doc.getString(CachingConstants.USER_NAME);
String appInfo = doc.getString(CachingConstants.ADD_INFO);
return new UserAccount(userId, userName, appInfo);
}
/**
* Write user account to DB
* Write user account to DB.
*/
public static void writeToDb(UserAccount userAccount) {
if (!useMongoDB) {
@ -116,12 +115,14 @@ public final class DbManager {
}
}
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()));
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
);
}
/**
* Update DB
* Update DB.
*/
public static void updateDb(UserAccount userAccount) {
if (!useMongoDB) {
@ -142,7 +143,6 @@ public final class DbManager {
}
/**
*
* Insert data into DB if it does not exist. Else, update it.
*/
public static void upsertDb(UserAccount userAccount) {
@ -161,8 +161,10 @@ public final class DbManager {
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set",
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName()).append(CachingConstants.ADD_INFO,
userAccount.getAdditionalInfo())),
new UpdateOptions().upsert(true));
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
),
new UpdateOptions().upsert(true)
);
}
}

View File

@ -23,22 +23,19 @@
package com.iluwatar.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* 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.
*
*/
public class LruCache {
@ -66,7 +63,7 @@ public class LruCache {
}
/**
* Get user account
* Get user account.
*/
public UserAccount get(String userId) {
if (cache.containsKey(userId)) {
@ -110,7 +107,7 @@ public class LruCache {
}
/**
* Set user account
* Set user account.
*/
public void set(String userId, UserAccount userAccount) {
if (cache.containsKey(userId)) {
@ -137,7 +134,7 @@ public class LruCache {
}
/**
* Invalidate cache for user
* Invalidate cache for user.
*/
public void invalidate(String userId) {
Node toBeRemoved = cache.remove(userId);
@ -156,7 +153,7 @@ public class LruCache {
}
/**
* Clear cache
* Clear cache.
*/
public void clear() {
head = null;
@ -178,12 +175,12 @@ public class LruCache {
}
/**
* Set cache capacity
* Set cache capacity.
*/
public void setCapacity(int newCapacity) {
if (capacity > newCapacity) {
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
// just clear the cache.
// just clear the cache.
} else {
this.capacity = newCapacity;
}

View File

@ -32,7 +32,7 @@ public class UserAccount {
private String additionalInfo;
/**
* Constructor
* Constructor.
*/
public UserAccount(String userId, String userName, String additionalInfo) {
this.userId = userId;

View File

@ -24,9 +24,7 @@
package com.iluwatar.caching.constants;
/**
*
* Constant class for defining constants
*
* Constant class for defining constants.
*/
public class CachingConstants {