mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 01:26:35 +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:
@@ -66,14 +66,14 @@ public class App {
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,10 @@ package com.iluwatar.dao;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/**
|
||||
* Custom exception.
|
||||
*/
|
||||
/** Custom exception. */
|
||||
public class CustomException extends Exception {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
|
||||
@@ -30,9 +30,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* A customer POJO that represents the data that will be read from the data source.
|
||||
*/
|
||||
/** A customer POJO that represents the data that will be read from the data source. */
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@@ -40,8 +38,7 @@ import lombok.ToString;
|
||||
@AllArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
@EqualsAndHashCode.Include
|
||||
private int id;
|
||||
@EqualsAndHashCode.Include private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
}
|
||||
|
||||
@@ -24,18 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
/**
|
||||
* Customer Schema SQL Class.
|
||||
*/
|
||||
/** Customer Schema SQL Class. */
|
||||
public final class CustomerSchemaSql {
|
||||
|
||||
private CustomerSchemaSql() {
|
||||
}
|
||||
private CustomerSchemaSql() {}
|
||||
|
||||
public static final String CREATE_SCHEMA_SQL =
|
||||
"CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
+ "LNAME VARCHAR(100))";
|
||||
"CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), " + "LNAME VARCHAR(100))";
|
||||
|
||||
public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
|
||||
|
||||
}
|
||||
|
||||
@@ -38,9 +38,7 @@ import javax.sql.DataSource;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
|
||||
*/
|
||||
/** An implementation of {@link CustomerDao} that persists customers in RDBMS. */
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class DbCustomerDao implements CustomerDao {
|
||||
@@ -60,22 +58,24 @@ public class DbCustomerDao implements CustomerDao {
|
||||
var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
|
||||
var resultSet = statement.executeQuery(); // NOSONAR
|
||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||
Spliterator.ORDERED) {
|
||||
return StreamSupport.stream(
|
||||
new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE, Spliterator.ORDERED) {
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Customer> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createCustomer(resultSet));
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Customer> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createCustomer(resultSet));
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
},
|
||||
false)
|
||||
.onClose(() -> mutedClose(connection, statement, resultSet));
|
||||
} catch (SQLException e) {
|
||||
throw new CustomException(e.getMessage(), e);
|
||||
}
|
||||
@@ -96,21 +96,18 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
private Customer createCustomer(ResultSet resultSet) throws SQLException {
|
||||
return new Customer(resultSet.getInt("ID"),
|
||||
resultSet.getString("FNAME"),
|
||||
resultSet.getString("LNAME"));
|
||||
return new Customer(
|
||||
resultSet.getInt("ID"), resultSet.getString("FNAME"), resultSet.getString("LNAME"));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<Customer> getById(int id) throws Exception {
|
||||
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
|
||||
statement.setInt(1, id);
|
||||
resultSet = statement.executeQuery();
|
||||
@@ -128,9 +125,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean add(Customer customer) throws Exception {
|
||||
if (getById(customer.getId()).isPresent()) {
|
||||
@@ -138,7 +133,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
statement.setString(2, customer.getFirstName());
|
||||
statement.setString(3, customer.getLastName());
|
||||
@@ -149,15 +144,12 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean update(Customer customer) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement =
|
||||
connection
|
||||
.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
var statement =
|
||||
connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
statement.setString(1, customer.getFirstName());
|
||||
statement.setString(2, customer.getLastName());
|
||||
statement.setInt(3, customer.getId());
|
||||
@@ -167,13 +159,11 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean delete(Customer customer) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
||||
|
||||
@@ -31,17 +31,14 @@ import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory and
|
||||
* data is lost when the application exits.
|
||||
* <br>
|
||||
* data is lost when the application exits. <br>
|
||||
* This implementation is useful as temporary database or for testing.
|
||||
*/
|
||||
public class InMemoryCustomerDao implements CustomerDao {
|
||||
|
||||
private final Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||
|
||||
/**
|
||||
* An eagerly evaluated stream of customers stored in memory.
|
||||
*/
|
||||
/** An eagerly evaluated stream of customers stored in memory. */
|
||||
@Override
|
||||
public Stream<Customer> getAll() {
|
||||
return idToCustomer.values().stream();
|
||||
|
||||
Reference in New Issue
Block a user