diff --git a/facade/README.md b/facade/README.md index 42000149898aabfede57dd5a01c4a1a5b9831f47..48650dfbb31c7a4c5c391b3cc111391baec7037c 100644 --- a/facade/README.md +++ b/facade/README.md @@ -76,9 +76,7 @@ public abstract class DwarvenMineWorker { } public void action(Action... actions) { - for (Action action : actions) { - action(action); - } + Arrays.stream(actions).forEach(this::action); } public abstract void work(); @@ -165,9 +163,7 @@ public class DwarvenGoldmineFacade { private static void makeActions(Collection workers, DwarvenMineWorker.Action... actions) { - for (DwarvenMineWorker worker : workers) { - worker.action(actions); - } + workers.forEach(worker -> worker.action(actions)); } } ``` diff --git a/facade/src/main/java/com/iluwatar/facade/App.java b/facade/src/main/java/com/iluwatar/facade/App.java index 8d1c34c39576c5af0270752a05ca38f37f720437..d77695b28ffdb77ae9dbf368dff18aa52d9a447d 100644 --- a/facade/src/main/java/com/iluwatar/facade/App.java +++ b/facade/src/main/java/com/iluwatar/facade/App.java @@ -42,7 +42,7 @@ public class App { * @param args command line args */ public static void main(String[] args) { - DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade(); + var facade = new DwarvenGoldmineFacade(); facade.startNewDay(); facade.digOutGold(); facade.endDay(); diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java index 9982e556b2b478322609c75866bae7215ebed2d5..0cc91f29d6a6b6bfa04090022eea43a35a1d0587 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java @@ -63,8 +63,6 @@ public class DwarvenGoldmineFacade { Collection workers, DwarvenMineWorker.Action... actions ) { - for (DwarvenMineWorker worker : workers) { - worker.action(actions); - } + workers.forEach(worker -> worker.action(actions)); } } diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java b/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java index 8263d71f241b9cbf5fc0beb60ffb7e4f9eb776a4..d52291ad434a10519b8e82e664659e4a0befb06a 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java @@ -23,6 +23,7 @@ package com.iluwatar.facade; +import java.util.Arrays; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,9 +77,7 @@ public abstract class DwarvenMineWorker { * Perform actions. */ public void action(Action... actions) { - for (Action action : actions) { - action(action); - } + Arrays.stream(actions).forEach(this::action); } public abstract void work(); diff --git a/facade/src/test/java/com/iluwatar/facade/AppTest.java b/facade/src/test/java/com/iluwatar/facade/AppTest.java index 828c01ebf61c6956c1bdf604bd0d3256c6a4709a..b6287b02b17427ca2081880ded0bd97325933f39 100644 --- a/facade/src/test/java/com/iluwatar/facade/AppTest.java +++ b/facade/src/test/java/com/iluwatar/facade/AppTest.java @@ -26,15 +26,12 @@ package com.iluwatar.facade; import org.junit.jupiter.api.Test; /** - * * Application test - * */ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/facade/src/test/java/com/iluwatar/facade/DwarvenGoldmineFacadeTest.java b/facade/src/test/java/com/iluwatar/facade/DwarvenGoldmineFacadeTest.java index 33e157b771d22760c51adb1db46f30a550298f31..3b67f37547c6a28c7946c05b7f6c880e47b6a623 100644 --- a/facade/src/test/java/com/iluwatar/facade/DwarvenGoldmineFacadeTest.java +++ b/facade/src/test/java/com/iluwatar/facade/DwarvenGoldmineFacadeTest.java @@ -23,20 +23,19 @@ package com.iluwatar.facade; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; +import java.util.LinkedList; +import java.util.List; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; -import java.util.LinkedList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - /** * Date: 12/9/15 - 9:40 PM * @@ -56,16 +55,16 @@ public class DwarvenGoldmineFacadeTest { appender.stop(); } - /** + /** * Test a complete day cycle in the gold mine by executing all three different steps: {@link * DwarvenGoldmineFacade#startNewDay()}, {@link DwarvenGoldmineFacade#digOutGold()} and {@link * DwarvenGoldmineFacade#endDay()}. - * + *

* See if the workers are doing what's expected from them on each step. */ @Test public void testFullWorkDay() { - final DwarvenGoldmineFacade goldMine = new DwarvenGoldmineFacade(); + final var goldMine = new DwarvenGoldmineFacade(); goldMine.startNewDay(); // On the start of a day, all workers should wake up ... @@ -128,7 +127,9 @@ public class DwarvenGoldmineFacadeTest { } public boolean logContains(String message) { - return log.stream().anyMatch(event -> event.getFormattedMessage().equals(message)); + return log.stream() + .map(ILoggingEvent::getFormattedMessage) + .anyMatch(message::equals); } } diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/App.java b/factory-kit/src/main/java/com/iluwatar/factorykit/App.java index f767fb689359ff2edf398026481fbdf9046e524f..a47ee38226cc8683bdc41eaf5b4fd456bc5e7147 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/App.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/App.java @@ -48,13 +48,13 @@ public class App { * @param args command line args */ public static void main(String[] args) { - WeaponFactory factory = WeaponFactory.factory(builder -> { + var factory = WeaponFactory.factory(builder -> { builder.add(WeaponType.SWORD, Sword::new); builder.add(WeaponType.AXE, Axe::new); builder.add(WeaponType.SPEAR, Spear::new); builder.add(WeaponType.BOW, Bow::new); }); - Weapon axe = factory.create(WeaponType.AXE); + var axe = factory.create(WeaponType.AXE); LOGGER.info(axe.toString()); } } 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 2bdde87f7545c680598d4f4a63d07b853e125235..cf520dc0563bdeb848f4ea38438a6987182a571c 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java @@ -24,7 +24,6 @@ package com.iluwatar.factorykit; import java.util.HashMap; -import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; @@ -52,7 +51,7 @@ public interface WeaponFactory { * @return factory with specified {@link Builder}s */ static WeaponFactory factory(Consumer consumer) { - Map> map = new HashMap<>(); + var map = new HashMap>(); consumer.accept(map::put); return name -> map.get(name).get(); } diff --git a/factory-kit/src/test/java/com/iluwatar/factorykit/app/AppTest.java b/factory-kit/src/test/java/com/iluwatar/factorykit/app/AppTest.java index 2d3481c4884d5473231ea419bf22d128d307f86d..f1d3c65a2f520a728bb7474981dfebe535374973 100644 --- a/factory-kit/src/test/java/com/iluwatar/factorykit/app/AppTest.java +++ b/factory-kit/src/test/java/com/iluwatar/factorykit/app/AppTest.java @@ -33,8 +33,7 @@ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/factory-kit/src/test/java/com/iluwatar/factorykit/factorykit/FactoryKitTest.java b/factory-kit/src/test/java/com/iluwatar/factorykit/factorykit/FactoryKitTest.java index 9892e8497409adaf4ab29bb276b2d1570d0c9f9c..fd8241efe262e35bf7692c96a68bd15c9651cbca 100644 --- a/factory-kit/src/test/java/com/iluwatar/factorykit/factorykit/FactoryKitTest.java +++ b/factory-kit/src/test/java/com/iluwatar/factorykit/factorykit/FactoryKitTest.java @@ -23,6 +23,8 @@ package com.iluwatar.factorykit.factorykit; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.iluwatar.factorykit.Axe; import com.iluwatar.factorykit.Spear; import com.iluwatar.factorykit.Sword; @@ -32,10 +34,8 @@ import com.iluwatar.factorykit.WeaponType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; - - /** - * Test Factory Kit Pattern +/** + * Test Factory Kit Pattern */ public class FactoryKitTest { @@ -51,30 +51,33 @@ public class FactoryKitTest { } /** - * Testing {@link WeaponFactory} to produce a SPEAR asserting that the Weapon is an instance of {@link Spear} + * Testing {@link WeaponFactory} to produce a SPEAR asserting that the Weapon is an instance of + * {@link Spear} */ @Test public void testSpearWeapon() { - Weapon weapon = factory.create(WeaponType.SPEAR); + var weapon = factory.create(WeaponType.SPEAR); verifyWeapon(weapon, Spear.class); } /** - * Testing {@link WeaponFactory} to produce a AXE asserting that the Weapon is an instance of {@link Axe} + * Testing {@link WeaponFactory} to produce a AXE asserting that the Weapon is an instance of + * {@link Axe} */ @Test public void testAxeWeapon() { - Weapon weapon = factory.create(WeaponType.AXE); + var weapon = factory.create(WeaponType.AXE); verifyWeapon(weapon, Axe.class); } /** - * Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of {@link Sword} + * Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of + * {@link Sword} */ @Test public void testWeapon() { - Weapon weapon = factory.create(WeaponType.SWORD); + var weapon = factory.create(WeaponType.SWORD); verifyWeapon(weapon, Sword.class); } diff --git a/factory-method/README.md b/factory-method/README.md index 18cbba2e4a359da7e8190777dac2d0daf623b2d4..48ba6649cbcd9385f58f7397e6fc7bbd3368dab6 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -55,7 +55,7 @@ public class OrcBlacksmith implements Blacksmith { Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured ```java -Blacksmith blacksmith = new ElfBlacksmith(); +var blacksmith = new ElfBlacksmith(); blacksmith.manufactureWeapon(WeaponType.SPEAR); blacksmith.manufactureWeapon(WeaponType.AXE); // Elvish weapons are created diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/App.java b/factory-method/src/main/java/com/iluwatar/factory/method/App.java index 8ebf54b03965141cd20eb2373fb0e51af7e52d5c..1bfd824578ebe6c188af9082c7986009285d91b9 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/App.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/App.java @@ -64,7 +64,7 @@ public class App { */ public static void main(String[] args) { // Lets go to war with Orc weapons - App app = new App(new OrcBlacksmith()); + var app = new App(new OrcBlacksmith()); app.manufactureWeapons(); // Lets go to war with Elf weapons @@ -73,8 +73,7 @@ public class App { } private void manufactureWeapons() { - Weapon weapon; - weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); + var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); LOGGER.info(weapon.toString()); weapon = blacksmith.manufactureWeapon(WeaponType.AXE); LOGGER.info(weapon.toString()); diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java index 0da783a34352573642f98776fb323373b8948c3a..b6f29e43a937c659fdd260a4b349b0f545f3880c 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java @@ -23,6 +23,7 @@ package com.iluwatar.factory.method; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -35,14 +36,12 @@ public class ElfBlacksmith implements Blacksmith { static { ELFARSENAL = new HashMap<>(WeaponType.values().length); - for (WeaponType type : WeaponType.values()) { - ELFARSENAL.put(type, new ElfWeapon(type)); - } + Arrays.stream(WeaponType.values()).forEach(type -> ELFARSENAL.put(type, new ElfWeapon(type))); } - + @Override public Weapon manufactureWeapon(WeaponType weaponType) { return ELFARSENAL.get(weaponType); } - + } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java index 376b2ec34283cc5cc3d07f18b36fa5ec29c68e39..b048300855df576c509365ac228c7aa3866c35ef 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java @@ -23,6 +23,7 @@ package com.iluwatar.factory.method; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -35,9 +36,7 @@ public class OrcBlacksmith implements Blacksmith { static { ORCARSENAL = new HashMap<>(WeaponType.values().length); - for (WeaponType type : WeaponType.values()) { - ORCARSENAL.put(type, new OrcWeapon(type)); - } + Arrays.stream(WeaponType.values()).forEach(type -> ORCARSENAL.put(type, new OrcWeapon(type))); } @Override diff --git a/factory-method/src/test/java/com/iluwatar/factory/method/AppTest.java b/factory-method/src/test/java/com/iluwatar/factory/method/AppTest.java index eb71fb16724980c35a81c1b217b532130116f0c1..c23295d9a0ff77e88e361f6df6de400017b72e64 100644 --- a/factory-method/src/test/java/com/iluwatar/factory/method/AppTest.java +++ b/factory-method/src/test/java/com/iluwatar/factory/method/AppTest.java @@ -25,15 +25,12 @@ package com.iluwatar.factory.method; import org.junit.jupiter.api.Test; -import java.io.IOException; - /** * Tests that Factory Method example runs without errors. */ public class AppTest { @Test - public void test() throws IOException { - String[] args = {}; - App.main(args); + public void test() { + App.main(new String[]{}); } } diff --git a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java index eab890002f249e5bbddb3e4922d1ada252bfa838..68960204a2b375d89c70c538e570588cb4e41090 100644 --- a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java +++ b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java @@ -23,79 +23,80 @@ package com.iluwatar.factory.method; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + /** * The Factory Method is a creational design pattern which uses factory methods to deal with the * problem of creating objects without specifying the exact class of object that will be created. * This is done by creating objects via calling a factory method either specified in an interface * and implemented by child classes, or implemented in a base class and optionally overridden by * derived classes—rather than by calling a constructor. - * - *

