Skip to content

Commit 9ae56c8

Browse files
committed
fix SonarQube
1 parent 069ef48 commit 9ae56c8

17 files changed

+168
-124
lines changed

dao-factory/src/main/java/com/iluwatar/daofactory/App.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.iluwatar.daofactory;
2626

27+
import java.io.Serializable;
2728
import java.util.List;
2829
import lombok.extern.slf4j.Slf4j;
2930
import org.bson.types.ObjectId;
@@ -32,8 +33,8 @@
3233
public class App {
3334

3435
public static void main(String[] args) {
35-
var daoFactory = DAOFactory.getDataSource(DataSourceType.H2);
36-
var customerDAO = daoFactory.createCustomerDAO();
36+
var daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.H2);
37+
CustomerDAO customerDAO = daoFactory.createCustomerDAO();
3738

3839
// Perform CRUD H2 Database
3940
if (customerDAO instanceof H2CustomerDAO h2CustomerDAO) {
@@ -55,7 +56,7 @@ public static void main(String[] args) {
5556
deleteSchema(customerDAO);
5657

5758
// Perform CRUD MongoDb
58-
daoFactory = DAOFactory.getDataSource(DataSourceType.Mongo);
59+
daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.MONGO);
5960
customerDAO = daoFactory.createCustomerDAO();
6061
ObjectId idCustomerMongo1 = new ObjectId();
6162
ObjectId idCustomerMongo2 = new ObjectId();
@@ -72,7 +73,7 @@ public static void main(String[] args) {
7273
deleteSchema(customerDAO);
7374

7475
// Perform CRUD Flat file
75-
daoFactory = DAOFactory.getDataSource(DataSourceType.FlatFile);
76+
daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.FLAT_FILE);
7677
customerDAO = daoFactory.createCustomerDAO();
7778
Customer<Long> customerFlatFile1 = new Customer<>(1L, "Duc");
7879
Customer<Long> customerFlatFile2 = new Customer<>(2L, "Quang");
@@ -88,11 +89,11 @@ public static void main(String[] args) {
8889
deleteSchema(customerDAO);
8990
}
9091

91-
public static void deleteSchema(CustomerDAO<?> customerDAO) {
92+
public static void deleteSchema(CustomerDAO customerDAO) {
9293
customerDAO.deleteSchema();
9394
}
9495

95-
public static <T> void performCreateCustomer(
96+
public static <T extends Serializable> void performCreateCustomer(
9697
CustomerDAO<T> customerDAO, List<Customer<T>> customerList) {
9798
for (Customer<T> customer : customerList) {
9899
customerDAO.save(customer);
@@ -103,7 +104,7 @@ public static <T> void performCreateCustomer(
103104
}
104105
}
105106

106-
public static <T> void performUpdateCustomer(
107+
public static <T extends Serializable> void performUpdateCustomer(
107108
CustomerDAO<T> customerDAO, Customer<T> customerUpdate) {
108109
customerDAO.update(customerUpdate);
109110
List<Customer<T>> customers = customerDAO.findAll();
@@ -112,7 +113,8 @@ public static <T> void performUpdateCustomer(
112113
}
113114
}
114115

115-
public static <T> void performDeleteCustomer(CustomerDAO<T> customerDAO, T customerId) {
116+
public static <T extends Serializable> void performDeleteCustomer(
117+
CustomerDAO<T> customerDAO, T customerId) {
116118
customerDAO.delete(customerId);
117119
List<Customer<T>> customers = customerDAO.findAll();
118120
for (Customer<T> customer : customers) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar.daofactory;
2+
3+
/** Customer exception */
4+
public class CustomException extends RuntimeException {
5+
public CustomException(String message) {
6+
super(message);
7+
}
8+
9+
public CustomException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}

dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@NoArgsConstructor
4242
@AllArgsConstructor
4343
@ToString
44-
public class Customer<T> implements Serializable {
44+
public class Customer<T extends Serializable> implements Serializable {
4545
private T id;
4646
private String name;
4747
}

dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.iluwatar.daofactory;
2626

27+
import java.io.Serializable;
2728
import java.util.List;
2829
import java.util.Optional;
2930

@@ -39,7 +40,7 @@
3940
* @see MongoCustomerDAO
4041
* @see FlatFileCustomerDAO
4142
*/
42-
public interface CustomerDAO<T> {
43+
public interface CustomerDAO<T extends Serializable> {
4344
/**
4445
* Persist the given customer
4546
*

dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java

-16
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@
3636
* @see FlatFileDataSourceFactory
3737
*/
3838
public abstract class DAOFactory {
39-
/**
40-
* Returns a concrete {@link DAOFactory} intance based on the specified data source type.
41-
*
42-
* @param dataSourceType The type of data source for which a factory is needed. Supported values:
43-
* {@code H2}, {@code Mongo}, {@code FlatFile}
44-
* @return A {@link DAOFactory} implementation corresponding to the given data source type.
45-
* @throws IllegalArgumentException if the given data source type is not supported.
46-
*/
47-
public static DAOFactory getDataSource(DataSourceType dataSourceType) {
48-
return switch (dataSourceType) {
49-
case H2 -> new H2DataSourceFactory();
50-
case Mongo -> new MongoDataSourceFactory();
51-
case FlatFile -> new FlatFileDataSourceFactory();
52-
};
53-
}
54-
5539
/**
5640
* Retrieves a {@link CustomerDAO} implementation specific to the underlying data source..
5741
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.iluwatar.daofactory;
2+
3+
/**
4+
* {@code DAOFactoryProvider} is a utility class responsible for providing concrete implementations
5+
* of the {@link DAOFactory} interface based on the specified data source type.
6+
*
7+
* <p>This class acts as an entry point to obtain DAO factories for different storage mechanisms
8+
* such as relational databases (e.g., H2), document stores (e.g., MongoDB), or file-based systems.
9+
* It uses the {@link DataSourceType} enumeration to determine which concrete factory to
10+
* instantiate.
11+
*
12+
* <p>Example usage:
13+
*
14+
* <pre>{@code
15+
* DAOFactory factory = DAOFactoryProvider.getDataSource(DataSourceType.H2);
16+
* }</pre>
17+
*/
18+
public class DAOFactoryProvider {
19+
20+
private DAOFactoryProvider() {}
21+
22+
/**
23+
* Returns a concrete {@link DAOFactory} intance based on the specified data source type.
24+
*
25+
* @param dataSourceType The type of data source for which a factory is needed. Supported values:
26+
* {@code H2}, {@code Mongo}, {@code FlatFile}
27+
* @return A {@link DAOFactory} implementation corresponding to the given data source type.
28+
* @throws IllegalArgumentException if the given data source type is not supported.
29+
*/
30+
public static DAOFactory getDataSource(DataSourceType dataSourceType) {
31+
return switch (dataSourceType) {
32+
case H2 -> new H2DataSourceFactory();
33+
case MONGO -> new MongoDataSourceFactory();
34+
case FLAT_FILE -> new FlatFileDataSourceFactory();
35+
};
36+
}
37+
}

dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
/** Enumerates the types of data sources supported by the application. */
2828
public enum DataSourceType {
2929
H2,
30-
Mongo,
31-
FlatFile
30+
MONGO,
31+
FLAT_FILE
3232
}

dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -67,80 +67,80 @@ public void save(Customer<Long> customer) {
6767
try (Reader reader = createReader(filePath)) {
6868
customers = gson.fromJson(reader, customerListType);
6969
} catch (IOException ex) {
70-
throw new RuntimeException("Failed to read customer data", ex);
70+
throw new CustomException("Failed to read customer data", ex);
7171
}
7272
}
7373
customers.add(customer);
7474
try (Writer writer = createWriter(filePath)) {
7575
gson.toJson(customers, writer);
7676
} catch (IOException ex) {
77-
throw new RuntimeException("Failed to write customer data", ex);
77+
throw new CustomException("Failed to write customer data", ex);
7878
}
7979
}
8080

8181
/** {@inheritDoc} */
8282
@Override
8383
public void update(Customer<Long> customer) {
8484
if (!filePath.toFile().exists()) {
85-
throw new RuntimeException("File not found");
85+
throw new CustomException("File not found");
8686
}
8787
List<Customer<Long>> customers;
8888
try (Reader reader = createReader(filePath)) {
8989
customers = gson.fromJson(reader, customerListType);
9090
} catch (IOException ex) {
91-
throw new RuntimeException("Failed to read customer data", ex);
91+
throw new CustomException("Failed to read customer data", ex);
9292
}
9393
customers.stream()
9494
.filter(c -> c.getId().equals(customer.getId()))
9595
.findFirst()
9696
.ifPresentOrElse(
9797
c -> c.setName(customer.getName()),
9898
() -> {
99-
throw new RuntimeException("Customer not found with id: " + customer.getId());
99+
throw new CustomException("Customer not found with id: " + customer.getId());
100100
});
101101
try (Writer writer = createWriter(filePath)) {
102102
gson.toJson(customers, writer);
103103
} catch (IOException ex) {
104-
throw new RuntimeException("Failed to write customer data", ex);
104+
throw new CustomException("Failed to write customer data", ex);
105105
}
106106
}
107107

108108
/** {@inheritDoc} */
109109
@Override
110110
public void delete(Long id) {
111111
if (!filePath.toFile().exists()) {
112-
throw new RuntimeException("File not found");
112+
throw new CustomException("File not found");
113113
}
114114
List<Customer<Long>> customers;
115115
try (Reader reader = createReader(filePath)) {
116116
customers = gson.fromJson(reader, customerListType);
117117
} catch (IOException ex) {
118-
throw new RuntimeException("Failed to read customer data", ex);
118+
throw new CustomException("Failed to read customer data", ex);
119119
}
120120
Customer<Long> customerToRemove =
121121
customers.stream()
122122
.filter(c -> c.getId().equals(id))
123123
.findFirst()
124-
.orElseThrow(() -> new RuntimeException("Customer not found with id: " + id));
124+
.orElseThrow(() -> new CustomException("Customer not found with id: " + id));
125125
customers.remove(customerToRemove);
126126
try (Writer writer = createWriter(filePath)) {
127127
gson.toJson(customers, writer);
128128
} catch (IOException ex) {
129-
throw new RuntimeException("Failed to write customer data", ex);
129+
throw new CustomException("Failed to write customer data", ex);
130130
}
131131
}
132132

133133
/** {@inheritDoc} */
134134
@Override
135135
public List<Customer<Long>> findAll() {
136136
if (!filePath.toFile().exists()) {
137-
throw new RuntimeException("File not found");
137+
throw new CustomException("File not found");
138138
}
139139
List<Customer<Long>> customers;
140140
try (Reader reader = createReader(filePath)) {
141141
customers = gson.fromJson(reader, customerListType);
142142
} catch (IOException ex) {
143-
throw new RuntimeException("Failed to read customer data", ex);
143+
throw new CustomException("Failed to read customer data", ex);
144144
}
145145
return customers;
146146
}
@@ -149,13 +149,13 @@ public List<Customer<Long>> findAll() {
149149
@Override
150150
public Optional<Customer<Long>> findById(Long id) {
151151
if (!filePath.toFile().exists()) {
152-
throw new RuntimeException("File not found");
152+
throw new CustomException("File not found");
153153
}
154154
List<Customer<Long>> customers = null;
155155
try (Reader reader = createReader(filePath)) {
156156
customers = gson.fromJson(reader, customerListType);
157157
} catch (IOException ex) {
158-
throw new RuntimeException("Failed to read customer data", ex);
158+
throw new CustomException("Failed to read customer data", ex);
159159
}
160160
return customers.stream().filter(c -> c.getId().equals(id)).findFirst();
161161
}
@@ -164,12 +164,12 @@ public Optional<Customer<Long>> findById(Long id) {
164164
@Override
165165
public void deleteSchema() {
166166
if (!filePath.toFile().exists()) {
167-
throw new RuntimeException("File not found");
167+
throw new CustomException("File not found");
168168
}
169169
try {
170170
Files.delete(filePath);
171171
} catch (IOException ex) {
172-
throw new RuntimeException("Failed to delete customer data");
172+
throw new CustomException("Failed to delete customer data");
173173
}
174174
}
175175
}

dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
/** FlatFileDataSourceFactory concrete factory. */
3333
public class FlatFileDataSourceFactory extends DAOFactory {
34-
private final String FILE_PATH = System.getProperty("user.home") + "/Desktop/customer.json";
34+
private static final String FILE_PATH =
35+
System.getProperty("user.home") + "/Desktop/customer.json";
3536

3637
@Override
37-
public CustomerDAO createCustomerDAO() {
38+
public CustomerDAO<Long> createCustomerDAO() {
3839
Path filePath = Paths.get(FILE_PATH);
3940
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
4041
return new FlatFileCustomerDAO(filePath, gson);

0 commit comments

Comments
 (0)