Skip to content

Commit f8380a7

Browse files
committed
fix SonarQube
1 parent 069ef48 commit f8380a7

17 files changed

+216
-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,36 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
/** Customer exception */
28+
public class CustomException extends RuntimeException {
29+
public CustomException(String message) {
30+
super(message);
31+
}
32+
33+
public CustomException(String message, Throwable cause) {
34+
super(message, cause);
35+
}
36+
}

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,61 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
/**
28+
* {@code DAOFactoryProvider} is a utility class responsible for providing concrete implementations
29+
* of the {@link DAOFactory} interface based on the specified data source type.
30+
*
31+
* <p>This class acts as an entry point to obtain DAO factories for different storage mechanisms
32+
* such as relational databases (e.g., H2), document stores (e.g., MongoDB), or file-based systems.
33+
* It uses the {@link DataSourceType} enumeration to determine which concrete factory to
34+
* instantiate.
35+
*
36+
* <p>Example usage:
37+
*
38+
* <pre>{@code
39+
* DAOFactory factory = DAOFactoryProvider.getDataSource(DataSourceType.H2);
40+
* }</pre>
41+
*/
42+
public class DAOFactoryProvider {
43+
44+
private DAOFactoryProvider() {}
45+
46+
/**
47+
* Returns a concrete {@link DAOFactory} intance based on the specified data source type.
48+
*
49+
* @param dataSourceType The type of data source for which a factory is needed. Supported values:
50+
* {@code H2}, {@code Mongo}, {@code FlatFile}
51+
* @return A {@link DAOFactory} implementation corresponding to the given data source type.
52+
* @throws IllegalArgumentException if the given data source type is not supported.
53+
*/
54+
public static DAOFactory getDataSource(DataSourceType dataSourceType) {
55+
return switch (dataSourceType) {
56+
case H2 -> new H2DataSourceFactory();
57+
case MONGO -> new MongoDataSourceFactory();
58+
case FLAT_FILE -> new FlatFileDataSourceFactory();
59+
};
60+
}
61+
}

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)