mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 10:58:51 +00:00
1) Refactored LotteryNumbers to use Joiner from guava library to join lottery numbers. 2) Solved potential thread safety issue in LotteryTicketId class, where it was using raw primitive value and incrementing it which is not thread-safe. So used AtomicInteger for brevity 3) assertEquals arguments were in incorrect order at many places, so changed order of those 4) Replaced assertFalse and assertTrue at some places with assertEquals and assertNotEquals for reducing complexity of code 5) Removed public modifiers from test cases, as they are no more needed by JUnit 5
This commit is contained in:
@@ -110,21 +110,6 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
return result.getInteger("seq");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mongo client
|
||||
*/
|
||||
public MongoClient getMongoClient() {
|
||||
return mongoClient;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return mongo database
|
||||
*/
|
||||
public MongoDatabase getMongoDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return tickets collection
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.PrimitiveIterator;
|
||||
@@ -84,15 +86,7 @@ public class LotteryNumbers {
|
||||
* @return numbers as comma separated string
|
||||
*/
|
||||
public String getNumbersAsString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
for (int i = 0; i < NUM_NUMBERS; i++) {
|
||||
builder.append(iterator.next());
|
||||
if (i < NUM_NUMBERS - 1) {
|
||||
builder.append(",");
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
return Joiner.on(',').join(numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,17 +22,19 @@
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
|
||||
/**
|
||||
* Lottery ticked id
|
||||
*/
|
||||
public class LotteryTicketId {
|
||||
|
||||
private static volatile int numAllocated;
|
||||
private static AtomicInteger numAllocated = new AtomicInteger(0);
|
||||
private final int id;
|
||||
|
||||
public LotteryTicketId() {
|
||||
this.id = numAllocated + 1;
|
||||
numAllocated++;
|
||||
this.id = numAllocated.incrementAndGet();
|
||||
}
|
||||
|
||||
public LotteryTicketId(int id) {
|
||||
|
||||
Reference in New Issue
Block a user