From 20b1c2bd49f07dc398ecab605bd930f6cfe8d117 Mon Sep 17 00:00:00 2001 From: leogtzr Date: Sat, 24 Dec 2016 00:49:46 -0700 Subject: [PATCH 1/5] Adding initialization on demand holder idiom. --- .../java/com/iluwatar/singleton/IvoryTower.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java b/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java index ed7d44705..ec23ba71b 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java @@ -27,22 +27,24 @@ package com.iluwatar.singleton; */ public final class IvoryTower { - /** - * Static to class instance of the class. - */ - private static final IvoryTower INSTANCE = new IvoryTower(); - /** * Private constructor so nobody can instantiate the class. */ private IvoryTower() {} + private static class IvoryTowerHolder { + /** + * Static to class instance of the class. + */ + private static final IvoryTower INSTANCE = new IvoryTower(); + } + /** * To be called by user to obtain instance of the class. * * @return instance of the singleton. */ public static IvoryTower getInstance() { - return INSTANCE; + return IvoryTowerHolder.INSTANCE; } } From 62926902505b77058e821f604074249241993bc3 Mon Sep 17 00:00:00 2001 From: leogtzr Date: Sat, 24 Dec 2016 14:49:41 -0700 Subject: [PATCH 2/5] Changing constant interface pattern with a Noninstantiable class --- .../main/java/com/iluwatar/dao/CustomerSchemaSql.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java b/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java index 6b8acffd2..e81e415fe 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java @@ -22,10 +22,13 @@ */ package com.iluwatar.dao; -public interface CustomerSchemaSql { +public final class CustomerSchemaSql { - String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), " + private CustomerSchemaSql() {} + + public static final String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), " + "LNAME VARCHAR(100))"; - String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS"; + public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS"; + } From d6fc28e120b8d8d0d8d065e7954a0f23a0a11459 Mon Sep 17 00:00:00 2001 From: leogtzr Date: Sat, 21 Jan 2017 15:47:54 -0700 Subject: [PATCH 3/5] Changing code to use interfaces instead of implementations. --- .../java/com/iluwatar/caching/LruCache.java | 2 +- .../iluwatar/factorykit/WeaponFactory.java | 3 +- .../iluwatar/hexagonal/banking/MongoBank.java | 3 +- .../database/MongoTicketRepository.java | 8 ++- .../layers/CakeBakingServiceImpl.java | 2 +- memory-dao-test/.springBeans | 16 +++++ memory-dao-test/pom.xml | 71 +++++++++++++++++++ .../src/main/java/com/memory/dao/App.java | 22 ++++++ .../main/java/com/memory/dao/AppConfig.java | 9 +++ .../main/java/com/memory/dao/db/Queries.java | 19 +++++ .../main/java/com/memory/dao/db/UserDAO.java | 11 +++ .../java/com/memory/dao/db/UserDAOImpl.java | 61 ++++++++++++++++ .../main/java/com/memory/dao/pojo/User.java | 38 ++++++++++ memory-dao-test/src/main/resources/beans.xml | 20 ++++++ .../src/main/resources/db-h2-config.xml | 18 +++++ .../src/main/resources/db/sql/create-db.sql | 7 ++ .../src/main/resources/db/sql/insert-data.sql | 3 + .../src/test/java/com/memory/dao/AppTest.java | 30 ++++++++ module/error.txt | 0 module/output.txt | 0 .../com/iluwatar/object/pool/ObjectPool.java | 5 +- .../com/iluwatar/semaphore/FruitBowl.java | 3 +- .../main/java/com/iluwatar/servant/App.java | 3 +- .../com/iluwatar/servant/ServantTest.java | 5 +- 24 files changed, 346 insertions(+), 13 deletions(-) create mode 100644 memory-dao-test/.springBeans create mode 100644 memory-dao-test/pom.xml create mode 100644 memory-dao-test/src/main/java/com/memory/dao/App.java create mode 100644 memory-dao-test/src/main/java/com/memory/dao/AppConfig.java create mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/Queries.java create mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java create mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java create mode 100644 memory-dao-test/src/main/java/com/memory/dao/pojo/User.java create mode 100644 memory-dao-test/src/main/resources/beans.xml create mode 100755 memory-dao-test/src/main/resources/db-h2-config.xml create mode 100755 memory-dao-test/src/main/resources/db/sql/create-db.sql create mode 100755 memory-dao-test/src/main/resources/db/sql/insert-data.sql create mode 100644 memory-dao-test/src/test/java/com/memory/dao/AppTest.java create mode 100644 module/error.txt create mode 100644 module/output.txt diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java index 2fc37ce44..fb9127bdc 100644 --- a/caching/src/main/java/com/iluwatar/caching/LruCache.java +++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java @@ -167,7 +167,7 @@ public class LruCache { * Returns cache data in list form. */ public List getCacheDataInListForm() { - ArrayList listOfCacheData = new ArrayList<>(); + List listOfCacheData = new ArrayList<>(); Node temp = head; while (temp != null) { listOfCacheData.add(temp.userAccount); diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java index 4e8d1f77d..8ee9c6c39 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java @@ -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 consumer) { - HashMap> map = new HashMap<>(); + Map> map = new HashMap<>(); consumer.accept(map::put); return name -> map.get(name).get(); } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java index fd0c1086f..7ccc829ed 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java @@ -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 results = accountsCollection.find(search).limit(1).into(new ArrayList()); + List results = accountsCollection.find(search).limit(1).into(new ArrayList()); if (results.size() > 0) { return results.get(0).getInteger("funds"); } else { diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/database/MongoTicketRepository.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/database/MongoTicketRepository.java index a68eee59a..ac747d4ea 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/database/MongoTicketRepository.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/database/MongoTicketRepository.java @@ -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 findById(LotteryTicketId id) { Document find = new Document("ticketId", id.getId()); - ArrayList results = ticketsCollection.find(find).limit(1).into(new ArrayList()); + List results = ticketsCollection.find(find).limit(1).into(new ArrayList()); 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 findAll() { Map map = new HashMap<>(); - ArrayList docs = ticketsCollection.find(new Document()).into(new ArrayList()); + List docs = ticketsCollection.find(new Document()).into(new ArrayList()); 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 numbers = new HashSet<>(); + Set numbers = new HashSet<>(); for (int num: numArray) { numbers.add(num); } diff --git a/layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java b/layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java index 85a362d90..e8deee73a 100644 --- a/layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java +++ b/layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java @@ -163,7 +163,7 @@ public class CakeBakingServiceImpl implements CakeBakingService { CakeToppingInfo cakeToppingInfo = new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake .getTopping().getCalories()); - ArrayList cakeLayerInfos = new ArrayList<>(); + List cakeLayerInfos = new ArrayList<>(); for (CakeLayer layer : cake.getLayers()) { cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories())); } diff --git a/memory-dao-test/.springBeans b/memory-dao-test/.springBeans new file mode 100644 index 000000000..1fc985600 --- /dev/null +++ b/memory-dao-test/.springBeans @@ -0,0 +1,16 @@ + + + 1 + + + + + + + src/main/resources/beans.xml + + + + + + diff --git a/memory-dao-test/pom.xml b/memory-dao-test/pom.xml new file mode 100644 index 000000000..c8fb9b740 --- /dev/null +++ b/memory-dao-test/pom.xml @@ -0,0 +1,71 @@ + + 4.0.0 + com.memory.dao + memory-dao-test + 0.0.1 + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.12 + + + + org.apache.commons + commons-lang3 + 3.0 + + + + mysql + mysql-connector-java + 5.1.25 + + + + + com.h2database + h2 + 1.4.193 + + + + org.springframework + spring-core + 4.2.5.RELEASE + + + org.springframework + spring-beans + 4.2.5.RELEASE + + + org.springframework + spring-context + 4.2.5.RELEASE + + + + org.springframework + spring-jdbc + 4.2.5.RELEASE + + + + org.springframework + spring-test + 4.2.5.RELEASE + test + + + + + diff --git a/memory-dao-test/src/main/java/com/memory/dao/App.java b/memory-dao-test/src/main/java/com/memory/dao/App.java new file mode 100644 index 000000000..5f5b669ec --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/App.java @@ -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(); + } +} diff --git a/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java b/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java new file mode 100644 index 000000000..3ab6daeeb --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java @@ -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 { +} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java b/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java new file mode 100644 index 000000000..bae78e0c6 --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java @@ -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; + } + +} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java new file mode 100644 index 000000000..ce21259ce --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java @@ -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 findAll(); + +} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java new file mode 100644 index 000000000..23385834a --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java @@ -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 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 findAll() { + + Map params = new HashMap(); + + final List result = namedParameterJDBCTemplate.query(Queries.GET_ALL_USERS.get(), params, new UserMapper()); + + return result; + + } + + private static final class UserMapper implements RowMapper { + @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; + } + } + +} diff --git a/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java b/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java new file mode 100644 index 000000000..776df7a61 --- /dev/null +++ b/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java @@ -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 + "]"; + } + +} diff --git a/memory-dao-test/src/main/resources/beans.xml b/memory-dao-test/src/main/resources/beans.xml new file mode 100644 index 000000000..d6711afcc --- /dev/null +++ b/memory-dao-test/src/main/resources/beans.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + diff --git a/memory-dao-test/src/main/resources/db-h2-config.xml b/memory-dao-test/src/main/resources/db-h2-config.xml new file mode 100755 index 000000000..23e69d35d --- /dev/null +++ b/memory-dao-test/src/main/resources/db-h2-config.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/memory-dao-test/src/main/resources/db/sql/create-db.sql b/memory-dao-test/src/main/resources/db/sql/create-db.sql new file mode 100755 index 000000000..db19f033a --- /dev/null +++ b/memory-dao-test/src/main/resources/db/sql/create-db.sql @@ -0,0 +1,7 @@ +--DROP TABLE users IF EXISTS; + +CREATE TABLE users ( + id INTEGER PRIMARY KEY, + name VARCHAR(30), + email VARCHAR(50) +); diff --git a/memory-dao-test/src/main/resources/db/sql/insert-data.sql b/memory-dao-test/src/main/resources/db/sql/insert-data.sql new file mode 100755 index 000000000..c0988e622 --- /dev/null +++ b/memory-dao-test/src/main/resources/db/sql/insert-data.sql @@ -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'); \ No newline at end of file diff --git a/memory-dao-test/src/test/java/com/memory/dao/AppTest.java b/memory-dao-test/src/test/java/com/memory/dao/AppTest.java new file mode 100644 index 000000000..913d803b8 --- /dev/null +++ b/memory-dao-test/src/test/java/com/memory/dao/AppTest.java @@ -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); + } +} diff --git a/module/error.txt b/module/error.txt new file mode 100644 index 000000000..e69de29bb diff --git a/module/output.txt b/module/output.txt new file mode 100644 index 000000000..e69de29bb diff --git a/object-pool/src/main/java/com/iluwatar/object/pool/ObjectPool.java b/object-pool/src/main/java/com/iluwatar/object/pool/ObjectPool.java index 790641690..510d9dc88 100644 --- a/object-pool/src/main/java/com/iluwatar/object/pool/ObjectPool.java +++ b/object-pool/src/main/java/com/iluwatar/object/pool/ObjectPool.java @@ -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 { - private HashSet available = new HashSet<>(); - private HashSet inUse = new HashSet<>(); + private Set available = new HashSet<>(); + private Set inUse = new HashSet<>(); protected abstract T create(); diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java b/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java index 4f527f01c..a53672109 100644 --- a/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java +++ b/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java @@ -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 = new ArrayList<>(); + private List fruit = new ArrayList<>(); /** * diff --git a/servant/src/main/java/com/iluwatar/servant/App.java b/servant/src/main/java/com/iluwatar/servant/App.java index 33a57e367..0e65e1de2 100644 --- a/servant/src/main/java/com/iluwatar/servant/App.java +++ b/servant/src/main/java/com/iluwatar/servant/App.java @@ -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 guests = new ArrayList<>(); + List guests = new ArrayList<>(); guests.add(k); guests.add(q); diff --git a/servant/src/test/java/com/iluwatar/servant/ServantTest.java b/servant/src/test/java/com/iluwatar/servant/ServantTest.java index e2e61e899..900ccc418 100644 --- a/servant/src/test/java/com/iluwatar/servant/ServantTest.java +++ b/servant/src/test/java/com/iluwatar/servant/ServantTest.java @@ -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 goodCompany = new ArrayList<>(); + final List goodCompany = new ArrayList<>(); goodCompany.add(goodMoodRoyalty); goodCompany.add(goodMoodRoyalty); goodCompany.add(goodMoodRoyalty); - final ArrayList badCompany = new ArrayList<>(); + final List badCompany = new ArrayList<>(); goodCompany.add(goodMoodRoyalty); goodCompany.add(goodMoodRoyalty); goodCompany.add(badMoodRoyalty); From e26215578c3cbc978f4cb52cc872f9570ede9c62 Mon Sep 17 00:00:00 2001 From: leogtzr Date: Sat, 21 Jan 2017 15:49:29 -0700 Subject: [PATCH 4/5] Changing code to use interfaces instead of implementations. --- memory-dao-test/.springBeans | 16 ----- memory-dao-test/pom.xml | 71 ------------------- .../src/main/java/com/memory/dao/App.java | 22 ------ .../main/java/com/memory/dao/AppConfig.java | 9 --- .../main/java/com/memory/dao/db/Queries.java | 19 ----- .../main/java/com/memory/dao/db/UserDAO.java | 11 --- .../java/com/memory/dao/db/UserDAOImpl.java | 61 ---------------- .../main/java/com/memory/dao/pojo/User.java | 38 ---------- memory-dao-test/src/main/resources/beans.xml | 20 ------ .../src/main/resources/db-h2-config.xml | 18 ----- .../src/main/resources/db/sql/create-db.sql | 7 -- .../src/main/resources/db/sql/insert-data.sql | 3 - .../src/test/java/com/memory/dao/AppTest.java | 30 -------- 13 files changed, 325 deletions(-) delete mode 100644 memory-dao-test/.springBeans delete mode 100644 memory-dao-test/pom.xml delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/App.java delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/AppConfig.java delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/Queries.java delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java delete mode 100644 memory-dao-test/src/main/java/com/memory/dao/pojo/User.java delete mode 100644 memory-dao-test/src/main/resources/beans.xml delete mode 100755 memory-dao-test/src/main/resources/db-h2-config.xml delete mode 100755 memory-dao-test/src/main/resources/db/sql/create-db.sql delete mode 100755 memory-dao-test/src/main/resources/db/sql/insert-data.sql delete mode 100644 memory-dao-test/src/test/java/com/memory/dao/AppTest.java diff --git a/memory-dao-test/.springBeans b/memory-dao-test/.springBeans deleted file mode 100644 index 1fc985600..000000000 --- a/memory-dao-test/.springBeans +++ /dev/null @@ -1,16 +0,0 @@ - - - 1 - - - - - - - src/main/resources/beans.xml - - - - - - diff --git a/memory-dao-test/pom.xml b/memory-dao-test/pom.xml deleted file mode 100644 index c8fb9b740..000000000 --- a/memory-dao-test/pom.xml +++ /dev/null @@ -1,71 +0,0 @@ - - 4.0.0 - com.memory.dao - memory-dao-test - 0.0.1 - - - UTF-8 - 1.7 - 1.7 - - - - - junit - junit - 4.12 - - - - org.apache.commons - commons-lang3 - 3.0 - - - - mysql - mysql-connector-java - 5.1.25 - - - - - com.h2database - h2 - 1.4.193 - - - - org.springframework - spring-core - 4.2.5.RELEASE - - - org.springframework - spring-beans - 4.2.5.RELEASE - - - org.springframework - spring-context - 4.2.5.RELEASE - - - - org.springframework - spring-jdbc - 4.2.5.RELEASE - - - - org.springframework - spring-test - 4.2.5.RELEASE - test - - - - - diff --git a/memory-dao-test/src/main/java/com/memory/dao/App.java b/memory-dao-test/src/main/java/com/memory/dao/App.java deleted file mode 100644 index 5f5b669ec..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/App.java +++ /dev/null @@ -1,22 +0,0 @@ -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(); - } -} diff --git a/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java b/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java deleted file mode 100644 index 3ab6daeeb..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/AppConfig.java +++ /dev/null @@ -1,9 +0,0 @@ -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 { -} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java b/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java deleted file mode 100644 index bae78e0c6..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/db/Queries.java +++ /dev/null @@ -1,19 +0,0 @@ -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; - } - -} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java deleted file mode 100644 index ce21259ce..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAO.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.memory.dao.db; - -import com.memory.dao.pojo.User; -import java.util.List; - -public interface UserDAO { - - User findByName(String name); - List findAll(); - -} diff --git a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java b/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java deleted file mode 100644 index 23385834a..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/db/UserDAOImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -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 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 findAll() { - - Map params = new HashMap(); - - final List result = namedParameterJDBCTemplate.query(Queries.GET_ALL_USERS.get(), params, new UserMapper()); - - return result; - - } - - private static final class UserMapper implements RowMapper { - @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; - } - } - -} diff --git a/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java b/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java deleted file mode 100644 index 776df7a61..000000000 --- a/memory-dao-test/src/main/java/com/memory/dao/pojo/User.java +++ /dev/null @@ -1,38 +0,0 @@ -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 + "]"; - } - -} diff --git a/memory-dao-test/src/main/resources/beans.xml b/memory-dao-test/src/main/resources/beans.xml deleted file mode 100644 index d6711afcc..000000000 --- a/memory-dao-test/src/main/resources/beans.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/memory-dao-test/src/main/resources/db-h2-config.xml b/memory-dao-test/src/main/resources/db-h2-config.xml deleted file mode 100755 index 23e69d35d..000000000 --- a/memory-dao-test/src/main/resources/db-h2-config.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/memory-dao-test/src/main/resources/db/sql/create-db.sql b/memory-dao-test/src/main/resources/db/sql/create-db.sql deleted file mode 100755 index db19f033a..000000000 --- a/memory-dao-test/src/main/resources/db/sql/create-db.sql +++ /dev/null @@ -1,7 +0,0 @@ ---DROP TABLE users IF EXISTS; - -CREATE TABLE users ( - id INTEGER PRIMARY KEY, - name VARCHAR(30), - email VARCHAR(50) -); diff --git a/memory-dao-test/src/main/resources/db/sql/insert-data.sql b/memory-dao-test/src/main/resources/db/sql/insert-data.sql deleted file mode 100755 index c0988e622..000000000 --- a/memory-dao-test/src/main/resources/db/sql/insert-data.sql +++ /dev/null @@ -1,3 +0,0 @@ -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'); \ No newline at end of file diff --git a/memory-dao-test/src/test/java/com/memory/dao/AppTest.java b/memory-dao-test/src/test/java/com/memory/dao/AppTest.java deleted file mode 100644 index 913d803b8..000000000 --- a/memory-dao-test/src/test/java/com/memory/dao/AppTest.java +++ /dev/null @@ -1,30 +0,0 @@ -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); - } -} From c6d0d2855713b8f807911529312eb9ae625de594 Mon Sep 17 00:00:00 2001 From: leogtzr Date: Sun, 22 Jan 2017 11:06:57 -0700 Subject: [PATCH 5/5] Reverting initialization on demand holder idiom. --- .../src/main/java/com/iluwatar/singleton/IvoryTower.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java b/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java index ec23ba71b..37917bda8 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java @@ -32,12 +32,10 @@ public final class IvoryTower { */ private IvoryTower() {} - private static class IvoryTowerHolder { - /** + /** * Static to class instance of the class. */ - private static final IvoryTower INSTANCE = new IvoryTower(); - } + private static final IvoryTower INSTANCE = new IvoryTower(); /** * To be called by user to obtain instance of the class. @@ -45,6 +43,6 @@ public final class IvoryTower { * @return instance of the singleton. */ public static IvoryTower getInstance() { - return IvoryTowerHolder.INSTANCE; + return INSTANCE; } }