Skip to content

Commit 4903984

Browse files
committed
Fix issues in VALIDATE phase
1 parent dabe4d2 commit 4903984

File tree

9 files changed

+381
-365
lines changed

9 files changed

+381
-365
lines changed

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

Lines changed: 150 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -54,155 +54,155 @@
5454
*/
5555
@Slf4j
5656
public class App {
57-
/**
58-
* Constant parameter name to use mongoDB.
59-
*/
60-
private static final String USE_MONGO_DB = "--mongo";
61-
/**
62-
* Application manager.
63-
*/
64-
private final AppManager appManager;
65-
66-
/**
67-
* Constructor of current App.
68-
* @param isMongo boolean
69-
*/
70-
public App(final boolean isMongo) {
71-
DbManager dbManager = DbManagerFactory.initDb(isMongo);
72-
appManager = new AppManager(dbManager);
73-
appManager.initDb();
74-
}
75-
76-
/**
77-
* Program entry point.
78-
*
79-
* @param args command line args
80-
*/
81-
public static void main(final String[] args) {
82-
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
83-
// and the App class to avoid Maven compilation errors. Set flag to
84-
// true to run the tests with MongoDB (provided that MongoDB is
85-
// installed and socket connection is open).
86-
App app = new App(isDbMongo(args));
87-
app.useReadAndWriteThroughStrategy();
88-
String splitLine = "==============================================";
89-
LOGGER.info(splitLine);
90-
app.useReadThroughAndWriteAroundStrategy();
91-
LOGGER.info(splitLine);
92-
app.useReadThroughAndWriteBehindStrategy();
93-
LOGGER.info(splitLine);
94-
app.useCacheAsideStategy();
95-
LOGGER.info(splitLine);
96-
}
97-
98-
/**
99-
* Check the input parameters. if
100-
* @param args input params
101-
* @return true if there is "--mongo" parameter in arguments
102-
*/
103-
private static boolean isDbMongo(final String[] args) {
104-
for (String arg : args) {
105-
if (arg.equals(USE_MONGO_DB)) {
106-
return true;
107-
}
108-
}
109-
return false;
110-
}
111-
112-
/**
113-
* Read-through and write-through.
114-
*/
115-
public void useReadAndWriteThroughStrategy() {
116-
LOGGER.info("# CachingPolicy.THROUGH");
117-
appManager.initCachingPolicy(CachingPolicy.THROUGH);
118-
119-
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
120-
121-
appManager.save(userAccount1);
122-
LOGGER.info(appManager.printCacheContent());
123-
appManager.find("001");
124-
appManager.find("001");
125-
}
126-
127-
/**
128-
* Read-through and write-around.
129-
*/
130-
public void useReadThroughAndWriteAroundStrategy() {
131-
LOGGER.info("# CachingPolicy.AROUND");
132-
appManager.initCachingPolicy(CachingPolicy.AROUND);
133-
134-
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
135-
136-
appManager.save(userAccount2);
137-
LOGGER.info(appManager.printCacheContent());
138-
appManager.find("002");
139-
LOGGER.info(appManager.printCacheContent());
140-
userAccount2 = appManager.find("002");
141-
userAccount2.setUserName("Jane G.");
142-
appManager.save(userAccount2);
143-
LOGGER.info(appManager.printCacheContent());
144-
appManager.find("002");
145-
LOGGER.info(appManager.printCacheContent());
146-
appManager.find("002");
147-
}
148-
149-
/**
150-
* Read-through and write-behind.
151-
*/
152-
public void useReadThroughAndWriteBehindStrategy() {
153-
LOGGER.info("# CachingPolicy.BEHIND");
154-
appManager.initCachingPolicy(CachingPolicy.BEHIND);
155-
156-
var userAccount3 = new UserAccount("003",
157-
"Adam",
158-
"He likes food.");
159-
var userAccount4 = new UserAccount("004",
160-
"Rita",
161-
"She hates cats.");
162-
var userAccount5 = new UserAccount("005",
163-
"Isaac",
164-
"He is allergic to mustard.");
165-
166-
appManager.save(userAccount3);
167-
appManager.save(userAccount4);
168-
appManager.save(userAccount5);
169-
LOGGER.info(appManager.printCacheContent());
170-
appManager.find("003");
171-
LOGGER.info(appManager.printCacheContent());
172-
UserAccount userAccount6 = new UserAccount("006",
173-
"Yasha",
174-
"She is an only child.");
175-
appManager.save(userAccount6);
176-
LOGGER.info(appManager.printCacheContent());
177-
appManager.find("004");
178-
LOGGER.info(appManager.printCacheContent());
179-
}
180-
181-
/**
182-
* Cache-Aside.
183-
*/
184-
public void useCacheAsideStategy() {
185-
LOGGER.info("# CachingPolicy.ASIDE");
186-
appManager.initCachingPolicy(CachingPolicy.ASIDE);
187-
LOGGER.info(appManager.printCacheContent());
188-
189-
var userAccount3 = new UserAccount("003",
190-
"Adam",
191-
"He likes food.");
192-
var userAccount4 = new UserAccount("004",
193-
"Rita",
194-
"She hates cats.");
195-
var userAccount5 = new UserAccount("005",
196-
"Isaac",
197-
"He is allergic to mustard.");
198-
appManager.save(userAccount3);
199-
appManager.save(userAccount4);
200-
appManager.save(userAccount5);
201-
202-
LOGGER.info(appManager.printCacheContent());
203-
appManager.find("003");
204-
LOGGER.info(appManager.printCacheContent());
205-
appManager.find("004");
206-
LOGGER.info(appManager.printCacheContent());
57+
/**
58+
* Constant parameter name to use mongoDB.
59+
*/
60+
private static final String USE_MONGO_DB = "--mongo";
61+
/**
62+
* Application manager.
63+
*/
64+
private final AppManager appManager;
65+
66+
/**
67+
* Constructor of current App.
68+
* @param isMongo boolean
69+
*/
70+
public App(final boolean isMongo) {
71+
DbManager dbManager = DbManagerFactory.initDb(isMongo);
72+
appManager = new AppManager(dbManager);
73+
appManager.initDb();
74+
}
75+
76+
/**
77+
* Program entry point.
78+
*
79+
* @param args command line args
80+
*/
81+
public static void main(final String[] args) {
82+
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
83+
// and the App class to avoid Maven compilation errors. Set flag to
84+
// true to run the tests with MongoDB (provided that MongoDB is
85+
// installed and socket connection is open).
86+
App app = new App(isDbMongo(args));
87+
app.useReadAndWriteThroughStrategy();
88+
String splitLine = "==============================================";
89+
LOGGER.info(splitLine);
90+
app.useReadThroughAndWriteAroundStrategy();
91+
LOGGER.info(splitLine);
92+
app.useReadThroughAndWriteBehindStrategy();
93+
LOGGER.info(splitLine);
94+
app.useCacheAsideStategy();
95+
LOGGER.info(splitLine);
96+
}
97+
98+
/**
99+
* Check the input parameters. if
100+
* @param args input params
101+
* @return true if there is "--mongo" parameter in arguments
102+
*/
103+
private static boolean isDbMongo(final String[] args) {
104+
for (String arg : args) {
105+
if (arg.equals(USE_MONGO_DB)) {
106+
return true;
107+
}
207108
}
109+
return false;
110+
}
111+
112+
/**
113+
* Read-through and write-through.
114+
*/
115+
public void useReadAndWriteThroughStrategy() {
116+
LOGGER.info("# CachingPolicy.THROUGH");
117+
appManager.initCachingPolicy(CachingPolicy.THROUGH);
118+
119+
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
120+
121+
appManager.save(userAccount1);
122+
LOGGER.info(appManager.printCacheContent());
123+
appManager.find("001");
124+
appManager.find("001");
125+
}
126+
127+
/**
128+
* Read-through and write-around.
129+
*/
130+
public void useReadThroughAndWriteAroundStrategy() {
131+
LOGGER.info("# CachingPolicy.AROUND");
132+
appManager.initCachingPolicy(CachingPolicy.AROUND);
133+
134+
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
135+
136+
appManager.save(userAccount2);
137+
LOGGER.info(appManager.printCacheContent());
138+
appManager.find("002");
139+
LOGGER.info(appManager.printCacheContent());
140+
userAccount2 = appManager.find("002");
141+
userAccount2.setUserName("Jane G.");
142+
appManager.save(userAccount2);
143+
LOGGER.info(appManager.printCacheContent());
144+
appManager.find("002");
145+
LOGGER.info(appManager.printCacheContent());
146+
appManager.find("002");
147+
}
148+
149+
/**
150+
* Read-through and write-behind.
151+
*/
152+
public void useReadThroughAndWriteBehindStrategy() {
153+
LOGGER.info("# CachingPolicy.BEHIND");
154+
appManager.initCachingPolicy(CachingPolicy.BEHIND);
155+
156+
var userAccount3 = new UserAccount("003",
157+
"Adam",
158+
"He likes food.");
159+
var userAccount4 = new UserAccount("004",
160+
"Rita",
161+
"She hates cats.");
162+
var userAccount5 = new UserAccount("005",
163+
"Isaac",
164+
"He is allergic to mustard.");
165+
166+
appManager.save(userAccount3);
167+
appManager.save(userAccount4);
168+
appManager.save(userAccount5);
169+
LOGGER.info(appManager.printCacheContent());
170+
appManager.find("003");
171+
LOGGER.info(appManager.printCacheContent());
172+
UserAccount userAccount6 = new UserAccount("006",
173+
"Yasha",
174+
"She is an only child.");
175+
appManager.save(userAccount6);
176+
LOGGER.info(appManager.printCacheContent());
177+
appManager.find("004");
178+
LOGGER.info(appManager.printCacheContent());
179+
}
180+
181+
/**
182+
* Cache-Aside.
183+
*/
184+
public void useCacheAsideStategy() {
185+
LOGGER.info("# CachingPolicy.ASIDE");
186+
appManager.initCachingPolicy(CachingPolicy.ASIDE);
187+
LOGGER.info(appManager.printCacheContent());
188+
189+
var userAccount3 = new UserAccount("003",
190+
"Adam",
191+
"He likes food.");
192+
var userAccount4 = new UserAccount("004",
193+
"Rita",
194+
"She hates cats.");
195+
var userAccount5 = new UserAccount("005",
196+
"Isaac",
197+
"He is allergic to mustard.");
198+
appManager.save(userAccount3);
199+
appManager.save(userAccount4);
200+
appManager.save(userAccount5);
201+
202+
LOGGER.info(appManager.printCacheContent());
203+
appManager.find("003");
204+
LOGGER.info(appManager.printCacheContent());
205+
appManager.find("004");
206+
LOGGER.info(appManager.printCacheContent());
207+
}
208208
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
package com.iluwatar.caching;
2525

26+
import com.iluwatar.caching.database.DbManager;
2627
import java.util.Optional;
2728

28-
import com.iluwatar.caching.database.DbManager;
2929
import lombok.Data;
3030
import lombok.extern.slf4j.Slf4j;
3131

@@ -68,7 +68,7 @@ public AppManager(final DbManager newDbManager) {
6868
* to (temporarily) store the data/objects during runtime.
6969
*/
7070
public void initDb() {
71-
dbManager.connect();
71+
dbManager.connect();
7272
}
7373

7474
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
package com.iluwatar.caching;
2525

26+
import com.iluwatar.caching.database.DbManager;
2627
import java.util.List;
2728
import java.util.Optional;
2829
import java.util.stream.Collectors;
2930

30-
import com.iluwatar.caching.database.DbManager;
3131
import lombok.extern.slf4j.Slf4j;
3232

3333
/**

0 commit comments

Comments
 (0)