Skip to content

Commit d6fc28e

Browse files
committed
Changing code to use interfaces instead of implementations.
1 parent 6292690 commit d6fc28e

File tree

24 files changed

+346
-13
lines changed

24 files changed

+346
-13
lines changed

caching/src/main/java/com/iluwatar/caching/LruCache.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void clear() {
167167
* Returns cache data in list form.
168168
*/
169169
public List<UserAccount> getCacheDataInListForm() {
170-
ArrayList<UserAccount> listOfCacheData = new ArrayList<>();
170+
List<UserAccount> listOfCacheData = new ArrayList<>();
171171
Node temp = head;
172172
while (temp != null) {
173173
listOfCacheData.add(temp.userAccount);

factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.iluwatar.factorykit;
2424

2525
import java.util.HashMap;
26+
import java.util.Map;
2627
import java.util.function.Consumer;
2728
import java.util.function.Supplier;
2829

@@ -48,7 +49,7 @@ public interface WeaponFactory {
4849
* @return factory with specified {@link Builder}s
4950
*/
5051
static WeaponFactory factory(Consumer<Builder> consumer) {
51-
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
52+
Map<WeaponType, Supplier<Weapon>> map = new HashMap<>();
5253
consumer.accept(map::put);
5354
return name -> map.get(name).get();
5455
}

hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.bson.Document;
3030

3131
import java.util.ArrayList;
32+
import java.util.List;
3233

3334
/**
3435
* Mongo based banking adapter
@@ -110,7 +111,7 @@ public void setFunds(String bankAccount, int amount) {
110111
@Override
111112
public int getFunds(String bankAccount) {
112113
Document search = new Document("_id", bankAccount);
113-
ArrayList<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>());
114+
List<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>());
114115
if (results.size() > 0) {
115116
return results.get(0).getInteger("funds");
116117
} else {

hexagonal/src/main/java/com/iluwatar/hexagonal/database/MongoTicketRepository.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import java.util.Arrays;
3636
import java.util.HashMap;
3737
import java.util.HashSet;
38+
import java.util.List;
3839
import java.util.Map;
3940
import java.util.Optional;
41+
import java.util.Set;
4042

4143
/**
4244
* Mongo lottery ticket database
@@ -142,7 +144,7 @@ public MongoCollection<Document> getCountersCollection() {
142144
@Override
143145
public Optional<LotteryTicket> findById(LotteryTicketId id) {
144146
Document find = new Document("ticketId", id.getId());
145-
ArrayList<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
147+
List<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
146148
if (results.size() > 0) {
147149
LotteryTicket lotteryTicket = docToTicket(results.get(0));
148150
return Optional.of(lotteryTicket);
@@ -166,7 +168,7 @@ public Optional<LotteryTicketId> save(LotteryTicket ticket) {
166168
@Override
167169
public Map<LotteryTicketId, LotteryTicket> findAll() {
168170
Map<LotteryTicketId, LotteryTicket> map = new HashMap<>();
169-
ArrayList<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
171+
List<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
170172
for (Document doc: docs) {
171173
LotteryTicket lotteryTicket = docToTicket(doc);
172174
map.put(lotteryTicket.getId(), lotteryTicket);
@@ -183,7 +185,7 @@ private LotteryTicket docToTicket(Document doc) {
183185
PlayerDetails playerDetails = new PlayerDetails(doc.getString("email"), doc.getString("bank"),
184186
doc.getString("phone"));
185187
int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray();
186-
HashSet<Integer> numbers = new HashSet<>();
188+
Set<Integer> numbers = new HashSet<>();
187189
for (int num: numArray) {
188190
numbers.add(num);
189191
}

layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public List<CakeInfo> getAllCakes() {
163163
CakeToppingInfo cakeToppingInfo =
164164
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake
165165
.getTopping().getCalories());
166-
ArrayList<CakeLayerInfo> cakeLayerInfos = new ArrayList<>();
166+
List<CakeLayerInfo> cakeLayerInfos = new ArrayList<>();
167167
for (CakeLayer layer : cake.getLayers()) {
168168
cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories()));
169169
}

memory-dao-test/.springBeans

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansProjectDescription>
3+
<version>1</version>
4+
<pluginVersion><![CDATA[3.8.2.201610040608-RELEASE]]></pluginVersion>
5+
<configSuffixes>
6+
<configSuffix><![CDATA[xml]]></configSuffix>
7+
</configSuffixes>
8+
<enableImports><![CDATA[false]]></enableImports>
9+
<configs>
10+
<config>src/main/resources/beans.xml</config>
11+
</configs>
12+
<autoconfigs>
13+
</autoconfigs>
14+
<configSets>
15+
</configSets>
16+
</beansProjectDescription>

memory-dao-test/pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.memory.dao</groupId>
5+
<artifactId>memory-dao-test</artifactId>
6+
<version>0.0.1</version>
7+
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.7</maven.compiler.source>
11+
<maven.compiler.target>1.7</maven.compiler.target>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.12</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.apache.commons</groupId>
23+
<artifactId>commons-lang3</artifactId>
24+
<version>3.0</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>mysql</groupId>
29+
<artifactId>mysql-connector-java</artifactId>
30+
<version>5.1.25</version>
31+
</dependency>
32+
33+
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
34+
<dependency>
35+
<groupId>com.h2database</groupId>
36+
<artifactId>h2</artifactId>
37+
<version>1.4.193</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-core</artifactId>
43+
<version>4.2.5.RELEASE</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-beans</artifactId>
48+
<version>4.2.5.RELEASE</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-context</artifactId>
53+
<version>4.2.5.RELEASE</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.springframework</groupId>
58+
<artifactId>spring-jdbc</artifactId>
59+
<version>4.2.5.RELEASE</version>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-test</artifactId>
65+
<version>4.2.5.RELEASE</version>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.memory.dao;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
import com.memory.dao.db.UserDAO;
7+
import com.memory.dao.pojo.User;
8+
9+
public class App {
10+
public static void main(String[] args) {
11+
12+
final ApplicationContext context = new ClassPathXmlApplicationContext(
13+
"file:src/main/resources/beans.xml");
14+
15+
final UserDAO dao = (UserDAO)context.getBean("userDao");
16+
for (final User user : dao.findAll()) {
17+
System.out.println(user);
18+
}
19+
20+
((ClassPathXmlApplicationContext)context).close();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.memory.dao;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = {"com.memory.dao"})
8+
public class AppConfig {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.memory.dao.db;
2+
3+
public enum Queries {
4+
5+
GET_USER("SELECT * FROM users WHERE name = :name"),
6+
GET_ALL_USERS("SELECT * FROM users")
7+
;
8+
9+
private final String query;
10+
11+
Queries(final String query) {
12+
this.query = query;
13+
}
14+
15+
public String get() {
16+
return this.query;
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.memory.dao.db;
2+
3+
import com.memory.dao.pojo.User;
4+
import java.util.List;
5+
6+
public interface UserDAO {
7+
8+
User findByName(String name);
9+
List<User> findAll();
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.memory.dao.db;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.HashMap;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.jdbc.core.RowMapper;
11+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
12+
import org.springframework.stereotype.Repository;
13+
14+
import com.memory.dao.pojo.User;
15+
16+
@Repository
17+
public class UserDAOImpl implements UserDAO {
18+
19+
private NamedParameterJdbcTemplate namedParameterJDBCTemplate;
20+
21+
@Autowired
22+
public void setNamedParameterJDBCTemplate(final NamedParameterJdbcTemplate namedParameterJDBCTemplate) {
23+
this.namedParameterJDBCTemplate = namedParameterJDBCTemplate;
24+
}
25+
26+
@Override
27+
public User findByName(final String name) {
28+
final Map<String, Object> params = new HashMap<>();
29+
params.put("name", name);
30+
31+
final User user = namedParameterJDBCTemplate.
32+
queryForObject(Queries.GET_USER.get(), params, new UserMapper());
33+
34+
System.out.println("Found: " + user);
35+
36+
return user;
37+
}
38+
39+
@Override
40+
public List<User> findAll() {
41+
42+
Map<String, Object> params = new HashMap<String, Object>();
43+
44+
final List<User> result = namedParameterJDBCTemplate.query(Queries.GET_ALL_USERS.get(), params, new UserMapper());
45+
46+
return result;
47+
48+
}
49+
50+
private static final class UserMapper implements RowMapper<User> {
51+
@Override
52+
public User mapRow(final ResultSet rs, final int rowNum) throws SQLException {
53+
final User user = new User();
54+
user.setId(rs.getInt("id"));
55+
user.setName(rs.getString("name"));
56+
user.setEmail(rs.getString("email"));
57+
return user;
58+
}
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.memory.dao.pojo;
2+
3+
public class User {
4+
5+
private int id;
6+
private String name;
7+
private String email;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public void setId(final int id) {
14+
this.id = id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(final String name) {
22+
this.name = name;
23+
}
24+
25+
public String getEmail() {
26+
return email;
27+
}
28+
29+
public void setEmail(final String email) {
30+
this.email = email;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<import resource="db-h2-config.xml" />
9+
10+
<bean id="userDao" class="com.memory.dao.db.UserDAOImpl">
11+
<property name="namedParameterJDBCTemplate" ref="jdbcTemplate" />
12+
</bean>
13+
14+
<bean id="jdbcTemplate"
15+
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <!-- <property name="dataSource" ref="dataSource" /> -->
16+
<!-- <constructor-arg ref="dbcpDataSource" /> -->
17+
<constructor-arg ref="dataSource" />
18+
</bean>
19+
20+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
8+
http://www.springframework.org/schema/jdbc
9+
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
10+
http://www.springframework.org/schema/context
11+
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
12+
13+
<jdbc:embedded-database id="dataSource" type="H2">
14+
<jdbc:script location="classpath:db/sql/create-db.sql" />
15+
<jdbc:script location="classpath:db/sql/insert-data.sql" />
16+
</jdbc:embedded-database>
17+
18+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--DROP TABLE users IF EXISTS;
2+
3+
CREATE TABLE users (
4+
id INTEGER PRIMARY KEY,
5+
name VARCHAR(30),
6+
email VARCHAR(50)
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO users VALUES (1, 'mkyong', '[email protected]');
2+
INSERT INTO users VALUES (2, 'alex', '[email protected]');
3+
INSERT INTO users VALUES (3, 'joel', '[email protected]');

0 commit comments

Comments
 (0)