Java 11 migration: patterns (remaining b-c) (#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
6ef840f3cf
commit
33ea7335b1
@ -76,7 +76,7 @@ public class App {
|
||||
// true to run the tests with MongoDB (provided that MongoDB is
|
||||
// installed and socket connection is open).
|
||||
AppManager.initCacheCapacity(3);
|
||||
App app = new App();
|
||||
var app = new App();
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
@ -90,7 +90,7 @@ public class App {
|
||||
LOGGER.info("# CachingPolicy.THROUGH");
|
||||
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
|
||||
|
||||
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
||||
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
||||
|
||||
AppManager.save(userAccount1);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
@ -105,7 +105,7 @@ public class App {
|
||||
LOGGER.info("# CachingPolicy.AROUND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.AROUND);
|
||||
|
||||
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
||||
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
||||
|
||||
AppManager.save(userAccount2);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
@ -127,9 +127,9 @@ public class App {
|
||||
LOGGER.info("# CachingPolicy.BEHIND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
|
||||
|
||||
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
|
||||
AppManager.save(userAccount3);
|
||||
AppManager.save(userAccount4);
|
||||
@ -152,9 +152,9 @@ public class App {
|
||||
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
|
||||
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
AppManager.save(userAccount3);
|
||||
AppManager.save(userAccount4);
|
||||
AppManager.save(userAccount5);
|
||||
|
@ -24,6 +24,7 @@
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* AppManager helps to bridge the gap in communication between the main class and the application's
|
||||
@ -116,16 +117,12 @@ public final class AppManager {
|
||||
* Cache-Aside find user account helper.
|
||||
*/
|
||||
private static UserAccount findAside(String userId) {
|
||||
UserAccount userAccount = CacheStore.get(userId);
|
||||
if (userAccount != null) {
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
userAccount = DbManager.readFromDb(userId);
|
||||
if (userAccount != null) {
|
||||
CacheStore.set(userId, userAccount);
|
||||
}
|
||||
|
||||
return userAccount;
|
||||
return Optional.ofNullable(CacheStore.get(userId))
|
||||
.or(() -> {
|
||||
Optional<UserAccount> userAccount = Optional.ofNullable(DbManager.readFromDb(userId));
|
||||
userAccount.ifPresent(account -> CacheStore.set(userId, account));
|
||||
return userAccount;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,8 @@
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -34,7 +36,7 @@ public class CacheStore {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CacheStore.class);
|
||||
|
||||
static LruCache cache;
|
||||
private static LruCache cache;
|
||||
|
||||
private CacheStore() {
|
||||
}
|
||||
@ -134,27 +136,22 @@ public class CacheStore {
|
||||
*/
|
||||
public static void flushCache() {
|
||||
LOGGER.info("# flushCache...");
|
||||
if (null == cache) {
|
||||
return;
|
||||
}
|
||||
List<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||
for (UserAccount userAccount : listOfUserAccounts) {
|
||||
DbManager.upsertDb(userAccount);
|
||||
}
|
||||
Optional.ofNullable(cache)
|
||||
.map(LruCache::getCacheDataInListForm)
|
||||
.orElse(List.of())
|
||||
.forEach(DbManager::updateDb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print user accounts.
|
||||
*/
|
||||
public static String print() {
|
||||
List<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\n--CACHE CONTENT--\n");
|
||||
for (UserAccount userAccount : listOfUserAccounts) {
|
||||
sb.append(userAccount.toString() + "\n");
|
||||
}
|
||||
sb.append("----\n");
|
||||
return sb.toString();
|
||||
return Optional.ofNullable(cache)
|
||||
.map(LruCache::getCacheDataInListForm)
|
||||
.orElse(List.of())
|
||||
.stream()
|
||||
.map(userAccount -> userAccount.toString() + "\n")
|
||||
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,7 +25,6 @@ package com.iluwatar.caching;
|
||||
|
||||
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;
|
||||
@ -87,7 +86,7 @@ public final class DbManager {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
FindIterable<Document> iterable = db
|
||||
var iterable = db
|
||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.find(new Document(CachingConstants.USER_ID, userId));
|
||||
if (iterable == null) {
|
||||
|
@ -67,7 +67,7 @@ public class LruCache {
|
||||
*/
|
||||
public UserAccount get(String userId) {
|
||||
if (cache.containsKey(userId)) {
|
||||
Node node = cache.get(userId);
|
||||
var node = cache.get(userId);
|
||||
remove(node);
|
||||
setHead(node);
|
||||
return node.userAccount;
|
||||
@ -111,12 +111,12 @@ public class LruCache {
|
||||
*/
|
||||
public void set(String userId, UserAccount userAccount) {
|
||||
if (cache.containsKey(userId)) {
|
||||
Node old = cache.get(userId);
|
||||
var old = cache.get(userId);
|
||||
old.userAccount = userAccount;
|
||||
remove(old);
|
||||
setHead(old);
|
||||
} else {
|
||||
Node newNode = new Node(userId, userAccount);
|
||||
var newNode = new Node(userId, userAccount);
|
||||
if (cache.size() >= capacity) {
|
||||
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
|
||||
cache.remove(end.userId); // remove LRU data from cache.
|
||||
@ -137,7 +137,7 @@ public class LruCache {
|
||||
* Invalidate cache for user.
|
||||
*/
|
||||
public void invalidate(String userId) {
|
||||
Node toBeRemoved = cache.remove(userId);
|
||||
var toBeRemoved = cache.remove(userId);
|
||||
if (toBeRemoved != null) {
|
||||
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
||||
remove(toBeRemoved);
|
||||
@ -165,8 +165,8 @@ public class LruCache {
|
||||
* Returns cache data in list form.
|
||||
*/
|
||||
public List<UserAccount> getCacheDataInListForm() {
|
||||
List<UserAccount> listOfCacheData = new ArrayList<>();
|
||||
Node temp = head;
|
||||
var listOfCacheData = new ArrayList<UserAccount>();
|
||||
var temp = head;
|
||||
while (temp != null) {
|
||||
listOfCacheData.add(temp.userAccount);
|
||||
temp = temp.next;
|
||||
|
Reference in New Issue
Block a user