mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-21 00:24:17 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -27,12 +27,12 @@ package com.iluwatar.identitymap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The basic idea behind the Identity Map is to have a series of maps containing objects that have been pulled from the database.
|
||||
* The below example demonstrates the identity map pattern by creating a sample DB.
|
||||
* Since only 1 DB has been created we only have 1 map corresponding to it for the purpose of this demo.
|
||||
* When you load an object from the database, you first check the map.
|
||||
* If there’s an object in it that corresponds to the one you’re loading, you return it. If not, you go to the database,
|
||||
* putting the objects on the map for future reference as you load them.
|
||||
* The basic idea behind the Identity Map is to have a series of maps containing objects that have
|
||||
* been pulled from the database. The below example demonstrates the identity map pattern by
|
||||
* creating a sample DB. Since only 1 DB has been created we only have 1 map corresponding to it for
|
||||
* the purpose of this demo. When you load an object from the database, you first check the map. If
|
||||
* there’s an object in it that corresponds to the one you’re loading, you return it. If not, you go
|
||||
* to the database, putting the objects on the map for future reference as you load them.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
/**
|
||||
* Using Runtime Exception to control the flow in case Person Id doesn't exist.
|
||||
*/
|
||||
/** Using Runtime Exception to control the flow in case Person Id doesn't exist. */
|
||||
public class IdNotFoundException extends RuntimeException {
|
||||
public IdNotFoundException(final String message) {
|
||||
super(message);
|
||||
|
||||
@@ -30,20 +30,20 @@ import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This class stores the map into which we will be caching records after loading them from a DataBase.
|
||||
* Stores the records as a Hash Map with the personNationalIDs as keys.
|
||||
* This class stores the map into which we will be caching records after loading them from a
|
||||
* DataBase. Stores the records as a Hash Map with the personNationalIDs as keys.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
public class IdentityMap {
|
||||
private Map<Integer, Person> personMap = new HashMap<>();
|
||||
/**
|
||||
* Add person to the map.
|
||||
*/
|
||||
|
||||
/** Add person to the map. */
|
||||
public void addPerson(Person person) {
|
||||
if (!personMap.containsKey(person.getPersonNationalId())) {
|
||||
personMap.put(person.getPersonNationalId(), person);
|
||||
} else { // Ensure that addPerson does not update a record. This situation will never arise in our implementation. Added only for testing purposes.
|
||||
} else { // Ensure that addPerson does not update a record. This situation will never arise in
|
||||
// our implementation. Added only for testing purposes.
|
||||
LOGGER.info("Key already in Map");
|
||||
}
|
||||
}
|
||||
@@ -63,14 +63,11 @@ public class IdentityMap {
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the map.
|
||||
*/
|
||||
/** Get the size of the map. */
|
||||
public int size() {
|
||||
if (personMap == null) {
|
||||
return 0;
|
||||
}
|
||||
return personMap.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,28 +31,27 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Person definition.
|
||||
*/
|
||||
/** Person definition. */
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public final class Person implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@EqualsAndHashCode.Include
|
||||
private int personNationalId;
|
||||
@EqualsAndHashCode.Include private int personNationalId;
|
||||
private String name;
|
||||
private long phoneNum;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
return "Person ID is : " + personNationalId + " ; Person Name is : " + name + " ; Phone Number is :" + phoneNum;
|
||||
|
||||
return "Person ID is : "
|
||||
+ personNationalId
|
||||
+ " ; Person Name is : "
|
||||
+ name
|
||||
+ " ; Phone Number is :"
|
||||
+ phoneNum;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.identitymap;
|
||||
|
||||
/**
|
||||
* Simulator interface for Person DB.
|
||||
*/
|
||||
/** Simulator interface for Person DB. */
|
||||
public interface PersonDbSimulator {
|
||||
Person find(int personNationalId);
|
||||
|
||||
@@ -35,5 +33,4 @@ public interface PersonDbSimulator {
|
||||
void update(Person person);
|
||||
|
||||
void delete(int personNationalId);
|
||||
|
||||
}
|
||||
|
||||
+21
-17
@@ -30,26 +30,26 @@ import java.util.Optional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This is a sample database implementation. The database is in the form of an arraylist which stores records of
|
||||
* different persons. The personNationalId acts as the primary key for a record.
|
||||
* Operations :
|
||||
* -> find (look for object with a particular ID)
|
||||
* -> insert (insert record for a new person into the database)
|
||||
* -> update (update the record of a person). To do this, create a new person instance with the same ID as the record you
|
||||
* want to update. Then call this method with that person as an argument.
|
||||
* -> delete (delete the record for a particular ID)
|
||||
* This is a sample database implementation. The database is in the form of an arraylist which
|
||||
* stores records of different persons. The personNationalId acts as the primary key for a record.
|
||||
* Operations : -> find (look for object with a particular ID) -> insert (insert record for a new
|
||||
* person into the database) -> update (update the record of a person). To do this, create a new
|
||||
* person instance with the same ID as the record you want to update. Then call this method with
|
||||
* that person as an argument. -> delete (delete the record for a particular ID)
|
||||
*/
|
||||
@Slf4j
|
||||
public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
|
||||
//This simulates a table in the database. To extend logic to multiple tables just add more lists to the implementation.
|
||||
// This simulates a table in the database. To extend logic to multiple tables just add more lists
|
||||
// to the implementation.
|
||||
private List<Person> personList = new ArrayList<>();
|
||||
static final String NOT_IN_DATA_BASE = " not in DataBase";
|
||||
static final String ID_STR = "ID : ";
|
||||
|
||||
@Override
|
||||
public Person find(int personNationalId) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == personNationalId).findFirst();
|
||||
Optional<Person> elem =
|
||||
personList.stream().filter(p -> p.getPersonNationalId() == personNationalId).findFirst();
|
||||
if (elem.isEmpty()) {
|
||||
throw new IdNotFoundException(ID_STR + personNationalId + NOT_IN_DATA_BASE);
|
||||
}
|
||||
@@ -59,7 +59,10 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
|
||||
@Override
|
||||
public void insert(Person person) {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
|
||||
Optional<Person> elem =
|
||||
personList.stream()
|
||||
.filter(p -> p.getPersonNationalId() == person.getPersonNationalId())
|
||||
.findFirst();
|
||||
if (elem.isPresent()) {
|
||||
LOGGER.info("Record already exists.");
|
||||
return;
|
||||
@@ -69,7 +72,10 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
|
||||
@Override
|
||||
public void update(Person person) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
|
||||
Optional<Person> elem =
|
||||
personList.stream()
|
||||
.filter(p -> p.getPersonNationalId() == person.getPersonNationalId())
|
||||
.findFirst();
|
||||
if (elem.isPresent()) {
|
||||
elem.get().setName(person.getName());
|
||||
elem.get().setPhoneNum(person.getPhoneNum());
|
||||
@@ -85,7 +91,8 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
* @param id : personNationalId for person whose record is to be deleted.
|
||||
*/
|
||||
public void delete(int id) throws IdNotFoundException {
|
||||
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == id).findFirst();
|
||||
Optional<Person> elem =
|
||||
personList.stream().filter(p -> p.getPersonNationalId() == id).findFirst();
|
||||
if (elem.isPresent()) {
|
||||
personList.remove(elem.get());
|
||||
LOGGER.info("Record deleted successfully.");
|
||||
@@ -94,14 +101,11 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
|
||||
throw new IdNotFoundException(ID_STR + id + NOT_IN_DATA_BASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the size of the database.
|
||||
*/
|
||||
/** Return the size of the database. */
|
||||
public int size() {
|
||||
if (personList == null) {
|
||||
return 0;
|
||||
}
|
||||
return personList.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Any object of this class stores a DataBase and an Identity Map. When we try to look for a key we first check if
|
||||
* it has been cached in the Identity Map and return it if it is indeed in the map.
|
||||
* If that is not the case then go to the DataBase, get the record, store it in the
|
||||
* Identity Map and then return the record. Now if we look for the record again we will find it in the table itself which
|
||||
* will make lookup faster.
|
||||
* Any object of this class stores a DataBase and an Identity Map. When we try to look for a key we
|
||||
* first check if it has been cached in the Identity Map and return it if it is indeed in the map.
|
||||
* If that is not the case then go to the DataBase, get the record, store it in the Identity Map and
|
||||
* then return the record. Now if we look for the record again we will find it in the table itself
|
||||
* which will make lookup faster.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@@ -43,6 +43,7 @@ public class PersonFinder {
|
||||
// Access to the Identity Map
|
||||
private IdentityMap identityMap = new IdentityMap();
|
||||
private PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
|
||||
|
||||
/**
|
||||
* get person corresponding to input ID.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user