Adjust checkstyle rules. Make checkstyle fail the build when violations are found. Correct all current checkstyle violations.
This commit is contained in:
@ -8,7 +8,7 @@ public final class IvoryTower {
|
||||
/**
|
||||
* Static to class instance of the class.
|
||||
*/
|
||||
private static final IvoryTower instance = new IvoryTower();
|
||||
private static final IvoryTower INSTANCE = new IvoryTower();
|
||||
|
||||
/**
|
||||
* Private constructor so nobody can instantiate the class.
|
||||
@ -21,6 +21,6 @@ public final class IvoryTower {
|
||||
* @return instance of the singleton.
|
||||
*/
|
||||
public static IvoryTower getInstance() {
|
||||
return instance;
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
@ -11,14 +11,14 @@ package com.iluwatar.singleton;
|
||||
*/
|
||||
public class ThreadSafeDoubleCheckLocking {
|
||||
|
||||
private static volatile ThreadSafeDoubleCheckLocking INSTANCE;
|
||||
private static volatile ThreadSafeDoubleCheckLocking instance;
|
||||
|
||||
/**
|
||||
* private constructor to prevent client from instantiating.
|
||||
*/
|
||||
private ThreadSafeDoubleCheckLocking() {
|
||||
// to prevent instantiating by Reflection call
|
||||
if (INSTANCE != null) {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Already initialized.");
|
||||
}
|
||||
}
|
||||
@ -31,12 +31,12 @@ public class ThreadSafeDoubleCheckLocking {
|
||||
public static ThreadSafeDoubleCheckLocking getInstance() {
|
||||
// local variable increases performance by 25 percent
|
||||
// Joshua Bloch "Effective Java, Second Edition", p. 283-284
|
||||
ThreadSafeDoubleCheckLocking result = INSTANCE;
|
||||
ThreadSafeDoubleCheckLocking result = instance;
|
||||
if (result == null) {
|
||||
synchronized (ThreadSafeDoubleCheckLocking.class) {
|
||||
result = INSTANCE;
|
||||
result = instance;
|
||||
if (result == null) {
|
||||
INSTANCE = result = new ThreadSafeDoubleCheckLocking();
|
||||
instance = result = new ThreadSafeDoubleCheckLocking();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class ThreadSafeLazyLoadedIvoryTower {
|
||||
/**
|
||||
* The instance gets created only when it is called for first time. Lazy-loading
|
||||
*/
|
||||
public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
|
||||
public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() {
|
||||
|
||||
if (instance == null) {
|
||||
instance = new ThreadSafeLazyLoadedIvoryTower();
|
||||
|
@ -1,13 +1,17 @@
|
||||
package com.iluwatar.singleton;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* This class provides several test case that test singleton construction.
|
||||
@ -29,7 +33,7 @@ public class LazyLoadedSingletonThreadSafetyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_MultipleCallsReturnTheSameObjectInSameThread() {
|
||||
public void testMultipleCallsReturnTheSameObjectInSameThread() {
|
||||
// Create several instances in the same calling thread
|
||||
ThreadSafeLazyLoadedIvoryTower instance1 = ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
ThreadSafeLazyLoadedIvoryTower instance2 = ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
@ -42,9 +46,10 @@ public class LazyLoadedSingletonThreadSafetyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_MultipleCallsReturnTheSameObjectInDifferentThreads()
|
||||
public void testMultipleCallsReturnTheSameObjectInDifferentThreads()
|
||||
throws InterruptedException, ExecutionException {
|
||||
{// create several threads and inside each callable instantiate the singleton class
|
||||
{
|
||||
// create several threads and inside each callable instantiate the singleton class
|
||||
ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
List<Callable<NullObject>> threadList = new ArrayList<>();
|
||||
@ -63,7 +68,8 @@ public class LazyLoadedSingletonThreadSafetyTest {
|
||||
// tidy up the executor
|
||||
executorService.shutdown();
|
||||
}
|
||||
{// now check the contents that were added to threadObjects by each thread
|
||||
{
|
||||
// now check the contents that were added to threadObjects by each thread
|
||||
assertEquals(NUM_THREADS, threadObjects.size());
|
||||
assertEquals(threadObjects.get(0), threadObjects.get(1));
|
||||
assertEquals(threadObjects.get(1), threadObjects.get(2));
|
||||
|
Reference in New Issue
Block a user