Factory produces the object of its liking. - * The weapon {@link Weapon} manufactured by the - * blacksmith depends on the kind of factory implementation it is referring to. + * + *

Factory produces the object of its liking. + * The weapon {@link Weapon} manufactured by the blacksmith depends on the kind of factory + * implementation it is referring to. *

*/ public class FactoryMethodTest { /** - * Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance - * of {@link OrcWeapon}. + * Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance of + * {@link OrcWeapon}. */ @Test public void testOrcBlacksmithWithSpear() { - Blacksmith blacksmith = new OrcBlacksmith(); - Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); + var blacksmith = new OrcBlacksmith(); + var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class); } /** - * Testing {@link OrcBlacksmith} to produce an AXE asserting that the Weapon is an instance - * of {@link OrcWeapon}. + * Testing {@link OrcBlacksmith} to produce an AXE asserting that the Weapon is an instance of + * {@link OrcWeapon}. */ @Test public void testOrcBlacksmithWithAxe() { - Blacksmith blacksmith = new OrcBlacksmith(); - Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE); + var blacksmith = new OrcBlacksmith(); + var weapon = blacksmith.manufactureWeapon(WeaponType.AXE); verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class); } /** - * Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an - * instance of {@link ElfWeapon}. + * Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an instance + * of {@link ElfWeapon}. */ @Test public void testElfBlacksmithWithShortSword() { - Blacksmith blacksmith = new ElfBlacksmith(); - Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); + var blacksmith = new ElfBlacksmith(); + var weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class); } /** - * Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance - * of {@link ElfWeapon}. + * Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance of + * {@link ElfWeapon}. */ @Test public void testElfBlacksmithWithSpear() { - Blacksmith blacksmith = new ElfBlacksmith(); - Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); + var blacksmith = new ElfBlacksmith(); + var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class); } /** * This method asserts that the weapon object that is passed is an instance of the clazz and the * weapon is of type expectedWeaponType. - * - * @param weapon weapon object which is to be verified + * + * @param weapon weapon object which is to be verified * @param expectedWeaponType expected WeaponType of the weapon - * @param clazz expected class of the weapon + * @param clazz expected class of the weapon */ private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName()); - assertEquals(expectedWeaponType, weapon.getWeaponType(), "Weapon must be of weaponType: " + expectedWeaponType); + assertEquals(expectedWeaponType, weapon + .getWeaponType(), "Weapon must be of weaponType: " + expectedWeaponType); } } diff --git a/feature-toggle/src/main/java/com/iluwatar/featuretoggle/App.java b/feature-toggle/src/main/java/com/iluwatar/featuretoggle/App.java index af8e731c8a98516edb6f9e3a001681f938bc0624..ceb926b63f68e261cb2cdddd56345427bf89223f 100644 --- a/feature-toggle/src/main/java/com/iluwatar/featuretoggle/App.java +++ b/feature-toggle/src/main/java/com/iluwatar/featuretoggle/App.java @@ -72,33 +72,33 @@ public class App { */ public static void main(String[] args) { - final Properties properties = new Properties(); + final var properties = new Properties(); properties.put("enhancedWelcome", true); - Service service = new PropertiesFeatureToggleVersion(properties); - final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); + var service = new PropertiesFeatureToggleVersion(properties); + final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); LOGGER.info(welcomeMessage); // --------------------------------------------- - final Properties turnedOff = new Properties(); + final var turnedOff = new Properties(); turnedOff.put("enhancedWelcome", false); - Service turnedOffService = new PropertiesFeatureToggleVersion(turnedOff); - final String welcomeMessageturnedOff = + var turnedOffService = new PropertiesFeatureToggleVersion(turnedOff); + final var welcomeMessageturnedOff = turnedOffService.getWelcomeMessage(new User("Jamie No Code")); LOGGER.info(welcomeMessageturnedOff); // -------------------------------------------- - Service service2 = new TieredFeatureToggleVersion(); + var service2 = new TieredFeatureToggleVersion(); - final User paidUser = new User("Jamie Coder"); - final User freeUser = new User("Alan Defect"); + final var paidUser = new User("Jamie Coder"); + final var freeUser = new User("Alan Defect"); UserGroup.addUserToPaidGroup(paidUser); UserGroup.addUserToFreeGroup(freeUser); - final String welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser); - final String welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser); + final var welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser); + final var welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser); LOGGER.info(welcomeMessageFreeUser); LOGGER.info(welcomeMessagePaidUser); } diff --git a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java index 0bab8bca4ba28005354758e16354b174439cceba..69641a7ada37d2251ca4059e0045bee6d5744eff 100644 --- a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java +++ b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java @@ -23,17 +23,15 @@ package com.iluwatar.featuretoggle.pattern.propertiesversion; -import com.iluwatar.featuretoggle.pattern.Service; -import com.iluwatar.featuretoggle.user.User; -import org.junit.jupiter.api.Test; - -import java.util.Properties; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.iluwatar.featuretoggle.user.User; +import java.util.Properties; +import org.junit.jupiter.api.Test; + /** * Test Properties Toggle */ @@ -49,7 +47,7 @@ public class PropertiesFeatureToggleVersionTest { @Test public void testNonBooleanProperty() { assertThrows(IllegalArgumentException.class, () -> { - final Properties properties = new Properties(); + final var properties = new Properties(); properties.setProperty("enhancedWelcome", "Something"); new PropertiesFeatureToggleVersion(properties); }); @@ -57,21 +55,21 @@ public class PropertiesFeatureToggleVersionTest { @Test public void testFeatureTurnedOn() { - final Properties properties = new Properties(); + final var properties = new Properties(); properties.put("enhancedWelcome", true); - Service service = new PropertiesFeatureToggleVersion(properties); + var service = new PropertiesFeatureToggleVersion(properties); assertTrue(service.isEnhanced()); - final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); + final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); assertEquals("Welcome Jamie No Code. You're using the enhanced welcome message.", welcomeMessage); } @Test public void testFeatureTurnedOff() { - final Properties properties = new Properties(); + final var properties = new Properties(); properties.put("enhancedWelcome", false); - Service service = new PropertiesFeatureToggleVersion(properties); + var service = new PropertiesFeatureToggleVersion(properties); assertFalse(service.isEnhanced()); - final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); + final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); assertEquals("Welcome to the application.", welcomeMessage); } } diff --git a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersionTest.java b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersionTest.java index 1d161c741a74c5d6c4480f71a14eb595f2d5d02b..bb7195b7d2e63e387d9f401e7c568d48142fbee7 100644 --- a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersionTest.java +++ b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersionTest.java @@ -23,15 +23,15 @@ package com.iluwatar.featuretoggle.pattern.tieredversion; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.iluwatar.featuretoggle.pattern.Service; import com.iluwatar.featuretoggle.user.User; import com.iluwatar.featuretoggle.user.UserGroup; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - /** * Test Tiered Feature Toggle */ @@ -49,15 +49,15 @@ public class TieredFeatureToggleVersionTest { @Test public void testGetWelcomeMessageForPaidUser() { - final String welcomeMessage = service.getWelcomeMessage(paidUser); - final String expected = "You're amazing Jamie Coder. Thanks for paying for this awesome software."; + final var welcomeMessage = service.getWelcomeMessage(paidUser); + final var expected = "You're amazing Jamie Coder. Thanks for paying for this awesome software."; assertEquals(expected, welcomeMessage); } @Test public void testGetWelcomeMessageForFreeUser() { - final String welcomeMessage = service.getWelcomeMessage(freeUser); - final String expected = "I suppose you can use this software."; + final var welcomeMessage = service.getWelcomeMessage(freeUser); + final var expected = "I suppose you can use this software."; assertEquals(expected, welcomeMessage); } diff --git a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/user/UserGroupTest.java b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/user/UserGroupTest.java index 9490b59a5967fc7dac5a82783916a1a3f25b1ae9..9bc29fabfcf70754d675111432288bab23b2062a 100644 --- a/feature-toggle/src/test/java/com/iluwatar/featuretoggle/user/UserGroupTest.java +++ b/feature-toggle/src/test/java/com/iluwatar/featuretoggle/user/UserGroupTest.java @@ -23,34 +23,34 @@ package com.iluwatar.featuretoggle.user; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + /** * Test User Group specific feature */ public class UserGroupTest { @Test - public void testAddUserToFreeGroup() throws Exception { - User user = new User("Free User"); + public void testAddUserToFreeGroup() { + var user = new User("Free User"); UserGroup.addUserToFreeGroup(user); assertFalse(UserGroup.isPaid(user)); } @Test - public void testAddUserToPaidGroup() throws Exception { - User user = new User("Paid User"); + public void testAddUserToPaidGroup() { + var user = new User("Paid User"); UserGroup.addUserToPaidGroup(user); assertTrue(UserGroup.isPaid(user)); } @Test - public void testAddUserToPaidWhenOnFree() throws Exception { - User user = new User("Paid User"); + public void testAddUserToPaidWhenOnFree() { + var user = new User("Paid User"); UserGroup.addUserToFreeGroup(user); assertThrows(IllegalArgumentException.class, () -> { UserGroup.addUserToPaidGroup(user); @@ -58,8 +58,8 @@ public class UserGroupTest { } @Test - public void testAddUserToFreeWhenOnPaid() throws Exception { - User user = new User("Free User"); + public void testAddUserToFreeWhenOnPaid() { + var user = new User("Free User"); UserGroup.addUserToPaidGroup(user); assertThrows(IllegalArgumentException.class, () -> { UserGroup.addUserToFreeGroup(user); diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java index b3f8fc0b6a322b9efd19ce625b31fd9ea045864b..547c657e4c538003a3bf91bd78c4a60012cd0753 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java @@ -28,8 +28,6 @@ import static java.lang.String.valueOf; import com.iluwatar.fluentinterface.fluentiterable.FluentIterable; import com.iluwatar.fluentinterface.fluentiterable.lazy.LazyFluentIterable; import com.iluwatar.fluentinterface.fluentiterable.simple.SimpleFluentIterable; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.StringJoiner; import java.util.function.Function; @@ -57,19 +55,23 @@ public class App { */ public static void main(String[] args) { - List integerList = new ArrayList<>(); - integerList.addAll(List.of(1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, - -68, 45)); + var integerList = List.of(1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, -68); prettyPrint("The initial list contains: ", integerList); - List firstFiveNegatives = - SimpleFluentIterable.fromCopyOf(integerList).filter(negatives()).first(3).asList(); + var firstFiveNegatives = SimpleFluentIterable + .fromCopyOf(integerList) + .filter(negatives()) + .first(3) + .asList(); prettyPrint("The first three negative values are: ", firstFiveNegatives); - List lastTwoPositives = - SimpleFluentIterable.fromCopyOf(integerList).filter(positives()).last(2).asList(); + var lastTwoPositives = SimpleFluentIterable + .fromCopyOf(integerList) + .filter(positives()) + .last(2) + .asList(); prettyPrint("The last two positive values are: ", lastTwoPositives); SimpleFluentIterable @@ -79,15 +81,21 @@ public class App { .ifPresent(evenNumber -> LOGGER.info("The first even number is: {}", evenNumber)); - List transformedList = - SimpleFluentIterable.fromCopyOf(integerList).filter(negatives()).map(transformToString()) - .asList(); + var transformedList = SimpleFluentIterable + .fromCopyOf(integerList) + .filter(negatives()) + .map(transformToString()) + .asList(); prettyPrint("A string-mapped list of negative numbers contains: ", transformedList); - List lastTwoOfFirstFourStringMapped = - LazyFluentIterable.from(integerList).filter(positives()).first(4).last(2) - .map(number -> "String[" + valueOf(number) + "]").asList(); + var lastTwoOfFirstFourStringMapped = LazyFluentIterable + .from(integerList) + .filter(positives()) + .first(4) + .last(2) + .map(number -> "String[" + valueOf(number) + "]") + .asList(); prettyPrint("The lazy list contains the last two of the first four positive numbers " + "mapped to Strings: ", lastTwoOfFirstFourStringMapped); @@ -96,12 +104,11 @@ public class App { .filter(negatives()) .first(2) .last() - .ifPresent(lastOfFirstTwo -> LOGGER - .info("The last of the first two negatives is: {}", lastOfFirstTwo)); + .ifPresent(number -> LOGGER.info("Last amongst first two negatives: {}", number)); } private static Function transformToString() { - return integer -> "String[" + valueOf(integer) + "]"; + return integer -> "String[" + integer + "]"; } private static Predicate negatives() { @@ -116,14 +123,12 @@ public class App { prettyPrint(", ", prefix, iterable); } - private static void prettyPrint(String delimiter, String prefix, - Iterable iterable) { - StringJoiner joiner = new StringJoiner(delimiter, prefix, "."); - Iterator iterator = iterable.iterator(); - while (iterator.hasNext()) { - joiner.add(iterator.next().toString()); - } - + private static void prettyPrint( + String delimiter, String prefix, + Iterable iterable + ) { + var joiner = new StringJoiner(delimiter, prefix, "."); + iterable.forEach(e -> joiner.add(e.toString())); LOGGER.info(joiner.toString()); } } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java index ea8d7c9bf1fb1e1df5ef76c0778221ea64674437..2ffa4d3dd3558771a0c7308bd6eba09b3e71cc82 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java @@ -24,7 +24,6 @@ package com.iluwatar.fluentinterface.fluentiterable; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -102,11 +101,8 @@ public interface FluentIterable extends Iterable { * @return a list with all objects of the given iterator */ static List copyToList(Iterable iterable) { - List copy = new ArrayList<>(); - Iterator iterator = iterable.iterator(); - while (iterator.hasNext()) { - copy.add(iterator.next()); - } + var copy = new ArrayList(); + iterable.forEach(copy::add); return copy; } } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java index 711b54fc9c1afce708c0223e4b08ea27bda6ef94..c0b52cec7e0389717d51d55e681a1334da7ed3cd 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java @@ -65,7 +65,7 @@ public abstract class DecoratingIterator implements Iterator { if (next == null) { return fromIterator.next(); } else { - final E result = next; + final var result = next; next = null; return result; } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java index 82173c5131892cd0461898e4324f03ba51c761b5..f001c532f2c0d7535e27bcd387609cbb71350bcd 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java @@ -67,14 +67,14 @@ public class LazyFluentIterable implements FluentIterable { */ @Override public FluentIterable filter(Predicate predicate) { - return new LazyFluentIterable() { + return new LazyFluentIterable<>() { @Override public Iterator iterator() { return new DecoratingIterator(iterable.iterator()) { @Override public E computeNext() { while (fromIterator.hasNext()) { - E candidate = fromIterator.next(); + var candidate = fromIterator.next(); if (predicate.test(candidate)) { return candidate; } @@ -94,7 +94,7 @@ public class LazyFluentIterable implements FluentIterable { */ @Override public Optional first() { - Iterator resultIterator = first(1).iterator(); + var resultIterator = first(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -116,7 +116,7 @@ public class LazyFluentIterable implements FluentIterable { @Override public E computeNext() { if (currentIndex < count && fromIterator.hasNext()) { - E candidate = fromIterator.next(); + var candidate = fromIterator.next(); currentIndex++; return candidate; } @@ -134,7 +134,7 @@ public class LazyFluentIterable implements FluentIterable { */ @Override public Optional last() { - Iterator resultIterator = last(1).iterator(); + var resultIterator = last(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -162,25 +162,20 @@ public class LazyFluentIterable implements FluentIterable { public E computeNext() { initialize(); - E candidate = null; while (currentIndex < stopIndex && fromIterator.hasNext()) { currentIndex++; fromIterator.next(); } if (currentIndex >= stopIndex && fromIterator.hasNext()) { - candidate = fromIterator.next(); + return fromIterator.next(); } - return candidate; + return null; } private void initialize() { if (list == null) { list = new ArrayList<>(); - Iterator newIterator = iterable.iterator(); - while (newIterator.hasNext()) { - list.add(newIterator.next()); - } - + iterable.forEach(list::add); totalElementsCount = list.size(); stopIndex = totalElementsCount - count; } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java index 5165ca765e3718d790aebab0eb439c19bb87fb52..b6a0686532ec75a857e07302cff9030877590691 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java @@ -62,9 +62,9 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final FluentIterable filter(Predicate predicate) { - Iterator iterator = iterator(); + var iterator = iterator(); while (iterator.hasNext()) { - E nextElement = iterator.next(); + var nextElement = iterator.next(); if (!predicate.test(nextElement)) { iterator.remove(); } @@ -79,7 +79,7 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final Optional first() { - Iterator resultIterator = first(1).iterator(); + var resultIterator = first(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -92,8 +92,8 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final FluentIterable first(int count) { - Iterator iterator = iterator(); - int currentCount = 0; + var iterator = iterator(); + var currentCount = 0; while (iterator.hasNext()) { iterator.next(); if (currentCount >= count) { @@ -111,7 +111,7 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final Optional last() { - List list = last(1).asList(); + var list = last(1).asList(); if (list.isEmpty()) { return Optional.empty(); } @@ -127,9 +127,9 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final FluentIterable last(int count) { - int remainingElementsCount = getRemainingElementsCount(); - Iterator iterator = iterator(); - int currentIndex = 0; + var remainingElementsCount = getRemainingElementsCount(); + var iterator = iterator(); + var currentIndex = 0; while (iterator.hasNext()) { iterator.next(); if (currentIndex < remainingElementsCount - count) { @@ -150,11 +150,8 @@ public class SimpleFluentIterable implements FluentIterable { */ @Override public final FluentIterable map(Function function) { - List temporaryList = new ArrayList<>(); - Iterator iterator = iterator(); - while (iterator.hasNext()) { - temporaryList.add(function.apply(iterator.next())); - } + var temporaryList = new ArrayList(); + this.forEach(e -> temporaryList.add(function.apply(e))); return from(temporaryList); } @@ -178,7 +175,7 @@ public class SimpleFluentIterable implements FluentIterable { } public static FluentIterable fromCopyOf(Iterable iterable) { - List copy = FluentIterable.copyToList(iterable); + var copy = FluentIterable.copyToList(iterable); return new SimpleFluentIterable<>(copy); } @@ -204,10 +201,8 @@ public class SimpleFluentIterable implements FluentIterable { * @return the count of remaining objects of the current Iterable */ public final int getRemainingElementsCount() { - int counter = 0; - Iterator iterator = iterator(); - while (iterator.hasNext()) { - iterator.next(); + var counter = 0; + for (var ignored : this) { counter++; } return counter; @@ -219,10 +214,8 @@ public class SimpleFluentIterable implements FluentIterable { * @return a new List with the remaining objects. */ public static List toList(Iterator iterator) { - List copy = new ArrayList<>(); - while (iterator.hasNext()) { - copy.add(iterator.next()); - } + var copy = new ArrayList(); + iterator.forEachRemaining(copy::add); return copy; } } diff --git a/fluentinterface/src/test/java/com/iluwatar/fluentinterface/app/AppTest.java b/fluentinterface/src/test/java/com/iluwatar/fluentinterface/app/AppTest.java index f6543f46b3e57afb8a06c9bd1cb818db1cd489ae..6f25d841691d3336db7904c7c98c2033c94336e3 100644 --- a/fluentinterface/src/test/java/com/iluwatar/fluentinterface/app/AppTest.java +++ b/fluentinterface/src/test/java/com/iluwatar/fluentinterface/app/AppTest.java @@ -32,7 +32,6 @@ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/fluentinterface/src/test/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterableTest.java b/fluentinterface/src/test/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterableTest.java index 4ee30d3e5734dc60819f178fad387aad96dbeb3b..2ae69eaf84d3e83b1d5c13d3732bcc055797bd04 100644 --- a/fluentinterface/src/test/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterableTest.java +++ b/fluentinterface/src/test/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterableTest.java @@ -23,16 +23,19 @@ package com.iluwatar.fluentinterface.fluentiterable; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import java.util.Collections; import java.util.List; -import java.util.Optional; -import java.util.Spliterator; import java.util.function.Consumer; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import org.junit.jupiter.api.Test; /** * Date: 12/12/15 - 7:00 PM @@ -50,28 +53,28 @@ public abstract class FluentIterableTest { protected abstract FluentIterable createFluentIterable(final Iterable integers); @Test - public void testFirst() throws Exception { - final List integers = List.of(1, 2, 3, 10, 9, 8); - final Optional first = createFluentIterable(integers).first(); + public void testFirst() { + final var integers = List.of(1, 2, 3, 10, 9, 8); + final var first = createFluentIterable(integers).first(); assertNotNull(first); assertTrue(first.isPresent()); assertEquals(integers.get(0), first.get()); } @Test - public void testFirstEmptyCollection() throws Exception { - final List integers = Collections.emptyList(); - final Optional first = createFluentIterable(integers).first(); + public void testFirstEmptyCollection() { + final var integers = Collections.emptyList(); + final var first = createFluentIterable(integers).first(); assertNotNull(first); assertFalse(first.isPresent()); } @Test - public void testFirstCount() throws Exception { - final List integers = List.of(1, 2, 3, 10, 9, 8); - final List first4 = createFluentIterable(integers) - .first(4) - .asList(); + public void testFirstCount() { + final var integers = List.of(1, 2, 3, 10, 9, 8); + final var first4 = createFluentIterable(integers) + .first(4) + .asList(); assertNotNull(first4); assertEquals(4, first4.size()); @@ -83,11 +86,11 @@ public abstract class FluentIterableTest { } @Test - public void testFirstCountLessItems() throws Exception { - final List integers = List.of(1, 2, 3); - final List first4 = createFluentIterable(integers) - .first(4) - .asList(); + public void testFirstCountLessItems() { + final var integers = List.of(1, 2, 3); + final var first4 = createFluentIterable(integers) + .first(4) + .asList(); assertNotNull(first4); assertEquals(3, first4.size()); @@ -98,28 +101,28 @@ public abstract class FluentIterableTest { } @Test - public void testLast() throws Exception { - final List integers = List.of(1, 2, 3, 10, 9, 8); - final Optional last = createFluentIterable(integers).last(); + public void testLast() { + final var integers = List.of(1, 2, 3, 10, 9, 8); + final var last = createFluentIterable(integers).last(); assertNotNull(last); assertTrue(last.isPresent()); assertEquals(integers.get(integers.size() - 1), last.get()); } @Test - public void testLastEmptyCollection() throws Exception { - final List integers = Collections.emptyList(); - final Optional last = createFluentIterable(integers).last(); + public void testLastEmptyCollection() { + final var integers = Collections.emptyList(); + final var last = createFluentIterable(integers).last(); assertNotNull(last); assertFalse(last.isPresent()); } @Test - public void testLastCount() throws Exception { - final List integers = List.of(1, 2, 3, 10, 9, 8); - final List last4 = createFluentIterable(integers) - .last(4) - .asList(); + public void testLastCount() { + final var integers = List.of(1, 2, 3, 10, 9, 8); + final var last4 = createFluentIterable(integers) + .last(4) + .asList(); assertNotNull(last4); assertEquals(4, last4.size()); @@ -130,11 +133,11 @@ public abstract class FluentIterableTest { } @Test - public void testLastCountLessItems() throws Exception { - final List integers = List.of(1, 2, 3); - final List last4 = createFluentIterable(integers) - .last(4) - .asList(); + public void testLastCountLessItems() { + final var integers = List.of(1, 2, 3); + final var last4 = createFluentIterable(integers) + .last(4) + .asList(); assertNotNull(last4); assertEquals(3, last4.size()); @@ -145,11 +148,11 @@ public abstract class FluentIterableTest { } @Test - public void testFilter() throws Exception { - final List integers = List.of(1, 2, 3, 10, 9, 8); - final List evenItems = createFluentIterable(integers) - .filter(i -> i % 2 == 0) - .asList(); + public void testFilter() { + final var integers = List.of(1, 2, 3, 10, 9, 8); + final var evenItems = createFluentIterable(integers) + .filter(i -> i % 2 == 0) + .asList(); assertNotNull(evenItems); assertEquals(3, evenItems.size()); @@ -159,11 +162,11 @@ public abstract class FluentIterableTest { } @Test - public void testMap() throws Exception { - final List integers = List.of(1, 2, 3); - final List longs = createFluentIterable(integers) - .map(Integer::longValue) - .asList(); + public void testMap() { + final var integers = List.of(1, 2, 3); + final var longs = createFluentIterable(integers) + .map(Integer::longValue) + .asList(); assertNotNull(longs); assertEquals(integers.size(), longs.size()); @@ -174,7 +177,7 @@ public abstract class FluentIterableTest { @Test public void testForEach() { - final List integers = List.of(1, 2, 3); + final var integers = List.of(1, 2, 3); final Consumer consumer = mock(Consumer.class); createFluentIterable(integers).forEach(consumer); @@ -188,8 +191,8 @@ public abstract class FluentIterableTest { @Test public void testSpliterator() throws Exception { - final List integers = List.of(1, 2, 3); - final Spliterator split = createFluentIterable(integers).spliterator(); + final var integers = List.of(1, 2, 3); + final var split = createFluentIterable(integers).spliterator(); assertNotNull(split); } diff --git a/flux/src/main/java/com/iluwatar/flux/app/App.java b/flux/src/main/java/com/iluwatar/flux/app/App.java index 13a16c977437915204e16bc58bdeebfffd51d0cd..1344d9eaa1011cefc0fe1d2be0607d4049f732e2 100644 --- a/flux/src/main/java/com/iluwatar/flux/app/App.java +++ b/flux/src/main/java/com/iluwatar/flux/app/App.java @@ -54,13 +54,13 @@ public class App { public static void main(String[] args) { // initialize and wire the system - MenuStore menuStore = new MenuStore(); + var menuStore = new MenuStore(); Dispatcher.getInstance().registerStore(menuStore); - ContentStore contentStore = new ContentStore(); + var contentStore = new ContentStore(); Dispatcher.getInstance().registerStore(contentStore); - MenuView menuView = new MenuView(); + var menuView = new MenuView(); menuStore.registerView(menuView); - ContentView contentView = new ContentView(); + var contentView = new ContentView(); contentStore.registerView(contentView); // render initial view diff --git a/flux/src/main/java/com/iluwatar/flux/dispatcher/Dispatcher.java b/flux/src/main/java/com/iluwatar/flux/dispatcher/Dispatcher.java index 206ef8b3fdd05d4dd9e1911be508199922f94a9a..cf09ecf6866e32efa765ea82d1f70ae602654f96 100644 --- a/flux/src/main/java/com/iluwatar/flux/dispatcher/Dispatcher.java +++ b/flux/src/main/java/com/iluwatar/flux/dispatcher/Dispatcher.java @@ -70,6 +70,6 @@ public final class Dispatcher { } private void dispatchAction(Action action) { - stores.stream().forEach(store -> store.onAction(action)); + stores.forEach(store -> store.onAction(action)); } } diff --git a/flux/src/main/java/com/iluwatar/flux/store/ContentStore.java b/flux/src/main/java/com/iluwatar/flux/store/ContentStore.java index be2a5e419bdb8d4ab80e973d0fb88ee30a117b67..60d3faea0e2ebff09339e38fc75d0bbea8162110 100644 --- a/flux/src/main/java/com/iluwatar/flux/store/ContentStore.java +++ b/flux/src/main/java/com/iluwatar/flux/store/ContentStore.java @@ -38,7 +38,7 @@ public class ContentStore extends Store { @Override public void onAction(Action action) { if (action.getType().equals(ActionType.CONTENT_CHANGED)) { - ContentAction contentAction = (ContentAction) action; + var contentAction = (ContentAction) action; content = contentAction.getContent(); notifyChange(); } diff --git a/flux/src/main/java/com/iluwatar/flux/store/MenuStore.java b/flux/src/main/java/com/iluwatar/flux/store/MenuStore.java index 4757a563e63f4bacdf7c59582add4bd54dfcac4e..819500c8a360c86230e21b7c7c13b5cef2620861 100644 --- a/flux/src/main/java/com/iluwatar/flux/store/MenuStore.java +++ b/flux/src/main/java/com/iluwatar/flux/store/MenuStore.java @@ -38,7 +38,7 @@ public class MenuStore extends Store { @Override public void onAction(Action action) { if (action.getType().equals(ActionType.MENU_ITEM_SELECTED)) { - MenuAction menuAction = (MenuAction) action; + var menuAction = (MenuAction) action; selected = menuAction.getMenuItem(); notifyChange(); } diff --git a/flux/src/main/java/com/iluwatar/flux/store/Store.java b/flux/src/main/java/com/iluwatar/flux/store/Store.java index b0fc8bac0d23622307db6a0403d573ab92de31ae..cfbdf4af54efdb915907d1f2ad75239b55e93a38 100644 --- a/flux/src/main/java/com/iluwatar/flux/store/Store.java +++ b/flux/src/main/java/com/iluwatar/flux/store/Store.java @@ -42,6 +42,6 @@ public abstract class Store { } protected void notifyChange() { - views.stream().forEach(view -> view.storeChanged(this)); + views.forEach(view -> view.storeChanged(this)); } } diff --git a/flux/src/main/java/com/iluwatar/flux/view/ContentView.java b/flux/src/main/java/com/iluwatar/flux/view/ContentView.java index fc794d8dc6bf34e3ffcb0d3896dd5a0a104724b4..b71a2c53ba388f894aeb2ad3a1b616ae30c79628 100644 --- a/flux/src/main/java/com/iluwatar/flux/view/ContentView.java +++ b/flux/src/main/java/com/iluwatar/flux/view/ContentView.java @@ -40,7 +40,7 @@ public class ContentView implements View { @Override public void storeChanged(Store store) { - ContentStore contentStore = (ContentStore) store; + var contentStore = (ContentStore) store; content = contentStore.getContent(); render(); } diff --git a/flux/src/main/java/com/iluwatar/flux/view/MenuView.java b/flux/src/main/java/com/iluwatar/flux/view/MenuView.java index c09d146a92986a0d13e435570deb0741734c97f3..f4cdf7a4d7decdbe38541ffd12194cc744cf57a4 100644 --- a/flux/src/main/java/com/iluwatar/flux/view/MenuView.java +++ b/flux/src/main/java/com/iluwatar/flux/view/MenuView.java @@ -41,14 +41,14 @@ public class MenuView implements View { @Override public void storeChanged(Store store) { - MenuStore menuStore = (MenuStore) store; + var menuStore = (MenuStore) store; selected = menuStore.getSelected(); render(); } @Override public void render() { - for (MenuItem item : MenuItem.values()) { + for (var item : MenuItem.values()) { if (selected.equals(item)) { LOGGER.info("* {}", item); } else { diff --git a/flux/src/test/java/com/iluwatar/flux/action/ContentTest.java b/flux/src/test/java/com/iluwatar/flux/action/ContentTest.java index 3e047db216ca56bb4a3dd8754cb079bbfa74953b..f0f41f8ce0f7bd3fc7d10800ac8cd0972d62f864 100644 --- a/flux/src/test/java/com/iluwatar/flux/action/ContentTest.java +++ b/flux/src/test/java/com/iluwatar/flux/action/ContentTest.java @@ -23,11 +23,11 @@ package com.iluwatar.flux.action; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; + /** * Date: 12/12/15 - 10:11 PM * @@ -36,9 +36,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; public class ContentTest { @Test - public void testToString() throws Exception { - for (final Content content : Content.values()) { - final String toString = content.toString(); + public void testToString() { + for (final var content : Content.values()) { + final var toString = content.toString(); assertNotNull(toString); assertFalse(toString.trim().isEmpty()); } diff --git a/flux/src/test/java/com/iluwatar/flux/action/MenuItemTest.java b/flux/src/test/java/com/iluwatar/flux/action/MenuItemTest.java index 272c3a31b7c0f2fa8e012f97216a408dcfa7b4c1..374f27daaa4c73cc7c0f545a3f30cb0ccfddf9b0 100644 --- a/flux/src/test/java/com/iluwatar/flux/action/MenuItemTest.java +++ b/flux/src/test/java/com/iluwatar/flux/action/MenuItemTest.java @@ -23,11 +23,11 @@ package com.iluwatar.flux.action; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; + /** * Date: 12/12/15 - 10:15 PM * @@ -36,9 +36,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; public class MenuItemTest { @Test - public void testToString() throws Exception { - for (final MenuItem menuItem : MenuItem.values()) { - final String toString = menuItem.toString(); + public void testToString() { + for (final var menuItem : MenuItem.values()) { + final var toString = menuItem.toString(); assertNotNull(toString); assertFalse(toString.trim().isEmpty()); } diff --git a/flux/src/test/java/com/iluwatar/flux/app/AppTest.java b/flux/src/test/java/com/iluwatar/flux/app/AppTest.java index f4b58a9b11cb2e9bc588f139fabc10123035c2a8..8916ad4de856b260bcafc2e01235dfd76edda71e 100644 --- a/flux/src/test/java/com/iluwatar/flux/app/AppTest.java +++ b/flux/src/test/java/com/iluwatar/flux/app/AppTest.java @@ -26,15 +26,12 @@ package com.iluwatar.flux.app; import org.junit.jupiter.api.Test; /** - * * Application test - * */ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/flux/src/test/java/com/iluwatar/flux/dispatcher/DispatcherTest.java b/flux/src/test/java/com/iluwatar/flux/dispatcher/DispatcherTest.java index 33ee1b69d5c564bbfc5ab6249e3f9e1bf34965bf..5212a57d839675caff42bf06ddc485173f3a6875 100644 --- a/flux/src/test/java/com/iluwatar/flux/dispatcher/DispatcherTest.java +++ b/flux/src/test/java/com/iluwatar/flux/dispatcher/DispatcherTest.java @@ -23,6 +23,14 @@ package com.iluwatar.flux.dispatcher; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + import com.iluwatar.flux.action.Action; import com.iluwatar.flux.action.ActionType; import com.iluwatar.flux.action.Content; @@ -30,23 +38,12 @@ import com.iluwatar.flux.action.ContentAction; import com.iluwatar.flux.action.MenuAction; import com.iluwatar.flux.action.MenuItem; import com.iluwatar.flux.store.Store; +import java.util.List; +import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - /** * Date: 12/12/15 - 8:22 PM * @@ -61,53 +58,56 @@ public class DispatcherTest { */ @BeforeEach public void setUp() throws Exception { - final Constructor constructor; - constructor = Dispatcher.class.getDeclaredConstructor(); + final var constructor = Dispatcher.class.getDeclaredConstructor(); constructor.setAccessible(true); - final Field field = Dispatcher.class.getDeclaredField("instance"); + final var field = Dispatcher.class.getDeclaredField("instance"); field.setAccessible(true); field.set(Dispatcher.getInstance(), constructor.newInstance()); } @Test - public void testGetInstance() throws Exception { + public void testGetInstance() { assertNotNull(Dispatcher.getInstance()); assertSame(Dispatcher.getInstance(), Dispatcher.getInstance()); } @Test - public void testMenuItemSelected() throws Exception { - final Dispatcher dispatcher = Dispatcher.getInstance(); + public void testMenuItemSelected() { + final var dispatcher = Dispatcher.getInstance(); - final Store store = mock(Store.class); + final var store = mock(Store.class); dispatcher.registerStore(store); dispatcher.menuItemSelected(MenuItem.HOME); dispatcher.menuItemSelected(MenuItem.COMPANY); // We expect 4 events, 2 menu selections and 2 content change actions - final ArgumentCaptor actionCaptor = ArgumentCaptor.forClass(Action.class); + final var actionCaptor = ArgumentCaptor.forClass(Action.class); verify(store, times(4)).onAction(actionCaptor.capture()); verifyNoMoreInteractions(store); - final List actions = actionCaptor.getAllValues(); - final List menuActions = actions.stream() - .filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)) - .map(a -> (MenuAction) a) - .collect(Collectors.toList()); + final var actions = actionCaptor.getAllValues(); + final var menuActions = actions.stream() + .filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)) + .map(a -> (MenuAction) a) + .collect(Collectors.toList()); - final List contentActions = actions.stream() - .filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)) - .map(a -> (ContentAction) a) - .collect(Collectors.toList()); + final var contentActions = actions.stream() + .filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)) + .map(a -> (ContentAction) a) + .collect(Collectors.toList()); assertEquals(2, menuActions.size()); - assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count()); - assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count()); + assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals) + .count()); + assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem) + .filter(MenuItem.COMPANY::equals).count()); assertEquals(2, contentActions.size()); - assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count()); - assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count()); + assertEquals(1, contentActions.stream().map(ContentAction::getContent) + .filter(Content.PRODUCTS::equals).count()); + assertEquals(1, contentActions.stream().map(ContentAction::getContent) + .filter(Content.COMPANY::equals).count()); } diff --git a/flux/src/test/java/com/iluwatar/flux/store/ContentStoreTest.java b/flux/src/test/java/com/iluwatar/flux/store/ContentStoreTest.java index 5bb35a2f258ca5be65a97cfbcb214aeda5a7cbb6..cca59a5ab0086a947e0fa335b1ab8a55f8370e14 100644 --- a/flux/src/test/java/com/iluwatar/flux/store/ContentStoreTest.java +++ b/flux/src/test/java/com/iluwatar/flux/store/ContentStoreTest.java @@ -23,20 +23,20 @@ package com.iluwatar.flux.store; -import com.iluwatar.flux.action.Content; -import com.iluwatar.flux.action.ContentAction; -import com.iluwatar.flux.action.MenuAction; -import com.iluwatar.flux.action.MenuItem; -import com.iluwatar.flux.view.View; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; + +import com.iluwatar.flux.action.Content; +import com.iluwatar.flux.action.ContentAction; +import com.iluwatar.flux.action.MenuAction; +import com.iluwatar.flux.action.MenuItem; +import com.iluwatar.flux.view.View; +import org.junit.jupiter.api.Test; /** * Date: 12/12/15 - 10:18 PM @@ -46,10 +46,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; public class ContentStoreTest { @Test - public void testOnAction() throws Exception { - final ContentStore contentStore = new ContentStore(); + public void testOnAction() { + final var contentStore = new ContentStore(); - final View view = mock(View.class); + final var view = mock(View.class); contentStore.registerView(view); verifyZeroInteractions(view); diff --git a/flux/src/test/java/com/iluwatar/flux/store/MenuStoreTest.java b/flux/src/test/java/com/iluwatar/flux/store/MenuStoreTest.java index 8085326c86a9f84029e3a9aa5c3c907079792bcf..24a05f48659ac8a852dd99c972366fa7706d74f1 100644 --- a/flux/src/test/java/com/iluwatar/flux/store/MenuStoreTest.java +++ b/flux/src/test/java/com/iluwatar/flux/store/MenuStoreTest.java @@ -23,20 +23,20 @@ package com.iluwatar.flux.store; -import com.iluwatar.flux.action.Content; -import com.iluwatar.flux.action.ContentAction; -import com.iluwatar.flux.action.MenuAction; -import com.iluwatar.flux.action.MenuItem; -import com.iluwatar.flux.view.View; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; + +import com.iluwatar.flux.action.Content; +import com.iluwatar.flux.action.ContentAction; +import com.iluwatar.flux.action.MenuAction; +import com.iluwatar.flux.action.MenuItem; +import com.iluwatar.flux.view.View; +import org.junit.jupiter.api.Test; /** * Date: 12/12/15 - 10:18 PM @@ -46,10 +46,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; public class MenuStoreTest { @Test - public void testOnAction() throws Exception { - final MenuStore menuStore = new MenuStore(); + public void testOnAction() { + final var menuStore = new MenuStore(); - final View view = mock(View.class); + final var view = mock(View.class); menuStore.registerView(view); verifyZeroInteractions(view); diff --git a/flux/src/test/java/com/iluwatar/flux/view/ContentViewTest.java b/flux/src/test/java/com/iluwatar/flux/view/ContentViewTest.java index fe6b46618a6d4bb2973ab198af8ad794990f4bfa..15ffc48d0bbded452a4ea4c73c5cfae85fddb0d9 100644 --- a/flux/src/test/java/com/iluwatar/flux/view/ContentViewTest.java +++ b/flux/src/test/java/com/iluwatar/flux/view/ContentViewTest.java @@ -23,15 +23,15 @@ package com.iluwatar.flux.view; -import com.iluwatar.flux.action.Content; -import com.iluwatar.flux.store.ContentStore; -import org.junit.jupiter.api.Test; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import com.iluwatar.flux.action.Content; +import com.iluwatar.flux.store.ContentStore; +import org.junit.jupiter.api.Test; /** * Date: 12/12/15 - 10:31 PM @@ -41,11 +41,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; public class ContentViewTest { @Test - public void testStoreChanged() throws Exception { - final ContentStore store = mock(ContentStore.class); + public void testStoreChanged() { + final var store = mock(ContentStore.class); when(store.getContent()).thenReturn(Content.PRODUCTS); - final ContentView view = new ContentView(); + final var view = new ContentView(); view.storeChanged(store); verify(store, times(1)).getContent(); diff --git a/flux/src/test/java/com/iluwatar/flux/view/MenuViewTest.java b/flux/src/test/java/com/iluwatar/flux/view/MenuViewTest.java index 4383c0063fb63d90a6719719dc654cf5106eda2b..2efe0ba025aed5b404972002af0c622c98048930 100644 --- a/flux/src/test/java/com/iluwatar/flux/view/MenuViewTest.java +++ b/flux/src/test/java/com/iluwatar/flux/view/MenuViewTest.java @@ -23,13 +23,6 @@ package com.iluwatar.flux.view; -import com.iluwatar.flux.action.Action; -import com.iluwatar.flux.action.MenuItem; -import com.iluwatar.flux.dispatcher.Dispatcher; -import com.iluwatar.flux.store.MenuStore; -import com.iluwatar.flux.store.Store; -import org.junit.jupiter.api.Test; - import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -37,6 +30,13 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import com.iluwatar.flux.action.Action; +import com.iluwatar.flux.action.MenuItem; +import com.iluwatar.flux.dispatcher.Dispatcher; +import com.iluwatar.flux.store.MenuStore; +import com.iluwatar.flux.store.Store; +import org.junit.jupiter.api.Test; + /** * Date: 12/12/15 - 10:31 PM * @@ -45,11 +45,11 @@ import static org.mockito.Mockito.when; public class MenuViewTest { @Test - public void testStoreChanged() throws Exception { - final MenuStore store = mock(MenuStore.class); + public void testStoreChanged() { + final var store = mock(MenuStore.class); when(store.getSelected()).thenReturn(MenuItem.HOME); - final MenuView view = new MenuView(); + final var view = new MenuView(); view.storeChanged(store); verify(store, times(1)).getSelected(); @@ -57,11 +57,11 @@ public class MenuViewTest { } @Test - public void testItemClicked() throws Exception { - final Store store = mock(Store.class); + public void testItemClicked() { + final var store = mock(Store.class); Dispatcher.getInstance().registerStore(store); - final MenuView view = new MenuView(); + final var view = new MenuView(); view.itemClicked(MenuItem.PRODUCTS); // We should receive a menu click action and a content changed action diff --git a/flyweight/README.md b/flyweight/README.md index 4847b828505be84912d4bc046905c6d07b7ebdef..a263cc438a7e4d14b310607bf0efe86bf8407059 100644 --- a/flyweight/README.md +++ b/flyweight/README.md @@ -72,7 +72,7 @@ public class PotionFactory { } Potion createPotion(PotionType type) { - Potion potion = potions.get(type); + var potion = potions.get(type); if (potion == null) { switch (type) { case HEALING: @@ -99,7 +99,7 @@ public class PotionFactory { And it can be used as below ```java -PotionFactory factory = new PotionFactory(); +var factory = new PotionFactory(); factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818) factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364) factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818) diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java b/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java index e99cfc8078a8de0914f3779ddc806ef9e9c3f549..4fa7312e56c1669a807be0f2008a6a00b2710d09 100644 --- a/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java +++ b/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java @@ -23,7 +23,6 @@ package com.iluwatar.flyweight; -import java.util.Collections; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +41,7 @@ public class AlchemistShop { * Constructor. */ public AlchemistShop() { - PotionFactory factory = new PotionFactory(); + var factory = new PotionFactory(); topShelf = List.of( factory.createPotion(PotionType.INVISIBILITY), factory.createPotion(PotionType.INVISIBILITY), @@ -68,7 +67,7 @@ public class AlchemistShop { * @return The top shelf potions */ public final List getTopShelf() { - return Collections.unmodifiableList(this.topShelf); + return List.copyOf(this.topShelf); } /** @@ -77,24 +76,16 @@ public class AlchemistShop { * @return The bottom shelf potions */ public final List getBottomShelf() { - return Collections.unmodifiableList(this.bottomShelf); + return List.copyOf(this.bottomShelf); } /** * Enumerate potions. */ public void enumerate() { - LOGGER.info("Enumerating top shelf potions\n"); - - for (Potion p : topShelf) { - p.drink(); - } - + topShelf.forEach(Potion::drink); LOGGER.info("Enumerating bottom shelf potions\n"); - - for (Potion p : bottomShelf) { - p.drink(); - } + bottomShelf.forEach(Potion::drink); } } diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/App.java b/flyweight/src/main/java/com/iluwatar/flyweight/App.java index cbea6b9cc97b6f4492d652fbac51b5e3e63a05ba..8f5ff8742030d2a39c9b688551e5f797396a15ff 100644 --- a/flyweight/src/main/java/com/iluwatar/flyweight/App.java +++ b/flyweight/src/main/java/com/iluwatar/flyweight/App.java @@ -43,7 +43,7 @@ public class App { * @param args command line args */ public static void main(String[] args) { - AlchemistShop alchemistShop = new AlchemistShop(); + var alchemistShop = new AlchemistShop(); alchemistShop.enumerate(); } } diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java b/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java index 19edf0e21502e57ca003905348c34c06dc621cfa..a731c03590fb502b0baa50715f7af2e4f8ff9bd8 100644 --- a/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java +++ b/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java @@ -40,7 +40,7 @@ public class PotionFactory { } Potion createPotion(PotionType type) { - Potion potion = potions.get(type); + var potion = potions.get(type); if (potion == null) { switch (type) { case HEALING: diff --git a/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java b/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java index 16374efb2f95f199d25fa247366a06cacf15f526..50053dddf65c91393d0524a4cf13dd5f0d48348c 100644 --- a/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java +++ b/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java @@ -23,14 +23,12 @@ package com.iluwatar.flyweight; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.ArrayList; +import org.junit.jupiter.api.Test; + /** * Date: 12/12/15 - 10:54 PM * @@ -39,18 +37,18 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; public class AlchemistShopTest { @Test - public void testShop() throws Exception { - final AlchemistShop shop = new AlchemistShop(); + public void testShop() { + final var shop = new AlchemistShop(); - final List bottomShelf = shop.getBottomShelf(); + final var bottomShelf = shop.getBottomShelf(); assertNotNull(bottomShelf); assertEquals(5, bottomShelf.size()); - final List topShelf = shop.getTopShelf(); + final var topShelf = shop.getTopShelf(); assertNotNull(topShelf); assertEquals(8, topShelf.size()); - final List allPotions = new ArrayList<>(); + final var allPotions = new ArrayList(); allPotions.addAll(topShelf); allPotions.addAll(bottomShelf); diff --git a/flyweight/src/test/java/com/iluwatar/flyweight/AppTest.java b/flyweight/src/test/java/com/iluwatar/flyweight/AppTest.java index 8beff17235b5abf9430742385f11c717bc603820..3d81a6db2e7b21afb62e1bff9b61086f9a8e6c9b 100644 --- a/flyweight/src/test/java/com/iluwatar/flyweight/AppTest.java +++ b/flyweight/src/test/java/com/iluwatar/flyweight/AppTest.java @@ -26,15 +26,12 @@ package com.iluwatar.flyweight; import org.junit.jupiter.api.Test; /** - * * Application test - * */ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/front-controller/src/main/java/com/iluwatar/front/controller/App.java b/front-controller/src/main/java/com/iluwatar/front/controller/App.java index 7388a06f80d19eb4f911d9f18e3beb91750b1bd1..d1a77887f7f2101bb5a54d16e3093e4f3fdff271 100644 --- a/front-controller/src/main/java/com/iluwatar/front/controller/App.java +++ b/front-controller/src/main/java/com/iluwatar/front/controller/App.java @@ -47,7 +47,7 @@ public class App { * @param args command line args */ public static void main(String[] args) { - FrontController controller = new FrontController(); + var controller = new FrontController(); controller.handleRequest("Archer"); controller.handleRequest("Catapult"); controller.handleRequest("foobar"); diff --git a/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java b/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java index 91535f9e04d60023421a340632a817f385a8ab20..acdb7b1c8f35f5201ffa57f7f62c1eaa94c95080 100644 --- a/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java +++ b/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java @@ -30,12 +30,12 @@ package com.iluwatar.front.controller; public class FrontController { public void handleRequest(String request) { - Command command = getCommand(request); + var command = getCommand(request); command.process(); } private Command getCommand(String request) { - Class commandClass = getCommandClass(request); + var commandClass = getCommandClass(request); try { return (Command) commandClass.newInstance(); } catch (Exception e) { @@ -44,12 +44,10 @@ public class FrontController { } private static Class getCommandClass(String request) { - Class result; try { - result = Class.forName("com.iluwatar.front.controller." + request + "Command"); + return Class.forName("com.iluwatar.front.controller." + request + "Command"); } catch (ClassNotFoundException e) { - result = UnknownCommand.class; + return UnknownCommand.class; } - return result; } } diff --git a/front-controller/src/test/java/com/iluwatar/front/controller/AppTest.java b/front-controller/src/test/java/com/iluwatar/front/controller/AppTest.java index c77da640dbd709f1b51040cd4a5c503017f8a57a..2ea58c6a6da275fb61f573a273d52fffd525dc99 100644 --- a/front-controller/src/test/java/com/iluwatar/front/controller/AppTest.java +++ b/front-controller/src/test/java/com/iluwatar/front/controller/AppTest.java @@ -26,15 +26,12 @@ package com.iluwatar.front.controller; import org.junit.jupiter.api.Test; /** - * * Application test - * */ public class AppTest { @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } diff --git a/front-controller/src/test/java/com/iluwatar/front/controller/ApplicationExceptionTest.java b/front-controller/src/test/java/com/iluwatar/front/controller/ApplicationExceptionTest.java index 085c317555a4164396663f5308569c01cfcb8363..165d13f44f0d549f5693f94d57680198b3099b3b 100644 --- a/front-controller/src/test/java/com/iluwatar/front/controller/ApplicationExceptionTest.java +++ b/front-controller/src/test/java/com/iluwatar/front/controller/ApplicationExceptionTest.java @@ -36,7 +36,7 @@ public class ApplicationExceptionTest { @Test public void testCause() { - final Exception cause = new Exception(); + final var cause = new Exception(); assertSame(cause, new ApplicationException(cause).getCause()); } diff --git a/front-controller/src/test/java/com/iluwatar/front/controller/CommandTest.java b/front-controller/src/test/java/com/iluwatar/front/controller/CommandTest.java index 82cd4c0da699a96257dd5ed7207866261ed93f5e..46a3ae94b5a1b572b7350e2d3efc8a81df6c5bd4 100644 --- a/front-controller/src/test/java/com/iluwatar/front/controller/CommandTest.java +++ b/front-controller/src/test/java/com/iluwatar/front/controller/CommandTest.java @@ -23,17 +23,15 @@ package com.iluwatar.front.controller; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.iluwatar.front.controller.utils.InMemoryAppender; +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 java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** * Date: 12/13/15 - 1:39 PM * @@ -54,11 +52,11 @@ public class CommandTest { } static List dataProvider() { - final List parameters = new ArrayList<>(); - parameters.add(new Object[]{"Archer", "Displaying archers"}); - parameters.add(new Object[]{"Catapult", "Displaying catapults"}); - parameters.add(new Object[]{"NonExistentCommand", "Error 500"}); - return parameters; + return List.of( + new Object[]{"Archer", "Displaying archers"}, + new Object[]{"Catapult", "Displaying catapults"}, + new Object[]{"NonExistentCommand", "Error 500"} + ); } /** @@ -68,7 +66,7 @@ public class CommandTest { @ParameterizedTest @MethodSource("dataProvider") public void testDisplay(String request, String displayMessage) { - final FrontController frontController = new FrontController(); + final var frontController = new FrontController(); assertEquals(0, appender.getLogSize()); frontController.handleRequest(request); assertEquals(displayMessage, appender.getLastMessage()); diff --git a/front-controller/src/test/java/com/iluwatar/front/controller/FrontControllerTest.java b/front-controller/src/test/java/com/iluwatar/front/controller/FrontControllerTest.java index e1701892b24cbee71ebf19eb734f7e4dedc0d1d7..eac3ae3bc845ea830aacfbd65cf12fe693e48dec 100644 --- a/front-controller/src/test/java/com/iluwatar/front/controller/FrontControllerTest.java +++ b/front-controller/src/test/java/com/iluwatar/front/controller/FrontControllerTest.java @@ -23,17 +23,15 @@ package com.iluwatar.front.controller; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.iluwatar.front.controller.utils.InMemoryAppender; +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 java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** * Date: 12/13/15 - 1:39 PM * @@ -54,11 +52,11 @@ public class FrontControllerTest { } static List dataProvider() { - final List parameters = new ArrayList<>(); - parameters.add(new Object[]{new ArcherCommand(), "Displaying archers"}); - parameters.add(new Object[]{new CatapultCommand(), "Displaying catapults"}); - parameters.add(new Object[]{new UnknownCommand(), "Error 500"}); - return parameters; + return List.of( + new Object[]{new ArcherCommand(), "Displaying archers"}, + new Object[]{new CatapultCommand(), "Displaying catapults"}, + new Object[]{new UnknownCommand(), "Error 500"} + ); } /** diff --git a/front-controller/src/test/java/com/iluwatar/front/controller/ViewTest.java b/front-controller/src/test/java/com/iluwatar/front/controller/ViewTest.java index 3d954a129f1ce09bccbac85cf4078ff0016d37e7..6839d4ad00960d01779e1e6aaf38745a854660b9 100644 --- a/front-controller/src/test/java/com/iluwatar/front/controller/ViewTest.java +++ b/front-controller/src/test/java/com/iluwatar/front/controller/ViewTest.java @@ -23,17 +23,15 @@ package com.iluwatar.front.controller; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.iluwatar.front.controller.utils.InMemoryAppender; +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 java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** * Date: 12/13/15 - 1:39 PM * @@ -54,11 +52,11 @@ public class ViewTest { } static List dataProvider() { - final List parameters = new ArrayList<>(); - parameters.add(new Object[]{new ArcherView(), "Displaying archers"}); - parameters.add(new Object[]{new CatapultView(), "Displaying catapults"}); - parameters.add(new Object[]{new ErrorView(), "Error 500"}); - return parameters; + return List.of( + new Object[]{new ArcherView(), "Displaying archers"}, + new Object[]{new CatapultView(), "Displaying catapults"}, + new Object[]{new ErrorView(), "Error 500"} + ); } /**