Java 11 migrate c-d (remaining) (#1111)

* Moves converter pattern to Java 11

* Moves cqrs pattern to Java 11

* Moves dao pattern to Java 11

* Moves data-bus pattern to Java 11

* Moves data-locality pattern to Java 11

* Moves data-mapper pattern to Java 11

* Moves data-transfer-object pattern to Java 11

* Moves decorator pattern to Java 11

* Moves delegation pattern to Java 11

* Moves dependency-injection to Java 11

* Moves dirty-flag to Java 11

* Moves double-buffer to Java 11

* Moves double-checked-locking to Java 11

* Moves double-dispatch to Java 11

* Corrects with changes thats breaking test cases
This commit is contained in:
Anurag Agarwal
2019-12-15 00:02:45 +05:30
committed by Ilkka Seppälä
parent 5681684157
commit ea57934db6
75 changed files with 576 additions and 713 deletions
+15 -18
View File
@@ -23,11 +23,8 @@
package com.iluwatar.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.slf4j.Logger;
@@ -57,32 +54,32 @@ public class App {
* @throws Exception if any error occurs.
*/
public static void main(final String[] args) throws Exception {
final CustomerDao inMemoryDao = new InMemoryCustomerDao();
final var inMemoryDao = new InMemoryCustomerDao();
performOperationsUsing(inMemoryDao);
final DataSource dataSource = createDataSource();
final var dataSource = createDataSource();
createSchema(dataSource);
final CustomerDao dbDao = new DbCustomerDao(dataSource);
final var dbDao = new DbCustomerDao(dataSource);
performOperationsUsing(dbDao);
deleteSchema(dataSource);
}
private static void deleteSchema(DataSource dataSource) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
}
}
private static void createSchema(DataSource dataSource) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
}
}
private static DataSource createDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
var dataSource = new JdbcDataSource();
dataSource.setURL(DB_URL);
return dataSource;
}
@@ -90,18 +87,18 @@ public class App {
private static void performOperationsUsing(final CustomerDao customerDao) throws Exception {
addCustomers(customerDao);
log.info(ALL_CUSTOMERS);
try (Stream<Customer> customerStream = customerDao.getAll()) {
try (var customerStream = customerDao.getAll()) {
customerStream.forEach((customer) -> log.info(customer.toString()));
}
log.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
final Customer customer = new Customer(4, "Dan", "Danson");
final var customer = new Customer(4, "Dan", "Danson");
customerDao.add(customer);
log.info(ALL_CUSTOMERS + customerDao.getAll());
customer.setFirstName("Daniel");
customer.setLastName("Danielson");
customerDao.update(customer);
log.info(ALL_CUSTOMERS);
try (Stream<Customer> customerStream = customerDao.getAll()) {
try (var customerStream = customerDao.getAll()) {
customerStream.forEach((cust) -> log.info(cust.toString()));
}
customerDao.delete(customer);
@@ -109,7 +106,7 @@ public class App {
}
private static void addCustomers(CustomerDao customerDao) throws Exception {
for (Customer customer : generateSampleCustomers()) {
for (var customer : generateSampleCustomers()) {
customerDao.add(customer);
}
}
@@ -120,9 +117,9 @@ public class App {
* @return list of customers.
*/
public static List<Customer> generateSampleCustomers() {
final Customer customer1 = new Customer(1, "Adam", "Adamson");
final Customer customer2 = new Customer(2, "Bob", "Bobson");
final Customer customer3 = new Customer(3, "Carl", "Carlson");
final var customer1 = new Customer(1, "Adam", "Adamson");
final var customer2 = new Customer(2, "Bob", "Bobson");
final var customer3 = new Customer(3, "Carl", "Carlson");
return List.of(customer1, customer2, customer3);
}
}
@@ -73,11 +73,11 @@ public class Customer {
@Override
public boolean equals(final Object that) {
boolean isEqual = false;
var isEqual = false;
if (this == that) {
isEqual = true;
} else if (that != null && getClass() == that.getClass()) {
final Customer customer = (Customer) that;
final var customer = (Customer) that;
if (getId() == customer.getId()) {
isEqual = true;
}
@@ -65,13 +65,10 @@ public class DbCustomerDao implements CustomerDao {
*/
@Override
public Stream<Customer> getAll() throws Exception {
Connection connection;
try {
connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
ResultSet resultSet = statement.executeQuery(); // NOSONAR
var connection = getConnection();
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
var resultSet = statement.executeQuery(); // NOSONAR
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
Spliterator.ORDERED) {
@@ -121,9 +118,8 @@ public class DbCustomerDao implements CustomerDao {
ResultSet resultSet = null;
try (Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
try (var connection = getConnection();
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
statement.setInt(1, id);
resultSet = statement.executeQuery();
@@ -150,9 +146,8 @@ public class DbCustomerDao implements CustomerDao {
return false;
}
try (Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
try (var connection = getConnection();
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
statement.setInt(1, customer.getId());
statement.setString(2, customer.getFirstName());
statement.setString(3, customer.getLastName());
@@ -168,8 +163,8 @@ public class DbCustomerDao implements CustomerDao {
*/
@Override
public boolean update(Customer customer) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement =
try (var connection = getConnection();
var statement =
connection
.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
statement.setString(1, customer.getFirstName());
@@ -186,9 +181,8 @@ public class DbCustomerDao implements CustomerDao {
*/
@Override
public boolean delete(Customer customer) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
try (var connection = getConnection();
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
statement.setInt(1, customer.getId());
return statement.executeUpdate() > 0;
} catch (SQLException ex) {