Skip to content

Commit 01e489c

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Resolves checkstyle errors for dao data-bus data-locality data-mapper data-transfer-object decorator (iluwatar#1067)
* Reduces checkstyle errors in dao * Reduces checkstyle errors in data-bus * Reduces checkstyle errors in data-locality * Reduces checkstyle errors in data-mapper * Reduces checkstyle errors in data-transfer-object * Reduces checkstyle errors in decorator
1 parent eae09fc commit 01e489c

33 files changed

+176
-208
lines changed

dao/src/main/java/com/iluwatar/dao/App.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626
import java.sql.Connection;
2727
import java.sql.SQLException;
2828
import java.sql.Statement;
29-
import java.util.ArrayList;
3029
import java.util.List;
3130
import java.util.stream.Stream;
32-
3331
import javax.sql.DataSource;
34-
3532
import org.h2.jdbcx.JdbcDataSource;
3633
import org.slf4j.Logger;
3734
import org.slf4j.LoggerFactory;
@@ -44,27 +41,25 @@
4441
* application needs, in terms of domain-specific objects and data types (the public interface of
4542
* the DAO), from how these needs can be satisfied with a specific DBMS.
4643
*
47-
* <p>With the DAO pattern, we can use various method calls to retrieve/add/delete/update data
48-
* without directly interacting with the data source. The below example demonstrates basic CRUD
44+
* <p>With the DAO pattern, we can use various method calls to retrieve/add/delete/update data
45+
* without directly interacting with the data source. The below example demonstrates basic CRUD
4946
* operations: select, add, update, and delete.
50-
*
51-
*
5247
*/
5348
public class App {
5449
private static final String DB_URL = "jdbc:h2:~/dao";
5550
private static Logger log = LoggerFactory.getLogger(App.class);
5651
private static final String ALL_CUSTOMERS = "customerDao.getAllCustomers(): ";
57-
52+
5853
/**
5954
* Program entry point.
60-
*
55+
*
6156
* @param args command line args.
62-
* @throws Exception if any error occurs.
57+
* @throws Exception if any error occurs.
6358
*/
6459
public static void main(final String[] args) throws Exception {
6560
final CustomerDao inMemoryDao = new InMemoryCustomerDao();
6661
performOperationsUsing(inMemoryDao);
67-
62+
6863
final DataSource dataSource = createDataSource();
6964
createSchema(dataSource);
7065
final CustomerDao dbDao = new DbCustomerDao(dataSource);
@@ -74,14 +69,14 @@ public static void main(final String[] args) throws Exception {
7469

7570
private static void deleteSchema(DataSource dataSource) throws SQLException {
7671
try (Connection connection = dataSource.getConnection();
77-
Statement statement = connection.createStatement()) {
72+
Statement statement = connection.createStatement()) {
7873
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
7974
}
8075
}
8176

8277
private static void createSchema(DataSource dataSource) throws SQLException {
8378
try (Connection connection = dataSource.getConnection();
84-
Statement statement = connection.createStatement()) {
79+
Statement statement = connection.createStatement()) {
8580
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
8681
}
8782
}
@@ -121,7 +116,7 @@ private static void addCustomers(CustomerDao customerDao) throws Exception {
121116

122117
/**
123118
* Generate customers.
124-
*
119+
*
125120
* @return list of customers.
126121
*/
127122
public static List<Customer> generateSampleCustomers() {

dao/src/main/java/com/iluwatar/dao/CustomException.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@
2424
package com.iluwatar.dao;
2525

2626
/**
27-
*
28-
* Custom exception
29-
*
27+
* Custom exception.
3028
*/
3129
public class CustomException extends Exception {
3230

3331
private static final long serialVersionUID = 1L;
3432

35-
public CustomException() {}
33+
public CustomException() {
34+
}
3635

3736
public CustomException(String message) {
3837
super(message);
3938
}
40-
39+
4140
public CustomException(String message, Throwable cause) {
4241
super(message, cause);
4342
}

dao/src/main/java/com/iluwatar/dao/Customer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
/**
2727
* A customer POJO that represents the data that will be read from the data source.
28-
*
2928
*/
3029
public class Customer {
3130

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,61 @@
2828

2929
/**
3030
* In an application the Data Access Object (DAO) is a part of Data access layer. It is an object
31-
* that provides an interface to some type of persistence mechanism. By mapping application calls
32-
* to the persistence layer, DAO provides some specific data operations without exposing details
33-
* of the database. This isolation supports the Single responsibility principle. It separates what
34-
* data accesses the application needs, in terms of domain-specific objects and data types
35-
* (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
36-
* database schema, etc.
37-
*
38-
* <p>Any change in the way data is stored and retrieved will not change the client code as the
31+
* that provides an interface to some type of persistence mechanism. By mapping application calls to
32+
* the persistence layer, DAO provides some specific data operations without exposing details of the
33+
* database. This isolation supports the Single responsibility principle. It separates what data
34+
* accesses the application needs, in terms of domain-specific objects and data types (the public
35+
* interface of the DAO), from how these needs can be satisfied with a specific DBMS, database
36+
* schema, etc.
37+
*
38+
* <p>Any change in the way data is stored and retrieved will not change the client code as the
3939
* client will be using interface and need not worry about exact source.
40-
*
40+
*
4141
* @see InMemoryCustomerDao
4242
* @see DbCustomerDao
4343
*/
4444
public interface CustomerDao {
4545

4646
/**
47-
* @return all the customers as a stream. The stream may be lazily or eagerly evaluated based
48-
* on the implementation. The stream must be closed after use.
47+
* Get all customers.
48+
*
49+
* @return all the customers as a stream. The stream may be lazily or eagerly evaluated based on
50+
* the implementation. The stream must be closed after use.
4951
* @throws Exception if any error occurs.
5052
*/
5153
Stream<Customer> getAll() throws Exception;
52-
54+
5355
/**
56+
* Get customer as Optional by id.
57+
*
5458
* @param id unique identifier of the customer.
55-
* @return an optional with customer if a customer with unique identifier <code>id</code>
56-
* exists, empty optional otherwise.
59+
* @return an optional with customer if a customer with unique identifier <code>id</code> exists,
60+
* empty optional otherwise.
5761
* @throws Exception if any error occurs.
5862
*/
5963
Optional<Customer> getById(int id) throws Exception;
6064

6165
/**
66+
* Add a customer.
67+
*
6268
* @param customer the customer to be added.
6369
* @return true if customer is successfully added, false if customer already exists.
6470
* @throws Exception if any error occurs.
6571
*/
6672
boolean add(Customer customer) throws Exception;
6773

6874
/**
75+
* Update a customer.
76+
*
6977
* @param customer the customer to be updated.
7078
* @return true if customer exists and is successfully updated, false otherwise.
7179
* @throws Exception if any error occurs.
7280
*/
7381
boolean update(Customer customer) throws Exception;
7482

7583
/**
84+
* Delete a customer.
85+
*
7686
* @param customer the customer to be deleted.
7787
* @return true if customer exists and is successfully deleted, false otherwise.
7888
* @throws Exception if any error occurs.

dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
package com.iluwatar.dao;
2525

2626
/**
27-
* Customer Schema SQL Class
27+
* Customer Schema SQL Class.
2828
*/
2929
public final class CustomerSchemaSql {
3030

31-
private CustomerSchemaSql() {}
31+
private CustomerSchemaSql() {
32+
}
3233

33-
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
34-
+ "LNAME VARCHAR(100))";
34+
public static final String CREATE_SCHEMA_SQL =
35+
"CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
36+
+ "LNAME VARCHAR(100))";
3537

3638
public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
3739

dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
package com.iluwatar.dao;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2926
import java.sql.Connection;
3027
import java.sql.PreparedStatement;
3128
import java.sql.ResultSet;
@@ -36,12 +33,12 @@
3633
import java.util.function.Consumer;
3734
import java.util.stream.Stream;
3835
import java.util.stream.StreamSupport;
39-
4036
import javax.sql.DataSource;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
4139

4240
/**
4341
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
44-
*
4542
*/
4643
public class DbCustomerDao implements CustomerDao {
4744

@@ -50,29 +47,32 @@ public class DbCustomerDao implements CustomerDao {
5047
private final DataSource dataSource;
5148

5249
/**
53-
* Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code>
54-
* to store and retrieve customer information.
55-
*
50+
* Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code> to
51+
* store and retrieve customer information.
52+
*
5653
* @param dataSource a non-null dataSource.
5754
*/
5855
public DbCustomerDao(DataSource dataSource) {
5956
this.dataSource = dataSource;
6057
}
6158

6259
/**
63-
* @return a lazily populated stream of customers. Note the stream returned must be closed to
64-
* free all the acquired resources. The stream keeps an open connection to the database till
65-
* it is complete or is closed manually.
60+
* Get all customers as Java Stream.
61+
*
62+
* @return a lazily populated stream of customers. Note the stream returned must be closed to free
63+
* all the acquired resources. The stream keeps an open connection to the database till it is
64+
* complete or is closed manually.
6665
*/
6766
@Override
6867
public Stream<Customer> getAll() throws Exception {
6968

7069
Connection connection;
7170
try {
7271
connection = getConnection();
73-
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
72+
PreparedStatement statement =
73+
connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
7474
ResultSet resultSet = statement.executeQuery(); // NOSONAR
75-
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
75+
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
7676
Spliterator.ORDERED) {
7777

7878
@Override
@@ -108,8 +108,8 @@ private void mutedClose(Connection connection, PreparedStatement statement, Resu
108108
}
109109

110110
private Customer createCustomer(ResultSet resultSet) throws SQLException {
111-
return new Customer(resultSet.getInt("ID"),
112-
resultSet.getString("FNAME"),
111+
return new Customer(resultSet.getInt("ID"),
112+
resultSet.getString("FNAME"),
113113
resultSet.getString("LNAME"));
114114
}
115115

@@ -122,8 +122,8 @@ public Optional<Customer> getById(int id) throws Exception {
122122
ResultSet resultSet = null;
123123

124124
try (Connection connection = getConnection();
125-
PreparedStatement statement =
126-
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
125+
PreparedStatement statement =
126+
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
127127

128128
statement.setInt(1, id);
129129
resultSet = statement.executeQuery();
@@ -151,8 +151,8 @@ public boolean add(Customer customer) throws Exception {
151151
}
152152

153153
try (Connection connection = getConnection();
154-
PreparedStatement statement =
155-
connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
154+
PreparedStatement statement =
155+
connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
156156
statement.setInt(1, customer.getId());
157157
statement.setString(2, customer.getFirstName());
158158
statement.setString(3, customer.getLastName());
@@ -169,8 +169,9 @@ public boolean add(Customer customer) throws Exception {
169169
@Override
170170
public boolean update(Customer customer) throws Exception {
171171
try (Connection connection = getConnection();
172-
PreparedStatement statement =
173-
connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
172+
PreparedStatement statement =
173+
connection
174+
.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
174175
statement.setString(1, customer.getFirstName());
175176
statement.setString(2, customer.getLastName());
176177
statement.setInt(3, customer.getId());
@@ -186,8 +187,8 @@ public boolean update(Customer customer) throws Exception {
186187
@Override
187188
public boolean delete(Customer customer) throws Exception {
188189
try (Connection connection = getConnection();
189-
PreparedStatement statement =
190-
connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
190+
PreparedStatement statement =
191+
connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
191192
statement.setInt(1, customer.getId());
192193
return statement.executeUpdate() > 0;
193194
} catch (SQLException ex) {

dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import java.util.stream.Stream;
3030

3131
/**
32-
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory
33-
* and data is lost when the application exits.
32+
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory and
33+
* data is lost when the application exits.
3434
* <br>
3535
* This implementation is useful as temporary database or for testing.
3636
*/
@@ -56,7 +56,7 @@ public boolean add(final Customer customer) {
5656
if (getById(customer.getId()).isPresent()) {
5757
return false;
5858
}
59-
59+
6060
idToCustomer.put(customer.getId(), customer);
6161
return true;
6262
}

0 commit comments

Comments
 (0)