diff --git a/singleton/src/main/java/com/iluwatar/singleton/App.java b/singleton/src/main/java/com/iluwatar/singleton/App.java index b085bc8af..604b81184 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/App.java +++ b/singleton/src/main/java/com/iluwatar/singleton/App.java @@ -27,40 +27,40 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Singleton pattern ensures that the class can have only one existing instance per Java classloader - * instance and provides global access to it. - *

- * One of the risks of this pattern is that bugs resulting from setting a singleton up in a + *

Singleton pattern ensures that the class can have only one existing instance per Java + * classloader instance and provides global access to it.

+ * + *

One of the risks of this pattern is that bugs resulting from setting a singleton up in a * distributed environment can be tricky to debug, since it will work fine if you debug with a * single classloader. Additionally, these problems can crop up a while after the implementation of * a singleton, since they may start out synchronous and only become async with time, so you it may - * not be clear why you are seeing certain changes in behaviour. - *

- * There are many ways to implement the Singleton. The first one is the eagerly initialized instance - * in {@link IvoryTower}. Eager initialization implies that the implementation is thread safe. If - * you can afford giving up control of the instantiation moment, then this implementation will suit - * you fine. - *

- * The other option to implement eagerly initialized Singleton is enum based Singleton. The example - * is found in {@link EnumIvoryTower}. At first glance the code looks short and simple. However, you - * should be aware of the downsides including committing to implementation strategy, extending the - * enum class, serializability and restrictions to coding. These are extensively discussed in Stack - * Overflow: + * not be clear why you are seeing certain changes in behaviour.

+ * + *

There are many ways to implement the Singleton. The first one is the eagerly initialized + * instance in {@link IvoryTower}. Eager initialization implies that the implementation is thread + * safe. If you can afford giving up control of the instantiation moment, then this implementation + * will suit you fine.

+ * + *

The other option to implement eagerly initialized Singleton is enum based Singleton. The + * example is found in {@link EnumIvoryTower}. At first glance the code looks short and simple. + * However, you should be aware of the downsides including committing to implementation strategy, + * extending the enum class, serializability and restrictions to coding. These are extensively + * discussed in Stack Overflow: * http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing - * -a-singleton-with-javas-enum - *

- * {@link ThreadSafeLazyLoadedIvoryTower} is a Singleton implementation that is initialized on + * -a-singleton-with-javas-enum

+ * + *

{@link ThreadSafeLazyLoadedIvoryTower} is a Singleton implementation that is initialized on * demand. The downside is that it is very slow to access since the whole access method is - * synchronized. - *

- * Another Singleton implementation that is initialized on demand is found in + * synchronized.

+ * + *

Another Singleton implementation that is initialized on demand is found in * {@link ThreadSafeDoubleCheckLocking}. It is somewhat faster than * {@link ThreadSafeLazyLoadedIvoryTower} since it doesn't synchronize the whole access method but - * only the method internals on specific conditions. - *

- * Yet another way to implement thread safe lazily initialized Singleton can be found in + * only the method internals on specific conditions.

+ * + *

Yet another way to implement thread safe lazily initialized Singleton can be found in * {@link InitializingOnDemandHolderIdiom}. However, this implementation requires at least Java 8 - * API level to work. + * API level to work.

