From efc17fcc70781c13b1c3cc938de73b6514721cb7 Mon Sep 17 00:00:00 2001 From: Anurag Agarwal Date: Sun, 10 Nov 2019 00:53:12 +0530 Subject: [PATCH] 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 --- .../com/iluwatar/business/delegate/App.java | 4 +- .../business/delegate/BusinessDelegate.java | 2 +- .../business/delegate/BusinessLookup.java | 2 + .../business/delegate/BusinessService.java | 4 +- .../iluwatar/business/delegate/Client.java | 4 +- .../business/delegate/EjbService.java | 4 +- .../business/delegate/JmsService.java | 4 +- .../business/delegate/ServiceType.java | 4 +- .../main/java/com/iluwatar/bytecode/App.java | 54 ++++++++--------- .../com/iluwatar/bytecode/Instruction.java | 5 +- .../com/iluwatar/bytecode/VirtualMachine.java | 7 ++- .../java/com/iluwatar/bytecode/Wizard.java | 3 +- .../util/InstructionConverterUtil.java | 7 ++- .../main/java/com/iluwatar/caching/App.java | 22 ++++--- .../java/com/iluwatar/caching/AppManager.java | 13 ++--- .../java/com/iluwatar/caching/CacheStore.java | 29 +++++----- .../com/iluwatar/caching/CachingPolicy.java | 2 - .../java/com/iluwatar/caching/DbManager.java | 58 ++++++++++--------- .../java/com/iluwatar/caching/LruCache.java | 19 +++--- .../com/iluwatar/caching/UserAccount.java | 2 +- .../caching/constants/CachingConstants.java | 4 +- 21 files changed, 119 insertions(+), 134 deletions(-) diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java index 68e382c0f..fcb1acd1b 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java @@ -28,11 +28,11 @@ package com.iluwatar.business.delegate; * tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate * encapsulates knowledge about how to locate, connect to, and interact with the business objects * that make up the application. - * + * *

Some of the services the Business Delegate uses are instantiated directly, and some can be * retrieved through service lookups. The Business Delegate itself may contain business logic too * potentially tying together multiple service calls, exception handling, retrying etc. - * + * *

