Skip to content

Commit 9ec0935

Browse files
authored
Merge pull request #532 from leogtzr/master
Adding initialization-on-demand idiom and noninstantiable class instead of interface constant idiom
2 parents 115a853 + c6d0d28 commit 9ec0935

File tree

13 files changed

+31
-20
lines changed

13 files changed

+31
-20
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);

dao/src/main/java/com/iluwatar/dao/CustomerSchemaSql.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
*/
2323
package com.iluwatar.dao;
2424

25-
public interface CustomerSchemaSql {
25+
public final class CustomerSchemaSql {
2626

27-
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
27+
private CustomerSchemaSql() {}
28+
29+
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
2830
+ "LNAME VARCHAR(100))";
2931

30-
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
32+
public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
33+
3134
}

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
}

module/error.txt

Whitespace-only changes.

module/output.txt

Whitespace-only changes.

object-pool/src/main/java/com/iluwatar/object/pool/ObjectPool.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
package com.iluwatar.object.pool;
2424

2525
import java.util.HashSet;
26+
import java.util.Set;
2627

2728
/**
2829
*
2930
* Generic object pool
3031
*/
3132
public abstract class ObjectPool<T> {
3233

33-
private HashSet<T> available = new HashSet<>();
34-
private HashSet<T> inUse = new HashSet<>();
34+
private Set<T> available = new HashSet<>();
35+
private Set<T> inUse = new HashSet<>();
3536

3637
protected abstract T create();
3738

semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
*/
2323
package com.iluwatar.semaphore;
2424

25+
import java.util.List;
2526
import java.util.ArrayList;
2627

2728
/**
2829
* A FruitBowl contains Fruit.
2930
*/
3031
public class FruitBowl {
3132

32-
private ArrayList<Fruit> fruit = new ArrayList<>();
33+
private List<Fruit> fruit = new ArrayList<>();
3334

3435
/**
3536
*

servant/src/main/java/com/iluwatar/servant/App.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.slf4j.LoggerFactory;
2727

2828
import java.util.ArrayList;
29+
import java.util.List;
2930

3031

3132
/**
@@ -58,7 +59,7 @@ public static void scenario(Servant servant, int compliment) {
5859
King k = new King();
5960
Queen q = new Queen();
6061

61-
ArrayList<Royalty> guests = new ArrayList<>();
62+
List<Royalty> guests = new ArrayList<>();
6263
guests.add(k);
6364
guests.add(q);
6465

servant/src/test/java/com/iluwatar/servant/ServantTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Test;
2626

2727
import java.util.ArrayList;
28+
import java.util.List;
2829

2930
import static org.junit.Assert.*;
3031
import static org.mockito.Mockito.mock;
@@ -74,12 +75,12 @@ public void testCheckIfYouWillBeHanged() throws Exception {
7475
final Royalty badMoodRoyalty = mock(Royalty.class);
7576
when(badMoodRoyalty.getMood()).thenReturn(true);
7677

77-
final ArrayList<Royalty> goodCompany = new ArrayList<>();
78+
final List<Royalty> goodCompany = new ArrayList<>();
7879
goodCompany.add(goodMoodRoyalty);
7980
goodCompany.add(goodMoodRoyalty);
8081
goodCompany.add(goodMoodRoyalty);
8182

82-
final ArrayList<Royalty> badCompany = new ArrayList<>();
83+
final List<Royalty> badCompany = new ArrayList<>();
8384
goodCompany.add(goodMoodRoyalty);
8485
goodCompany.add(goodMoodRoyalty);
8586
goodCompany.add(badMoodRoyalty);

singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
public final class IvoryTower {
2929

3030
/**
31-
* Static to class instance of the class.
31+
* Private constructor so nobody can instantiate the class.
3232
*/
33-
private static final IvoryTower INSTANCE = new IvoryTower();
33+
private IvoryTower() {}
3434

3535
/**
36-
* Private constructor so nobody can instantiate the class.
36+
* Static to class instance of the class.
3737
*/
38-
private IvoryTower() {}
38+
private static final IvoryTower INSTANCE = new IvoryTower();
3939

4040
/**
4141
* To be called by user to obtain instance of the class.

0 commit comments

Comments
 (0)