Skip to content

Commit 3c213fc

Browse files
committed
Fix 40 errors from checkstyle plugin run. 139 left))
1 parent a437f5b commit 3c213fc

File tree

5 files changed

+173
-85
lines changed

5 files changed

+173
-85
lines changed

caching/src/main/java/com/iluwatar/caching/App.java

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,46 @@
1-
/*
2-
* The MIT License
3-
* Copyright © 2014-2021 Ilkka Seppälä
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
11-
*
12-
* The above copyright notice and this permission notice shall be included in
13-
* all copies or substantial portions of the Software.
14-
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
* THE SOFTWARE.
22-
*/
23-
241
package com.iluwatar.caching;
252

263
import com.iluwatar.caching.database.DbManager;
274
import com.iluwatar.caching.database.DbManagerFactory;
285
import lombok.extern.slf4j.Slf4j;
296

307
/**
31-
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
32-
* the resources immediately after their use. The resources retain their identity, are kept in some
33-
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
34-
* caching strategies/techniques in this pattern; each with their own pros and cons. They are;
35-
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
36-
* <code>write-around</code> which writes data immediately into the DB instead of the cache,
37-
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
38-
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
39-
* responsibility of keeping the data synchronized in both data sources to the application itself.
40-
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
41-
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
42-
* stores it into the cache for future use. These strategies determine when the data in the cache
43-
* should be written back to the backing store (i.e. Database) and help keep both data sources
44-
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
45-
* consistency between data held in the cache and the data in the underlying data store.
8+
* The Caching pattern describes how to avoid expensive re-acquisition of
9+
* resources by not releasing the resources immediately after their use.
10+
* The resources retain their identity, are kept in some fast-access storage,
11+
* and are re-used to avoid having to acquire them again. There are four main
12+
* caching strategies/techniques in this pattern; each with their own pros and
13+
* cons. They are <code>write-through</code> which writes data to the cache and
14+
* DB in a single transaction, <code>write-around</code> which writes data
15+
* immediately into the DB instead of the cache, <code>write-behind</code>
16+
* which writes data into the cache initially whilst the data is only
17+
* written into the DB when the cache is full, and <code>cache-aside</code>
18+
* which pushes the responsibility of keeping the data synchronized in both
19+
* data sources to the application itself. The <code>read-through</code>
20+
* strategy is also included in the mentioned four strategies --
21+
* returns data from the cache to the caller <b>if</b> it exists <b>else</b>
22+
* queries from DB and stores it into the cache for future use. These strategies
23+
* determine when the data in the cache should be written back to the backing
24+
* store (i.e. Database) and help keep both data sources
25+
* synchronized/up-to-date. This pattern can improve performance and also helps
26+
* to maintainconsistency between data held in the cache and the data in
27+
* the underlying data store.
4628
*
47-
* <p>In this example, the user account ({@link UserAccount}) entity is used as the underlying
48-
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
49-
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
50-
* strategies are individually tested. The testing of the cache is restricted towards saving and
51-
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
52-
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
53-
* whether the data is coming from the cache or the DB (i.e. separation of concern). The AppManager
54-
* ({@link AppManager}) handles the transaction of data to-and-from the underlying data store
55-
* (depending on the preferred caching policy/strategy).
29+
* <p>In this example, the user account ({@link UserAccount}) entity is used
30+
* as the underlying application data. The cache itself is implemented as an
31+
* internal (Java) data structure. It adopts a Least-Recently-Used (LRU)
32+
* strategy for evicting data from itself when its full. The four
33+
* strategies are individually tested. The testing of the cache is restricted
34+
* towards saving and querying of user accounts from the
35+
* underlying data store( {@link DbManager}). The main class ( {@link App}
36+
* is not aware of the underlying mechanics of the application
37+
* (i.e. save and query) and whether the data is coming from the cache or the
38+
* DB (i.e. separation of concern). The AppManager ({@link AppManager}) handles
39+
* the transaction of data to-and-from the underlying data store (depending on
40+
* the preferred caching policy/strategy).
5641
* <p>
57-
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i>
42+
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy -->
43+
* DBManager} </i>
5844
* </p>
5945
*
6046
* <p>
@@ -68,10 +54,20 @@
6854
*/
6955
@Slf4j
7056
public class App {
57+
/**
58+
* Constant parameter name to use mongoDB.
59+
*/
7160
private static final String USE_MONGO_DB = "--mongo";
72-
private AppManager appManager;
61+
/**
62+
* Application manager.
63+
*/
64+
private final AppManager appManager;
7365

74-
public App(boolean isMongo) {
66+
/**
67+
* Constructor of current App.
68+
* @param isMongo boolean
69+
*/
70+
public App(final boolean isMongo) {
7571
DbManager dbManager = DbManagerFactory.initDb(isMongo);
7672
appManager = new AppManager(dbManager);
7773
appManager.initDb();
@@ -82,28 +78,28 @@ public App(boolean isMongo) {
8278
*
8379
* @param args command line args
8480
*/
85-
public static void main(String[] args) {
81+
public static void main(final String[] args) {
8682
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
8783
// and the App class to avoid Maven compilation errors. Set flag to
8884
// true to run the tests with MongoDB (provided that MongoDB is
8985
// installed and socket connection is open).
9086
App app = new App(isDbMongo(args));
9187
app.useReadAndWriteThroughStrategy();
92-
System.out.println("=====================================================");
88+
System.out.println("==============================================");
9389
app.useReadThroughAndWriteAroundStrategy();
94-
System.out.println("=====================================================");
90+
System.out.println("==============================================");
9591
app.useReadThroughAndWriteBehindStrategy();
96-
System.out.println("=====================================================");
92+
System.out.println("==============================================");
9793
app.useCacheAsideStategy();
98-
System.out.println("=====================================================");
94+
System.out.println("==============================================");
9995
}
10096

10197
/**
10298
* Check the input parameters. if
10399
* @param args input params
104100
* @return true if there is "--mongo" parameter in arguments
105101
*/
106-
private static boolean isDbMongo(String[] args) {
102+
private static boolean isDbMongo(final String[] args) {
107103
for (String arg : args) {
108104
if (arg.equals(USE_MONGO_DB)) {
109105
return true;
@@ -156,17 +152,25 @@ public void useReadThroughAndWriteBehindStrategy() {
156152
LOGGER.info("# CachingPolicy.BEHIND");
157153
appManager.initCachingPolicy(CachingPolicy.BEHIND);
158154

159-
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
160-
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
161-
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
155+
var userAccount3 = new UserAccount("003",
156+
"Adam",
157+
"He likes food.");
158+
var userAccount4 = new UserAccount("004",
159+
"Rita",
160+
"She hates cats.");
161+
var userAccount5 = new UserAccount("005",
162+
"Isaac",
163+
"He is allergic to mustard.");
162164

163165
appManager.save(userAccount3);
164166
appManager.save(userAccount4);
165167
appManager.save(userAccount5);
166168
LOGGER.info(appManager.printCacheContent());
167169
appManager.find("003");
168170
LOGGER.info(appManager.printCacheContent());
169-
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
171+
UserAccount userAccount6 = new UserAccount("006",
172+
"Yasha",
173+
"She is an only child.");
170174
appManager.save(userAccount6);
171175
LOGGER.info(appManager.printCacheContent());
172176
appManager.find("004");
@@ -181,9 +185,15 @@ public void useCacheAsideStategy() {
181185
appManager.initCachingPolicy(CachingPolicy.ASIDE);
182186
LOGGER.info(appManager.printCacheContent());
183187

184-
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
185-
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
186-
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
188+
var userAccount3 = new UserAccount("003",
189+
"Adam",
190+
"He likes food.");
191+
var userAccount4 = new UserAccount("004",
192+
"Rita",
193+
"She hates cats.");
194+
var userAccount5 = new UserAccount("005",
195+
"Isaac",
196+
"He is allergic to mustard.");
187197
appManager.save(userAccount3);
188198
appManager.save(userAccount4);
189199
appManager.save(userAccount5);

caching/src/main/java/com/iluwatar/caching/AppManager.java

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,51 @@
2929
import lombok.extern.slf4j.Slf4j;
3030

3131
/**
32-
* AppManager helps to bridge the gap in communication between the main class and the application's
33-
* back-end. DB connection is initialized through this class. The chosen caching strategy/policy is
34-
* also initialized here. Before the cache can be used, the size of the cache has to be set.
35-
* Depending on the chosen caching policy, AppManager will call the appropriate function in the
36-
* CacheStore class.
32+
* AppManager helps to bridge the gap in communication between the main class
33+
* and the application's back-end. DB connection is initialized through this
34+
* class. The chosen caching strategy/policy is also initialized here.
35+
* Before the cache can be used, the size of the cache has to be set.
36+
* Depending on the chosen caching policy, AppManager will call the
37+
* appropriate function in the CacheStore class.
3738
*/
3839
@Slf4j
3940
public class AppManager {
40-
41+
/**
42+
* Caching Policy.
43+
*/
4144
private static CachingPolicy cachingPolicy;
45+
/**
46+
* Database Manager.
47+
*/
4248
private final DbManager dbManager;
49+
/**
50+
* Cache Store.
51+
*/
4352
private final CacheStore cacheStore;
4453

45-
public AppManager(DbManager dbManager) {
46-
this.dbManager = dbManager;
47-
this.cacheStore = new CacheStore(dbManager);
54+
/**
55+
* Constructor.
56+
* @param newDbManager database manager
57+
*/
58+
public AppManager(final DbManager newDbManager) {
59+
this.dbManager = newDbManager;
60+
this.cacheStore = new CacheStore(newDbManager);
4861
}
4962

5063
/**
51-
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
52-
* data storage or a simple Java data structure to (temporarily) store the data/objects during
53-
* runtime.
64+
* Developer/Tester is able to choose whether the application should use
65+
* MongoDB as its underlying data storage or a simple Java data structure
66+
* to (temporarily) store the data/objects during runtime.
5467
*/
5568
public void initDb() {
5669
dbManager.connect();
5770
}
5871

5972
/**
6073
* Initialize caching policy.
74+
* @param policy is a {@link CachingPolicy}
6175
*/
62-
public void initCachingPolicy(CachingPolicy policy) {
76+
public void initCachingPolicy(final CachingPolicy policy) {
6377
cachingPolicy = policy;
6478
if (cachingPolicy == CachingPolicy.BEHIND) {
6579
Runtime.getRuntime().addShutdownHook(new Thread(cacheStore::flushCache));
@@ -69,10 +83,13 @@ public void initCachingPolicy(CachingPolicy policy) {
6983

7084
/**
7185
* Find user account.
86+
* @param userId String
87+
* @return {@link UserAccount}
7288
*/
73-
public UserAccount find(String userId) {
89+
public UserAccount find(final String userId) {
7490
LOGGER.info("Trying to find {} in cache", userId);
75-
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
91+
if (cachingPolicy == CachingPolicy.THROUGH
92+
|| cachingPolicy == CachingPolicy.AROUND) {
7693
return cacheStore.readThrough(userId);
7794
} else if (cachingPolicy == CachingPolicy.BEHIND) {
7895
return cacheStore.readThroughWithWriteBackPolicy(userId);
@@ -84,8 +101,9 @@ public UserAccount find(String userId) {
84101

85102
/**
86103
* Save user account.
104+
* @param userAccount {@link UserAccount}
87105
*/
88-
public void save(UserAccount userAccount) {
106+
public void save(final UserAccount userAccount) {
89107
LOGGER.info("Save record!");
90108
if (cachingPolicy == CachingPolicy.THROUGH) {
91109
cacheStore.writeThrough(userAccount);
@@ -98,25 +116,33 @@ public void save(UserAccount userAccount) {
98116
}
99117
}
100118

119+
/**
120+
* Returns String.
121+
* @return String
122+
*/
101123
public String printCacheContent() {
102124
return cacheStore.print();
103125
}
104126

105127
/**
106128
* Cache-Aside save user account helper.
129+
* @param userAccount {@link UserAccount}
107130
*/
108-
private void saveAside(UserAccount userAccount) {
131+
private void saveAside(final UserAccount userAccount) {
109132
dbManager.updateDb(userAccount);
110133
cacheStore.invalidate(userAccount.getUserId());
111134
}
112135

113136
/**
114137
* Cache-Aside find user account helper.
138+
* @param userId String
139+
* @return {@link UserAccount}
115140
*/
116-
private UserAccount findAside(String userId) {
141+
private UserAccount findAside(final String userId) {
117142
return Optional.ofNullable(cacheStore.get(userId))
118143
.or(() -> {
119-
Optional<UserAccount> userAccount = Optional.ofNullable(dbManager.readFromDb(userId));
144+
Optional<UserAccount> userAccount =
145+
Optional.ofNullable(dbManager.readFromDb(userId));
120146
userAccount.ifPresent(account -> cacheStore.set(userId, account));
121147
return userAccount;
122148
})

caching/src/main/java/com/iluwatar/caching/database/DbManager.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,38 @@
33
import com.iluwatar.caching.UserAccount;
44

55
/**
6-
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains
7-
* the implemented methods for querying, inserting, and updating data. MongoDB was used as the
8-
* database for the application.</p>
6+
* <p>DBManager handles the communication with the underlying data store i.e.
7+
* Database. It contains the implemented methods for querying, inserting,
8+
* and updating data. MongoDB was used as the database for the application.</p>
99
*/
1010
public interface DbManager {
11+
/**
12+
* Connect to DB.
13+
*/
1114
void connect();
15+
16+
/**
17+
* Read from DB.
18+
* @param userId {@link String}
19+
* @return {@link UserAccount}
20+
*/
1221
UserAccount readFromDb(String userId);
22+
/**
23+
* Write to DB.
24+
* @param userAccount {@link UserAccount}
25+
* @return {@link UserAccount}
26+
*/
1327
UserAccount writeToDb(UserAccount userAccount);
28+
/**
29+
* Update record.
30+
* @param userAccount {@link UserAccount}
31+
* @return {@link UserAccount}
32+
*/
1433
UserAccount updateDb(UserAccount userAccount);
34+
/**
35+
* Update record or Insert if not exists.
36+
* @param userAccount {@link UserAccount}
37+
* @return {@link UserAccount}
38+
*/
1539
UserAccount upsertDb(UserAccount userAccount);
1640
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Database classes
3+
*/
4+
package com.iluwatar.caching.database;

0 commit comments

Comments
 (0)