In this example the client ({@link Client}) utilizes a business delegate ( * {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate * service and makes the service call. diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java index cf2b25129..c39a2ad39 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java @@ -24,7 +24,7 @@ package com.iluwatar.business.delegate; /** - * BusinessDelegate separates the presentation and business tiers + * BusinessDelegate separates the presentation and business tiers. */ public class BusinessDelegate { diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java index e7d8400d3..489caa23c 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java @@ -33,6 +33,8 @@ public class BusinessLookup { private JmsService jmsService; /** + * Gets service instance based on service type. + * * @param serviceType Type of service instance to be returned. * @return Service instance. */ diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java index 6e08aca1f..3094d3f6e 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java @@ -24,9 +24,7 @@ package com.iluwatar.business.delegate; /** - * - * Interface for service implementations - * + * Interface for service implementations. */ public interface BusinessService { diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java index c9c8950db..dcf4ce6b2 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java @@ -24,9 +24,7 @@ package com.iluwatar.business.delegate; /** - * - * Client utilizes BusinessDelegate to call the business tier - * + * Client utilizes BusinessDelegate to call the business tier. */ public class Client { diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java index aa9457abf..6f39abb1a 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * Service EJB implementation - * + * Service EJB implementation. */ public class EjbService implements BusinessService { diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java index 83abd9762..2317d783a 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * Service JMS implementation - * + * Service JMS implementation. */ public class JmsService implements BusinessService { diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java index a09dde59c..87fd1562d 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java @@ -24,9 +24,7 @@ package com.iluwatar.business.delegate; /** - * - * Enumeration for service types - * + * Enumeration for service types. */ public enum ServiceType { diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/App.java b/bytecode/src/main/java/com/iluwatar/bytecode/App.java index 9a5f66d88..165043c70 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/App.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/App.java @@ -24,57 +24,59 @@ package com.iluwatar.bytecode; import com.iluwatar.bytecode.util.InstructionConverterUtil; +import java.util.Stack; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as instructions - * for a virtual machine. - * An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as - * a sequence of bytes. A virtual machine executes these instructions one at a time, - * using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined. - * - * This pattern should be used when there is a need to define high number of behaviours and implementation engine - * is not a good choice because - * It is too lowe level - * Iterating on it takes too long due to slow compile times or other tooling issues. - * It has too much trust. If you want to ensure the behavior being defined can’t break the game, - * you need to sandbox it from the rest of the codebase. + * The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as + * instructions for a virtual machine. An instruction set defines the low-level operations that can + * be performed. A series of instructions is encoded as a sequence of bytes. A virtual machine + * executes these instructions one at a time, using a stack for intermediate values. By combining + * instructions, complex high-level behavior can be defined. * + *

This pattern should be used when there is a need to define high number of behaviours and + * implementation engine is not a good choice because It is too lowe level Iterating on it takes too + * long due to slow compile times or other tooling issues. It has too much trust. If you want to + * ensure the behavior being defined can’t break the game, you need to sandbox it from the rest of + * the codebase. */ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Main app method + * Main app method. + * * @param args command line args */ public static void main(String[] args) { - VirtualMachine vm = new VirtualMachine(); Wizard wizard = new Wizard(); wizard.setHealth(45); wizard.setAgility(7); wizard.setWisdom(11); + + VirtualMachine vm = new VirtualMachine(); vm.getWizards()[0] = wizard; interpretInstruction("LITERAL 0", vm); - interpretInstruction( "LITERAL 0", vm); - interpretInstruction( "GET_HEALTH", vm); - interpretInstruction( "LITERAL 0", vm); - interpretInstruction( "GET_AGILITY", vm); - interpretInstruction( "LITERAL 0", vm); - interpretInstruction( "GET_WISDOM ", vm); - interpretInstruction( "ADD", vm); - interpretInstruction( "LITERAL 2", vm); - interpretInstruction( "DIVIDE", vm); - interpretInstruction( "ADD", vm); - interpretInstruction( "SET_HEALTH", vm); + interpretInstruction("LITERAL 0", vm); + interpretInstruction("GET_HEALTH", vm); + interpretInstruction("LITERAL 0", vm); + interpretInstruction("GET_AGILITY", vm); + interpretInstruction("LITERAL 0", vm); + interpretInstruction("GET_WISDOM ", vm); + interpretInstruction("ADD", vm); + interpretInstruction("LITERAL 2", vm); + interpretInstruction("DIVIDE", vm); + interpretInstruction("ADD", vm); + interpretInstruction("SET_HEALTH", vm); } private static void interpretInstruction(String instruction, VirtualMachine vm) { InstructionConverterUtil converter = new InstructionConverterUtil(); vm.execute(converter.convertToByteCode(instruction)); - LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "" ) + vm.getStack()); + Stack stack = vm.getStack(); + LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack); } } diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java b/bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java index 99b632ed8..7684c778d 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java @@ -24,7 +24,7 @@ package com.iluwatar.bytecode; /** - * Representation of instructions understandable by virtual machine + * Representation of instructions understandable by virtual machine. */ public enum Instruction { @@ -51,7 +51,8 @@ public enum Instruction { } /** - * Converts integer value to Instruction + * Converts integer value to Instruction. + * * @param value value of instruction * @return representation of the instruction */ diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java b/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java index c6b120963..111c27e73 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java @@ -26,7 +26,7 @@ package com.iluwatar.bytecode; import java.util.Stack; /** - * Implementation of virtual machine + * Implementation of virtual machine. */ public class VirtualMachine { @@ -35,7 +35,7 @@ public class VirtualMachine { private Wizard[] wizards = new Wizard[2]; /** - * Constructor + * Constructor. */ public VirtualMachine() { for (int i = 0; i < wizards.length; i++) { @@ -44,7 +44,8 @@ public class VirtualMachine { } /** - * Executes provided bytecode + * Executes provided bytecode. + * * @param bytecode to execute */ public void execute(int[] bytecode) { diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java b/bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java index 434a1bddd..5153969d9 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java @@ -27,7 +27,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This class represent game objects which properties can be changed by instructions interpreted by virtual machine + * This class represent game objects which properties can be changed by instructions interpreted by + * virtual machine. */ public class Wizard { private static final Logger LOGGER = LoggerFactory.getLogger(Wizard.class); diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java b/bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java index bdc0782dd..1d3002cb1 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java @@ -26,11 +26,11 @@ package com.iluwatar.bytecode.util; import com.iluwatar.bytecode.Instruction; /** - * Utility class used for instruction validation and conversion + * Utility class used for instruction validation and conversion. */ public class InstructionConverterUtil { /** - * Converts instructions represented as String + * Converts instructions represented as String. * * @param instructions to convert * @return array of int representing bytecode @@ -48,7 +48,8 @@ public class InstructionConverterUtil { } else if (isValidInt(splitedInstructions[i])) { bytecode[i] = Integer.valueOf(splitedInstructions[i]); } else { - throw new IllegalArgumentException("Invalid instruction or number: " + splitedInstructions[i]); + String errorMessage = "Invalid instruction or number: " + splitedInstructions[i]; + throw new IllegalArgumentException(errorMessage); } } diff --git a/caching/src/main/java/com/iluwatar/caching/App.java b/caching/src/main/java/com/iluwatar/caching/App.java index 4ef12b7de..e1e4e67ca 100644 --- a/caching/src/main/java/com/iluwatar/caching/App.java +++ b/caching/src/main/java/com/iluwatar/caching/App.java @@ -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. - *

- * In this example, the user account ({@link UserAccount}) entity is used as the underlying + * + *

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"); diff --git a/caching/src/main/java/com/iluwatar/caching/AppManager.java b/caching/src/main/java/com/iluwatar/caching/AppManager.java index 6939c6b80..ec7f0df69 100644 --- a/caching/src/main/java/com/iluwatar/caching/AppManager.java +++ b/caching/src/main/java/com/iluwatar/caching/AppManager.java @@ -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); diff --git a/caching/src/main/java/com/iluwatar/caching/CacheStore.java b/caching/src/main/java/com/iluwatar/caching/CacheStore.java index e221f16e7..17a733188 100644 --- a/caching/src/main/java/com/iluwatar/caching/CacheStore.java +++ b/caching/src/main/java/com/iluwatar/caching/CacheStore.java @@ -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 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); diff --git a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java index 23be7d7e2..6bc6dbd77 100644 --- a/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java +++ b/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java @@ -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"); diff --git a/caching/src/main/java/com/iluwatar/caching/DbManager.java b/caching/src/main/java/com/iluwatar/caching/DbManager.java index 01b727fa5..dbb885ee2 100644 --- a/caching/src/main/java/com/iluwatar/caching/DbManager.java +++ b/caching/src/main/java/com/iluwatar/caching/DbManager.java @@ -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; /** + *

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.

* - *

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.

- * - *

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()).

- * + *

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()).

*/ 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 iterable = - db.getCollection(CachingConstants.USER_ACCOUNT).find(new Document(CachingConstants.USER_ID, userId)); + FindIterable 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) + ); } } diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java index 0f2e53823..32bb3838e 100644 --- a/caching/src/main/java/com/iluwatar/caching/LruCache.java +++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java @@ -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; } diff --git a/caching/src/main/java/com/iluwatar/caching/UserAccount.java b/caching/src/main/java/com/iluwatar/caching/UserAccount.java index fc6b8cb4c..4c58cfb08 100644 --- a/caching/src/main/java/com/iluwatar/caching/UserAccount.java +++ b/caching/src/main/java/com/iluwatar/caching/UserAccount.java @@ -32,7 +32,7 @@ public class UserAccount { private String additionalInfo; /** - * Constructor + * Constructor. */ public UserAccount(String userId, String userName, String additionalInfo) { this.userId = userId; 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 7e53f61f0..86d2bd2a7 100644 --- a/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java +++ b/caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java @@ -24,9 +24,7 @@ package com.iluwatar.caching.constants; /** - * - * Constant class for defining constants - * + * Constant class for defining constants. */ public class CachingConstants {