mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 17:28:48 +00:00
📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@@ -26,9 +26,8 @@ package com.iluwatar.dao;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Data Access Object (DAO) is an object that provides an abstract interface to some type of
|
||||
@@ -42,9 +41,9 @@ import org.slf4j.LoggerFactory;
|
||||
* without directly interacting with the data source. The below example demonstrates basic CRUD
|
||||
* operations: select, add, update, and delete.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
private static final String DB_URL = "jdbc:h2:~/dao";
|
||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||
private static final String ALL_CUSTOMERS = "customerDao.getAllCustomers(): ";
|
||||
|
||||
/**
|
||||
@@ -86,23 +85,23 @@ public class App {
|
||||
|
||||
private static void performOperationsUsing(final CustomerDao customerDao) throws Exception {
|
||||
addCustomers(customerDao);
|
||||
log.info(ALL_CUSTOMERS);
|
||||
LOGGER.info(ALL_CUSTOMERS);
|
||||
try (var customerStream = customerDao.getAll()) {
|
||||
customerStream.forEach((customer) -> log.info(customer.toString()));
|
||||
customerStream.forEach(customer -> LOGGER.info(customer.toString()));
|
||||
}
|
||||
log.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
|
||||
LOGGER.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
|
||||
final var customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.add(customer);
|
||||
log.info(ALL_CUSTOMERS + customerDao.getAll());
|
||||
LOGGER.info(ALL_CUSTOMERS + customerDao.getAll());
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.update(customer);
|
||||
log.info(ALL_CUSTOMERS);
|
||||
LOGGER.info(ALL_CUSTOMERS);
|
||||
try (var customerStream = customerDao.getAll()) {
|
||||
customerStream.forEach((cust) -> log.info(cust.toString()));
|
||||
customerStream.forEach(cust -> LOGGER.info(cust.toString()));
|
||||
}
|
||||
customerDao.delete(customer);
|
||||
log.info(ALL_CUSTOMERS + customerDao.getAll());
|
||||
LOGGER.info(ALL_CUSTOMERS + customerDao.getAll());
|
||||
}
|
||||
|
||||
private static void addCustomers(CustomerDao customerDao) throws Exception {
|
||||
|
||||
@@ -23,70 +23,25 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* A customer POJO that represents the data that will be read from the data source.
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
@AllArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
@EqualsAndHashCode.Include
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
/**
|
||||
* Creates an instance of customer.
|
||||
*/
|
||||
public Customer(int id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
|
||||
+ getLastName() + '\'' + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object that) {
|
||||
var isEqual = false;
|
||||
if (this == that) {
|
||||
isEqual = true;
|
||||
} else if (that != null && getClass() == that.getClass()) {
|
||||
final var customer = (Customer) that;
|
||||
if (getId() == customer.getId()) {
|
||||
isEqual = true;
|
||||
}
|
||||
}
|
||||
return isEqual;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,28 +34,18 @@ import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
import javax.sql.DataSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class DbCustomerDao implements CustomerDao {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DbCustomerDao.class);
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
/**
|
||||
* Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code> to
|
||||
* store and retrieve customer information.
|
||||
*
|
||||
* @param dataSource a non-null dataSource.
|
||||
*/
|
||||
public DbCustomerDao(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all customers as Java Stream.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user