Changing code to use interfaces instead of implementations.
This commit is contained in:
parent
6292690250
commit
d6fc28e120
@ -167,7 +167,7 @@ public class LruCache {
|
||||
* Returns cache data in list form.
|
||||
*/
|
||||
public List<UserAccount> getCacheDataInListForm() {
|
||||
ArrayList<UserAccount> listOfCacheData = new ArrayList<>();
|
||||
List<UserAccount> listOfCacheData = new ArrayList<>();
|
||||
Node temp = head;
|
||||
while (temp != null) {
|
||||
listOfCacheData.add(temp.userAccount);
|
||||
|
@ -23,6 +23,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -48,7 +49,7 @@ public interface WeaponFactory {
|
||||
* @return factory with specified {@link Builder}s
|
||||
*/
|
||||
static WeaponFactory factory(Consumer<Builder> consumer) {
|
||||
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
|
||||
Map<WeaponType, Supplier<Weapon>> map = new HashMap<>();
|
||||
consumer.accept(map::put);
|
||||
return name -> map.get(name).get();
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import com.mongodb.client.model.UpdateOptions;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mongo based banking adapter
|
||||
@ -110,7 +111,7 @@ public class MongoBank implements WireTransfers {
|
||||
@Override
|
||||
public int getFunds(String bankAccount) {
|
||||
Document search = new Document("_id", bankAccount);
|
||||
ArrayList<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>());
|
||||
List<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>());
|
||||
if (results.size() > 0) {
|
||||
return results.get(0).getInteger("funds");
|
||||
} else {
|
||||
|
@ -35,8 +35,10 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Mongo lottery ticket database
|
||||
@ -142,7 +144,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
@Override
|
||||
public Optional<LotteryTicket> findById(LotteryTicketId id) {
|
||||
Document find = new Document("ticketId", id.getId());
|
||||
ArrayList<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
|
||||
List<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
|
||||
if (results.size() > 0) {
|
||||
LotteryTicket lotteryTicket = docToTicket(results.get(0));
|
||||
return Optional.of(lotteryTicket);
|
||||
@ -166,7 +168,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
@Override
|
||||
public Map<LotteryTicketId, LotteryTicket> findAll() {
|
||||
Map<LotteryTicketId, LotteryTicket> map = new HashMap<>();
|
||||
ArrayList<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
|
||||
List<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
|
||||
for (Document doc: docs) {
|
||||
LotteryTicket lotteryTicket = docToTicket(doc);
|
||||
map.put(lotteryTicket.getId(), lotteryTicket);
|
||||
@ -183,7 +185,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
PlayerDetails playerDetails = new PlayerDetails(doc.getString("email"), doc.getString("bank"),
|
||||
doc.getString("phone"));
|
||||
int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray();
|
||||
HashSet<Integer> numbers = new HashSet<>();
|
||||
Set<Integer> numbers = new HashSet<>();
|
||||
for (int num: numArray) {
|
||||
numbers.add(num);
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
CakeToppingInfo cakeToppingInfo =
|
||||
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake
|
||||
.getTopping().getCalories());
|
||||
ArrayList<CakeLayerInfo> cakeLayerInfos = new ArrayList<>();
|
||||
List<CakeLayerInfo> cakeLayerInfos = new ArrayList<>();
|
||||
for (CakeLayer layer : cake.getLayers()) {
|
||||
cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories()));
|
||||
}
|
||||
|
16
memory-dao-test/.springBeans
Normal file
16
memory-dao-test/.springBeans
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beansProjectDescription>
|
||||
<version>1</version>
|
||||
<pluginVersion><![CDATA[3.8.2.201610040608-RELEASE]]></pluginVersion>
|
||||
<configSuffixes>
|
||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
||||
</configSuffixes>
|
||||
<enableImports><![CDATA[false]]></enableImports>
|
||||
<configs>
|
||||
<config>src/main/resources/beans.xml</config>
|
||||
</configs>
|
||||
<autoconfigs>
|
||||
</autoconfigs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
</beansProjectDescription>
|
71
memory-dao-test/pom.xml
Normal file
71
memory-dao-test/pom.xml
Normal file
@ -0,0 +1,71 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.memory.dao</groupId>
|
||||
<artifactId>memory-dao-test</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.25</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.193</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
22
memory-dao-test/src/main/java/com/memory/dao/App.java
Normal file
22
memory-dao-test/src/main/java/com/memory/dao/App.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.memory.dao.db.UserDAO;
|
||||
import com.memory.dao.pojo.User;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
|
||||
final ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"file:src/main/resources/beans.xml");
|
||||
|
||||
final UserDAO dao = (UserDAO)context.getBean("userDao");
|
||||
for (final User user : dao.findAll()) {
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
((ClassPathXmlApplicationContext)context).close();
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.memory.dao"})
|
||||
public class AppConfig {
|
||||
}
|
19
memory-dao-test/src/main/java/com/memory/dao/db/Queries.java
Normal file
19
memory-dao-test/src/main/java/com/memory/dao/db/Queries.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
public enum Queries {
|
||||
|
||||
GET_USER("SELECT * FROM users WHERE name = :name"),
|
||||
GET_ALL_USERS("SELECT * FROM users")
|
||||
;
|
||||
|
||||
private final String query;
|
||||
|
||||
Queries(final String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return this.query;
|
||||
}
|
||||
|
||||
}
|
11
memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java
Normal file
11
memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
import com.memory.dao.pojo.User;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserDAO {
|
||||
|
||||
User findByName(String name);
|
||||
List<User> findAll();
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.memory.dao.pojo.User;
|
||||
|
||||
@Repository
|
||||
public class UserDAOImpl implements UserDAO {
|
||||
|
||||
private NamedParameterJdbcTemplate namedParameterJDBCTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setNamedParameterJDBCTemplate(final NamedParameterJdbcTemplate namedParameterJDBCTemplate) {
|
||||
this.namedParameterJDBCTemplate = namedParameterJDBCTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByName(final String name) {
|
||||
final Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", name);
|
||||
|
||||
final User user = namedParameterJDBCTemplate.
|
||||
queryForObject(Queries.GET_USER.get(), params, new UserMapper());
|
||||
|
||||
System.out.println("Found: " + user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
final List<User> result = namedParameterJDBCTemplate.query(Queries.GET_ALL_USERS.get(), params, new UserMapper());
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private static final class UserMapper implements RowMapper<User> {
|
||||
@Override
|
||||
public User mapRow(final ResultSet rs, final int rowNum) throws SQLException {
|
||||
final User user = new User();
|
||||
user.setId(rs.getInt("id"));
|
||||
user.setName(rs.getString("name"));
|
||||
user.setEmail(rs.getString("email"));
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
38
memory-dao-test/src/main/java/com/memory/dao/pojo/User.java
Normal file
38
memory-dao-test/src/main/java/com/memory/dao/pojo/User.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.memory.dao.pojo;
|
||||
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(final String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
20
memory-dao-test/src/main/resources/beans.xml
Normal file
20
memory-dao-test/src/main/resources/beans.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<import resource="db-h2-config.xml" />
|
||||
|
||||
<bean id="userDao" class="com.memory.dao.db.UserDAOImpl">
|
||||
<property name="namedParameterJDBCTemplate" ref="jdbcTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="jdbcTemplate"
|
||||
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <!-- <property name="dataSource" ref="dataSource" /> -->
|
||||
<!-- <constructor-arg ref="dbcpDataSource" /> -->
|
||||
<constructor-arg ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
18
memory-dao-test/src/main/resources/db-h2-config.xml
Executable file
18
memory-dao-test/src/main/resources/db-h2-config.xml
Executable file
@ -0,0 +1,18 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||
http://www.springframework.org/schema/jdbc
|
||||
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="H2">
|
||||
<jdbc:script location="classpath:db/sql/create-db.sql" />
|
||||
<jdbc:script location="classpath:db/sql/insert-data.sql" />
|
||||
</jdbc:embedded-database>
|
||||
|
||||
</beans>
|
7
memory-dao-test/src/main/resources/db/sql/create-db.sql
Executable file
7
memory-dao-test/src/main/resources/db/sql/create-db.sql
Executable file
@ -0,0 +1,7 @@
|
||||
--DROP TABLE users IF EXISTS;
|
||||
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name VARCHAR(30),
|
||||
email VARCHAR(50)
|
||||
);
|
3
memory-dao-test/src/main/resources/db/sql/insert-data.sql
Executable file
3
memory-dao-test/src/main/resources/db/sql/insert-data.sql
Executable file
@ -0,0 +1,3 @@
|
||||
INSERT INTO users VALUES (1, 'mkyong', 'mkyong@gmail.com');
|
||||
INSERT INTO users VALUES (2, 'alex', 'alex@yahoo.com');
|
||||
INSERT INTO users VALUES (3, 'joel', 'joel@gmail.com');
|
30
memory-dao-test/src/test/java/com/memory/dao/AppTest.java
Normal file
30
memory-dao-test/src/test/java/com/memory/dao/AppTest.java
Normal file
@ -0,0 +1,30 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import com.memory.dao.db.UserDAO;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("/beans.xml")
|
||||
public class AppTest {
|
||||
|
||||
@Autowired
|
||||
private UserDAO dao;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
System.out.println(String.format("Dao is: %s", dao));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApp() {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
0
module/error.txt
Normal file
0
module/error.txt
Normal file
0
module/output.txt
Normal file
0
module/output.txt
Normal file
@ -23,6 +23,7 @@
|
||||
package com.iluwatar.object.pool;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -30,8 +31,8 @@ import java.util.HashSet;
|
||||
*/
|
||||
public abstract class ObjectPool<T> {
|
||||
|
||||
private HashSet<T> available = new HashSet<>();
|
||||
private HashSet<T> inUse = new HashSet<>();
|
||||
private Set<T> available = new HashSet<>();
|
||||
private Set<T> inUse = new HashSet<>();
|
||||
|
||||
protected abstract T create();
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
*/
|
||||
package com.iluwatar.semaphore;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -29,7 +30,7 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public class FruitBowl {
|
||||
|
||||
private ArrayList<Fruit> fruit = new ArrayList<>();
|
||||
private List<Fruit> fruit = new ArrayList<>();
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -26,6 +26,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
@ -58,7 +59,7 @@ public class App {
|
||||
King k = new King();
|
||||
Queen q = new Queen();
|
||||
|
||||
ArrayList<Royalty> guests = new ArrayList<>();
|
||||
List<Royalty> guests = new ArrayList<>();
|
||||
guests.add(k);
|
||||
guests.add(q);
|
||||
|
||||
|
@ -25,6 +25,7 @@ package com.iluwatar.servant;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -74,12 +75,12 @@ public class ServantTest {
|
||||
final Royalty badMoodRoyalty = mock(Royalty.class);
|
||||
when(badMoodRoyalty.getMood()).thenReturn(true);
|
||||
|
||||
final ArrayList<Royalty> goodCompany = new ArrayList<>();
|
||||
final List<Royalty> goodCompany = new ArrayList<>();
|
||||
goodCompany.add(goodMoodRoyalty);
|
||||
goodCompany.add(goodMoodRoyalty);
|
||||
goodCompany.add(goodMoodRoyalty);
|
||||
|
||||
final ArrayList<Royalty> badCompany = new ArrayList<>();
|
||||
final List<Royalty> badCompany = new ArrayList<>();
|
||||
goodCompany.add(goodMoodRoyalty);
|
||||
goodCompany.add(goodMoodRoyalty);
|
||||
goodCompany.add(badMoodRoyalty);
|
||||
|
Loading…
x
Reference in New Issue
Block a user