*/ public class App { diff --git a/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java b/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java index 2028b7d76..753201dba 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java @@ -24,10 +24,10 @@ package com.iluwatar.singleton; /** - * Enum based singleton implementation. Effective Java 2nd Edition (Joshua Bloch) p. 18 + *

Enum based singleton implementation. Effective Java 2nd Edition (Joshua Bloch) p. 18

* - * This implementation is thread safe, however adding any other method and its thread safety - * is developers responsibility. + *

This implementation is thread safe, however adding any other method and its thread safety + * is developers responsibility.

*/ public enum EnumIvoryTower { diff --git a/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java b/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java index dfd4951f7..faf53a63e 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java +++ b/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java @@ -24,16 +24,16 @@ package com.iluwatar.singleton; /** - * The Initialize-on-demand-holder idiom is a secure way of creating a lazy initialized singleton - * object in Java. - *

- * The technique is as lazy as possible and works in all known versions of Java. It takes advantage - * of language guarantees about class initialization, and will therefore work correctly in all - * Java-compliant compilers and virtual machines. - *

- * The inner class is referenced no earlier (and therefore loaded no earlier by the class loader) than - * the moment that getInstance() is called. Thus, this solution is thread-safe without requiring special - * language constructs (i.e. volatile or synchronized). + *

The Initialize-on-demand-holder idiom is a secure way of creating a lazy initialized singleton + * object in Java.

+ * + *

The technique is as lazy as possible and works in all known versions of Java. It takes + * advantage of language guarantees about class initialization, and will therefore work correctly + * in all Java-compliant compilers and virtual machines.

+ * + *

The inner class is referenced no earlier (and therefore loaded no earlier by the class loader) + * than the moment that getInstance() is called. Thus, this solution is thread-safe without + * requiring special language constructs (i.e. volatile or synchronized).

* */ public final class InitializingOnDemandHolderIdiom { @@ -44,6 +44,8 @@ public final class InitializingOnDemandHolderIdiom { private InitializingOnDemandHolderIdiom() {} /** + * Sigleton instance. + * * @return Singleton instance */ public static InitializingOnDemandHolderIdiom getInstance() { diff --git a/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java b/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java index a1ae935a3..f5d47380f 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java +++ b/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java @@ -24,11 +24,11 @@ package com.iluwatar.singleton; /** - * Double check locking - *

- * http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html - *

- * Broken under Java 1.4. + *

Double check locking.

+ * + *

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

+ * + *

Broken under Java 1.4.

* * @author mortezaadi@gmail.com */ @@ -60,17 +60,21 @@ public final class ThreadSafeDoubleCheckLocking { // Joshua Bloch "Effective Java, Second Edition", p. 283-284 var result = instance; - // Check if singleton instance is initialized. If it is initialized then we can return the instance. + // Check if singleton instance is initialized. + // If it is initialized then we can return the instance. if (result == null) { - // It is not initialized but we cannot be sure because some other thread might have initialized it - // in the meanwhile. So to make sure we need to lock on an object to get mutual exclusion. + // It is not initialized but we cannot be sure because some other thread might have + // initialized it in the meanwhile. + // So to make sure we need to lock on an object to get mutual exclusion. synchronized (ThreadSafeDoubleCheckLocking.class) { - // Again assign the instance to local variable to check if it was initialized by some other thread - // while current thread was blocked to enter the locked zone. If it was initialized then we can - // return the previously created instance just like the previous null check. + // Again assign the instance to local variable to check if it was initialized by some + // other thread while current thread was blocked to enter the locked zone. + // If it was initialized then we can return the previously created instance + // just like the previous null check. result = instance; if (result == null) { - // The instance is still not initialized so we can safely (no other thread can enter this zone) + // The instance is still not initialized so we can safely + // (no other thread can enter this zone) // create an instance and make it our singleton instance. instance = result = new ThreadSafeDoubleCheckLocking(); } diff --git a/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java b/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java index 82580acf6..dd6d05fea 100644 --- a/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java @@ -24,11 +24,11 @@ package com.iluwatar.singleton; /** - * Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization - * mechanism. + *

Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization + * mechanism.

* - * Note: if created by reflection then a singleton will not be created but multiple options in the - * same classloader + *

Note: if created by reflection then a singleton will not be created but multiple options + * in the same classloader

*/ public final class ThreadSafeLazyLoadedIvoryTower { diff --git a/singleton/src/test/java/com/iluwatar/singleton/AppTest.java b/singleton/src/test/java/com/iluwatar/singleton/AppTest.java index 66974e472..75043eae0 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/AppTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/AppTest.java @@ -26,9 +26,7 @@ package com.iluwatar.singleton; import org.junit.jupiter.api.Test; /** - * - * Application test - * + * Application test. */ public class AppTest { diff --git a/singleton/src/test/java/com/iluwatar/singleton/EnumIvoryTowerTest.java b/singleton/src/test/java/com/iluwatar/singleton/EnumIvoryTowerTest.java index 49dfae6b0..6a19ca752 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/EnumIvoryTowerTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/EnumIvoryTowerTest.java @@ -24,14 +24,14 @@ package com.iluwatar.singleton; /** - * Date: 12/29/15 - 19:20 PM + * Date: 12/29/15 - 19:20 PM. * * @author Jeroen Meulemeester */ public class EnumIvoryTowerTest extends SingletonTest { /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. */ public EnumIvoryTowerTest() { super(() -> EnumIvoryTower.INSTANCE); diff --git a/singleton/src/test/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiomTest.java b/singleton/src/test/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiomTest.java index d7021dac7..e85555012 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiomTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiomTest.java @@ -24,14 +24,15 @@ package com.iluwatar.singleton; /** - * Date: 12/29/15 - 19:22 PM + * Date: 12/29/15 - 19:22 PM. * * @author Jeroen Meulemeester */ -public class InitializingOnDemandHolderIdiomTest extends SingletonTest { +public class InitializingOnDemandHolderIdiomTest + extends SingletonTest { /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. */ public InitializingOnDemandHolderIdiomTest() { super(InitializingOnDemandHolderIdiom::getInstance); diff --git a/singleton/src/test/java/com/iluwatar/singleton/IvoryTowerTest.java b/singleton/src/test/java/com/iluwatar/singleton/IvoryTowerTest.java index ac5a145cb..de4dc3a18 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/IvoryTowerTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/IvoryTowerTest.java @@ -24,14 +24,14 @@ package com.iluwatar.singleton; /** - * Date: 12/29/15 - 19:23 PM + * Date: 12/29/15 - 19:23 PM. * * @author Jeroen Meulemeester */ public class IvoryTowerTest extends SingletonTest { /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. */ public IvoryTowerTest() { super(IvoryTower::getInstance); diff --git a/singleton/src/test/java/com/iluwatar/singleton/SingletonTest.java b/singleton/src/test/java/com/iluwatar/singleton/SingletonTest.java index e98796aef..4dc1ecdff 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/SingletonTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/SingletonTest.java @@ -23,8 +23,10 @@ package com.iluwatar.singleton; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import static java.time.Duration.ofMillis; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTimeout; import java.util.ArrayList; import java.util.List; @@ -34,19 +36,17 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.function.Supplier; -import static java.time.Duration.ofMillis; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTimeout; +import org.junit.jupiter.api.Test; /** - * This class provides several test case that test singleton construction. + *

This class provides several test case that test singleton construction.

* - * The first proves that multiple calls to the singleton getInstance object are the same when called - * in the SAME thread. The second proves that multiple calls to the singleton getInstance object are - * the same when called in the DIFFERENT thread. + *

The first proves that multiple calls to the singleton getInstance object are the same when + * called in the SAME thread. The second proves that multiple calls to the singleton getInstance + * object are the same when called in the DIFFERENT thread.

+ * + *

Date: 12/29/15 - 19:25 PM

* - * Date: 12/29/15 - 19:25 PM * @param Supplier method generating singletons * @author Jeroen Meulemeester * @author Richard Jones @@ -54,12 +54,12 @@ import static org.junit.jupiter.api.Assertions.assertTimeout; public abstract class SingletonTest { /** - * The singleton's getInstance method + * The singleton's getInstance method. */ private final Supplier singletonInstanceMethod; /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. * * @param singletonInstanceMethod The singleton's getInstance method */ @@ -68,7 +68,7 @@ public abstract class SingletonTest { } /** - * Test the singleton in a non-concurrent setting + * Test the singleton in a non-concurrent setting. */ @Test public void testMultipleCallsReturnTheSameObjectInSameThread() { @@ -83,7 +83,7 @@ public abstract class SingletonTest { } /** - * Test singleton instance in a concurrent setting + * Test singleton instance in a concurrent setting. */ @Test public void testMultipleCallsReturnTheSameObjectInDifferentThreads() throws Exception { diff --git a/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLockingTest.java b/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLockingTest.java index fff516ad3..8babb081e 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLockingTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLockingTest.java @@ -23,34 +23,35 @@ package com.iluwatar.singleton; -import org.junit.Test; - import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import org.junit.Test; + /** - * Date: 12/29/15 - 19:26 PM + * Date: 12/29/15 - 19:26 PM. * * @author Jeroen Meulemeester */ public class ThreadSafeDoubleCheckLockingTest extends SingletonTest { /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. */ public ThreadSafeDoubleCheckLockingTest() { super(ThreadSafeDoubleCheckLocking::getInstance); } /** - * Test creating new instance by refection + * Test creating new instance by refection. */ @Test(expected = InvocationTargetException.class) public void testCreatingNewInstanceByRefection() throws Exception { ThreadSafeDoubleCheckLocking instance1 = ThreadSafeDoubleCheckLocking.getInstance(); Constructor constructor = ThreadSafeDoubleCheckLocking.class.getDeclaredConstructor(); constructor.setAccessible(true); - ThreadSafeDoubleCheckLocking instance2 = (ThreadSafeDoubleCheckLocking) constructor.newInstance(null); + ThreadSafeDoubleCheckLocking instance2 = + (ThreadSafeDoubleCheckLocking) constructor.newInstance(null); } } \ No newline at end of file diff --git a/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTowerTest.java b/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTowerTest.java index 7ca1caf3d..da04722fc 100644 --- a/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTowerTest.java +++ b/singleton/src/test/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTowerTest.java @@ -24,14 +24,15 @@ package com.iluwatar.singleton; /** - * Date: 12/29/15 - 19:26 PM + * Date: 12/29/15 - 19:26 PM. * * @author Jeroen Meulemeester */ -public class ThreadSafeLazyLoadedIvoryTowerTest extends SingletonTest { +public class ThreadSafeLazyLoadedIvoryTowerTest + extends SingletonTest { /** - * Create a new singleton test instance using the given 'getInstance' method + * Create a new singleton test instance using the given 'getInstance' method. */ public ThreadSafeLazyLoadedIvoryTowerTest() { super(ThreadSafeLazyLoadedIvoryTower::getInstance); diff --git a/strategy/src/main/java/com/iluwatar/strategy/App.java b/strategy/src/main/java/com/iluwatar/strategy/App.java index c6bd3aa3b..c526c7917 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/App.java +++ b/strategy/src/main/java/com/iluwatar/strategy/App.java @@ -28,15 +28,15 @@ import org.slf4j.LoggerFactory; /** * - * The Strategy pattern (also known as the policy pattern) is a software design pattern that enables - * an algorithm's behavior to be selected at runtime. - *

- * Before Java 8 the Strategies needed to be separate classes forcing the developer + *

The Strategy pattern (also known as the policy pattern) is a software design pattern that + * enables an algorithm's behavior to be selected at runtime.

+ * + *

Before Java 8 the Strategies needed to be separate classes forcing the developer * to write lots of boilerplate code. With modern Java it is easy to pass behavior - * with method references and lambdas making the code shorter and more readable. - *

- * In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing object - * ({@link DragonSlayer}) can alter its behavior by changing its strategy. + * with method references and lambdas making the code shorter and more readable.

+ * + *

In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing + * object ({@link DragonSlayer}) can alter its behavior by changing its strategy.

* */ public class App { @@ -44,7 +44,7 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Program entry point + * Program entry point. * * @param args command line args */ diff --git a/strategy/src/main/java/com/iluwatar/strategy/DragonSlayer.java b/strategy/src/main/java/com/iluwatar/strategy/DragonSlayer.java index 0455edaca..f6b91d967 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/DragonSlayer.java +++ b/strategy/src/main/java/com/iluwatar/strategy/DragonSlayer.java @@ -24,9 +24,7 @@ package com.iluwatar.strategy; /** - * * DragonSlayer uses different strategies to slay the dragon. - * */ public class DragonSlayer { diff --git a/strategy/src/main/java/com/iluwatar/strategy/DragonSlayingStrategy.java b/strategy/src/main/java/com/iluwatar/strategy/DragonSlayingStrategy.java index eb89523ad..537b521f4 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/DragonSlayingStrategy.java +++ b/strategy/src/main/java/com/iluwatar/strategy/DragonSlayingStrategy.java @@ -24,9 +24,7 @@ package com.iluwatar.strategy; /** - * * Strategy interface. - * */ @FunctionalInterface public interface DragonSlayingStrategy { diff --git a/strategy/src/main/java/com/iluwatar/strategy/MeleeStrategy.java b/strategy/src/main/java/com/iluwatar/strategy/MeleeStrategy.java index 8cb2f24c1..12f467b07 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/MeleeStrategy.java +++ b/strategy/src/main/java/com/iluwatar/strategy/MeleeStrategy.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * Melee strategy. - * */ public class MeleeStrategy implements DragonSlayingStrategy { diff --git a/strategy/src/main/java/com/iluwatar/strategy/ProjectileStrategy.java b/strategy/src/main/java/com/iluwatar/strategy/ProjectileStrategy.java index 4b6031ddf..769b0d7d9 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/ProjectileStrategy.java +++ b/strategy/src/main/java/com/iluwatar/strategy/ProjectileStrategy.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * Projectile strategy. - * */ public class ProjectileStrategy implements DragonSlayingStrategy { diff --git a/strategy/src/main/java/com/iluwatar/strategy/SpellStrategy.java b/strategy/src/main/java/com/iluwatar/strategy/SpellStrategy.java index ffe85c7a2..dfa47f72b 100644 --- a/strategy/src/main/java/com/iluwatar/strategy/SpellStrategy.java +++ b/strategy/src/main/java/com/iluwatar/strategy/SpellStrategy.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * Spell strategy. - * */ public class SpellStrategy implements DragonSlayingStrategy { diff --git a/strategy/src/test/java/com/iluwatar/strategy/AppTest.java b/strategy/src/test/java/com/iluwatar/strategy/AppTest.java index 598085ce4..713eecf43 100644 --- a/strategy/src/test/java/com/iluwatar/strategy/AppTest.java +++ b/strategy/src/test/java/com/iluwatar/strategy/AppTest.java @@ -26,9 +26,7 @@ package com.iluwatar.strategy; import org.junit.jupiter.api.Test; /** - * - * Application test - * + * Application test. */ public class AppTest { diff --git a/strategy/src/test/java/com/iluwatar/strategy/DragonSlayerTest.java b/strategy/src/test/java/com/iluwatar/strategy/DragonSlayerTest.java index 52dfb3ff1..c95612683 100644 --- a/strategy/src/test/java/com/iluwatar/strategy/DragonSlayerTest.java +++ b/strategy/src/test/java/com/iluwatar/strategy/DragonSlayerTest.java @@ -23,21 +23,21 @@ package com.iluwatar.strategy; -import org.junit.jupiter.api.Test; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import org.junit.jupiter.api.Test; + /** - * Date: 12/29/15 - 10:50 PM + * Date: 12/29/15 - 10:50 PM. * * @author Jeroen Meulemeester */ public class DragonSlayerTest { /** - * Verify if the dragon slayer uses the strategy during battle + * Verify if the dragon slayer uses the strategy during battle. */ @Test public void testGoToBattle() { @@ -50,7 +50,7 @@ public class DragonSlayerTest { } /** - * Verify if the dragon slayer uses the new strategy during battle after a change of strategy + * Verify if the dragon slayer uses the new strategy during battle after a change of strategy. */ @Test public void testChangeStrategy() { diff --git a/strategy/src/test/java/com/iluwatar/strategy/DragonSlayingStrategyTest.java b/strategy/src/test/java/com/iluwatar/strategy/DragonSlayingStrategyTest.java index 67e4b92cc..15106cdd9 100644 --- a/strategy/src/test/java/com/iluwatar/strategy/DragonSlayingStrategyTest.java +++ b/strategy/src/test/java/com/iluwatar/strategy/DragonSlayingStrategyTest.java @@ -23,45 +23,48 @@ package com.iluwatar.strategy; +import static org.junit.jupiter.api.Assertions.assertEquals; + import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.slf4j.LoggerFactory; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** - * Date: 12/29/15 - 10:58 PM + * Date: 12/29/15 - 10:58 PM. * * @author Jeroen Meulemeester */ public class DragonSlayingStrategyTest { /** + * Assembles test parameters. + * * @return The test parameters for each cycle */ static Collection dataProvider() { - return List.of( - new Object[]{ - new MeleeStrategy(), - "With your Excalibur you sever the dragon's head!" - }, - new Object[]{ - new ProjectileStrategy(), - "You shoot the dragon with the magical crossbow and it falls dead on the ground!" - }, - new Object[]{ - new SpellStrategy(), - "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!" - } + return List.of( + new Object[]{ + new MeleeStrategy(), + "With your Excalibur you sever the dragon's head!" + }, + new Object[]{ + new ProjectileStrategy(), + "You shoot the dragon with the magical crossbow and it falls dead on the ground!" + }, + new Object[]{ + new SpellStrategy(), + "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!" + } ); } @@ -79,7 +82,7 @@ public class DragonSlayingStrategyTest { /** - * Test if executing the strategy gives the correct response + * Test if executing the strategy gives the correct response. */ @ParameterizedTest @MethodSource("dataProvider") diff --git a/visitor/src/main/java/com/iluwatar/visitor/App.java b/visitor/src/main/java/com/iluwatar/visitor/App.java index 7b33f7e4d..7f0abb0f0 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/App.java +++ b/visitor/src/main/java/com/iluwatar/visitor/App.java @@ -24,19 +24,18 @@ package com.iluwatar.visitor; /** - * - * Visitor pattern defines mechanism to apply operations on nodes in hierarchy. New operations can - * be added without altering the node interface. - *

- * In this example there is a unit hierarchy beginning from {@link Commander}. This hierarchy is + *

Visitor pattern defines mechanism to apply operations on nodes in hierarchy. New operations + * can be added without altering the node interface.

+ * + *

In this example there is a unit hierarchy beginning from {@link Commander}. This hierarchy is * traversed by visitors. {@link SoldierVisitor} applies its operation on {@link Soldier}s, - * {@link SergeantVisitor} on {@link Sergeant}s and so on. + * {@link SergeantVisitor} on {@link Sergeant}s and so on.

* */ public class App { /** - * Program entry point + * Program entry point. * * @param args command line args */ diff --git a/visitor/src/main/java/com/iluwatar/visitor/Commander.java b/visitor/src/main/java/com/iluwatar/visitor/Commander.java index 938c42f8d..782d0116c 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/Commander.java +++ b/visitor/src/main/java/com/iluwatar/visitor/Commander.java @@ -24,9 +24,7 @@ package com.iluwatar.visitor; /** - * - * Commander - * + * Commander. */ public class Commander extends Unit { diff --git a/visitor/src/main/java/com/iluwatar/visitor/CommanderVisitor.java b/visitor/src/main/java/com/iluwatar/visitor/CommanderVisitor.java index 19ea5846e..c34c8ed9e 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/CommanderVisitor.java +++ b/visitor/src/main/java/com/iluwatar/visitor/CommanderVisitor.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * CommanderVisitor - * + * CommanderVisitor. */ public class CommanderVisitor implements UnitVisitor { diff --git a/visitor/src/main/java/com/iluwatar/visitor/Sergeant.java b/visitor/src/main/java/com/iluwatar/visitor/Sergeant.java index 95de25772..bdc96892a 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/Sergeant.java +++ b/visitor/src/main/java/com/iluwatar/visitor/Sergeant.java @@ -24,9 +24,7 @@ package com.iluwatar.visitor; /** - * - * Sergeant - * + * Sergeant. */ public class Sergeant extends Unit { diff --git a/visitor/src/main/java/com/iluwatar/visitor/SergeantVisitor.java b/visitor/src/main/java/com/iluwatar/visitor/SergeantVisitor.java index fdcdce5ca..59fb405e5 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/SergeantVisitor.java +++ b/visitor/src/main/java/com/iluwatar/visitor/SergeantVisitor.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * SergeantVisitor - * + * SergeantVisitor. */ public class SergeantVisitor implements UnitVisitor { diff --git a/visitor/src/main/java/com/iluwatar/visitor/Soldier.java b/visitor/src/main/java/com/iluwatar/visitor/Soldier.java index 7ab4a26b9..a2a812416 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/Soldier.java +++ b/visitor/src/main/java/com/iluwatar/visitor/Soldier.java @@ -24,9 +24,7 @@ package com.iluwatar.visitor; /** - * - * Soldier - * + * Soldier. */ public class Soldier extends Unit { diff --git a/visitor/src/main/java/com/iluwatar/visitor/SoldierVisitor.java b/visitor/src/main/java/com/iluwatar/visitor/SoldierVisitor.java index 6585246a0..1f19a6458 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/SoldierVisitor.java +++ b/visitor/src/main/java/com/iluwatar/visitor/SoldierVisitor.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * SoldierVisitor - * + * SoldierVisitor. */ public class SoldierVisitor implements UnitVisitor { diff --git a/visitor/src/main/java/com/iluwatar/visitor/Unit.java b/visitor/src/main/java/com/iluwatar/visitor/Unit.java index 49ddb8a89..318e84262 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/Unit.java +++ b/visitor/src/main/java/com/iluwatar/visitor/Unit.java @@ -24,9 +24,7 @@ package com.iluwatar.visitor; /** - * * Interface for the nodes in hierarchy. - * */ public abstract class Unit { @@ -37,7 +35,7 @@ public abstract class Unit { } /** - * Accept visitor + * Accept visitor. */ public void accept(UnitVisitor visitor) { for (var child : children) { diff --git a/visitor/src/main/java/com/iluwatar/visitor/UnitVisitor.java b/visitor/src/main/java/com/iluwatar/visitor/UnitVisitor.java index 086075e5e..8300fd1fe 100644 --- a/visitor/src/main/java/com/iluwatar/visitor/UnitVisitor.java +++ b/visitor/src/main/java/com/iluwatar/visitor/UnitVisitor.java @@ -24,9 +24,7 @@ package com.iluwatar.visitor; /** - * * Visitor interface. - * */ public interface UnitVisitor { diff --git a/visitor/src/test/java/com/iluwatar/visitor/AppTest.java b/visitor/src/test/java/com/iluwatar/visitor/AppTest.java index fb2fdd123..45804a9c6 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/AppTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/AppTest.java @@ -26,9 +26,7 @@ package com.iluwatar.visitor; import org.junit.jupiter.api.Test; /** - * - * Application test - * + * Application test. */ public class AppTest { diff --git a/visitor/src/test/java/com/iluwatar/visitor/CommanderTest.java b/visitor/src/test/java/com/iluwatar/visitor/CommanderTest.java index f3cf823fc..256a0cdbd 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/CommanderTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/CommanderTest.java @@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; /** - * Date: 12/30/15 - 19:45 PM + * Date: 12/30/15 - 19:45 PM. * * @author Jeroen Meulemeester */ public class CommanderTest extends UnitTest { /** - * Create a new test instance for the given {@link Commander} + * Create a new test instance for the given {@link Commander}. */ public CommanderTest() { super(Commander::new); diff --git a/visitor/src/test/java/com/iluwatar/visitor/CommanderVisitorTest.java b/visitor/src/test/java/com/iluwatar/visitor/CommanderVisitorTest.java index ed46ac90a..935baa58d 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/CommanderVisitorTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/CommanderVisitorTest.java @@ -26,14 +26,14 @@ package com.iluwatar.visitor; import java.util.Optional; /** - * Date: 12/30/15 - 18:43 PM + * Date: 12/30/15 - 18:43 PM. * * @author Jeroen Meulemeester */ public class CommanderVisitorTest extends VisitorTest { /** - * Create a new test instance for the given visitor + * Create a new test instance for the given visitor. */ public CommanderVisitorTest() { super( diff --git a/visitor/src/test/java/com/iluwatar/visitor/SergeantTest.java b/visitor/src/test/java/com/iluwatar/visitor/SergeantTest.java index b2e3d5baf..fd977f2bc 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/SergeantTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/SergeantTest.java @@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; /** - * Date: 12/30/15 - 19:45 PM + * Date: 12/30/15 - 19:45 PM. * * @author Jeroen Meulemeester */ public class SergeantTest extends UnitTest { /** - * Create a new test instance for the given {@link Sergeant} + * Create a new test instance for the given {@link Sergeant}. */ public SergeantTest() { super(Sergeant::new); diff --git a/visitor/src/test/java/com/iluwatar/visitor/SergeantVisitorTest.java b/visitor/src/test/java/com/iluwatar/visitor/SergeantVisitorTest.java index f7595b429..05eae49f2 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/SergeantVisitorTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/SergeantVisitorTest.java @@ -26,14 +26,14 @@ package com.iluwatar.visitor; import java.util.Optional; /** - * Date: 12/30/15 - 18:36 PM + * Date: 12/30/15 - 18:36 PM. * * @author Jeroen Meulemeester */ public class SergeantVisitorTest extends VisitorTest { /** - * Create a new test instance for the given visitor + * Create a new test instance for the given visitor. */ public SergeantVisitorTest() { super( diff --git a/visitor/src/test/java/com/iluwatar/visitor/SoldierTest.java b/visitor/src/test/java/com/iluwatar/visitor/SoldierTest.java index 713b190e2..3d2c7698f 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/SoldierTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/SoldierTest.java @@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; /** - * Date: 12/30/15 - 19:45 PM + * Date: 12/30/15 - 19:45 PM. * * @author Jeroen Meulemeester */ public class SoldierTest extends UnitTest { /** - * Create a new test instance for the given {@link Soldier} + * Create a new test instance for the given {@link Soldier}. */ public SoldierTest() { super(Soldier::new); diff --git a/visitor/src/test/java/com/iluwatar/visitor/SoldierVisitorTest.java b/visitor/src/test/java/com/iluwatar/visitor/SoldierVisitorTest.java index e5572f1ee..653d814c5 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/SoldierVisitorTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/SoldierVisitorTest.java @@ -26,14 +26,14 @@ package com.iluwatar.visitor; import java.util.Optional; /** - * Date: 12/30/15 - 18:59 PM + * Date: 12/30/15 - 18:59 PM. * * @author Jeroen Meulemeester */ public class SoldierVisitorTest extends VisitorTest { /** - * Create a new test instance for the given visitor + * Create a new test instance for the given visitor. */ public SoldierVisitorTest() { super( diff --git a/visitor/src/test/java/com/iluwatar/visitor/UnitTest.java b/visitor/src/test/java/com/iluwatar/visitor/UnitTest.java index 4441570d4..7385ea5a7 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/UnitTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/UnitTest.java @@ -23,18 +23,18 @@ package com.iluwatar.visitor; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.function.Function; - import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import java.util.Arrays; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + /** - * Date: 12/30/15 - 18:59 PM + * Date: 12/30/15 - 18:59 PM. * Test related to Units * @param Type of Unit * @author Jeroen Meulemeester @@ -42,12 +42,12 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; public abstract class UnitTest { /** - * Factory to create new instances of the tested unit + * Factory to create new instances of the tested unit. */ private final Function factory; /** - * Create a new test instance for the given unit type {@link U} + * Create a new test instance for the given unit type {@link U}. * * @param factory Factory to create new instances of the tested unit */ @@ -74,7 +74,7 @@ public abstract class UnitTest { } /** - * Verify if the correct visit method is called on the mock, depending on the tested instance + * Verify if the correct visit method is called on the mock, depending on the tested instance. * * @param unit The tested unit instance * @param mockedVisitor The mocked {@link UnitVisitor} who should have gotten a visit by the unit diff --git a/visitor/src/test/java/com/iluwatar/visitor/VisitorTest.java b/visitor/src/test/java/com/iluwatar/visitor/VisitorTest.java index c50c3822d..3c458f6f4 100644 --- a/visitor/src/test/java/com/iluwatar/visitor/VisitorTest.java +++ b/visitor/src/test/java/com/iluwatar/visitor/VisitorTest.java @@ -23,6 +23,8 @@ package com.iluwatar.visitor; +import static org.junit.jupiter.api.Assertions.assertEquals; + import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; @@ -35,10 +37,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; -import static org.junit.jupiter.api.Assertions.assertEquals; - /** - * Date: 12/30/15 - 18:59 PM + * Date: 12/30/15 - 18:59 PM. * Test case for Visitor Pattern * @param Type of UnitVisitor * @author Jeroen Meulemeester @@ -58,34 +58,36 @@ public abstract class VisitorTest { } /** - * The tested visitor instance + * The tested visitor instance. */ private final V visitor; /** - * The optional expected response when being visited by a commander + * The optional expected response when being visited by a commander. */ private final Optional commanderResponse; /** - * The optional expected response when being visited by a sergeant + * The optional expected response when being visited by a sergeant. */ private final Optional sergeantResponse; /** - * The optional expected response when being visited by a soldier + * The optional expected response when being visited by a soldier. */ private final Optional soldierResponse; /** - * Create a new test instance for the given visitor + * Create a new test instance for the given visitor. * * @param commanderResponse The optional expected response when being visited by a commander * @param sergeantResponse The optional expected response when being visited by a sergeant * @param soldierResponse The optional expected response when being visited by a soldier */ - public VisitorTest(final V visitor, final Optional commanderResponse, - final Optional sergeantResponse, final Optional soldierResponse) { + public VisitorTest(final V visitor, + final Optional commanderResponse, + final Optional sergeantResponse, + final Optional soldierResponse) { this.visitor = visitor; this.commanderResponse = commanderResponse;