📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
		| @@ -23,8 +23,7 @@ | ||||
|  | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
|  | ||||
| /** | ||||
|  * The Factory Method is a creational design pattern which uses factory methods to deal with the | ||||
| @@ -37,14 +36,13 @@ import org.slf4j.LoggerFactory; | ||||
|  * creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses ( | ||||
|  * {@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of | ||||
|  * their liking. | ||||
|  *  | ||||
|  * | ||||
|  */ | ||||
| @Slf4j | ||||
| public class App { | ||||
|  | ||||
|   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||||
|  | ||||
|   private final Blacksmith blacksmith; | ||||
|    | ||||
|  | ||||
|   /** | ||||
|    * Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture  | ||||
|    * the weapons for war. | ||||
| @@ -56,22 +54,22 @@ public class App { | ||||
|   public App(Blacksmith blacksmith) { | ||||
|     this.blacksmith = blacksmith; | ||||
|   } | ||||
|    | ||||
|  | ||||
|   /** | ||||
|    * Program entry point. | ||||
|    *  | ||||
|    * | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     // Lets go to war with Orc weapons | ||||
|     var app = new App(new OrcBlacksmith()); | ||||
|     app.manufactureWeapons(); | ||||
|      | ||||
|  | ||||
|     // Lets go to war with Elf weapons | ||||
|     app = new App(new ElfBlacksmith()); | ||||
|     app.manufactureWeapons(); | ||||
|   } | ||||
|    | ||||
|  | ||||
|   private void manufactureWeapons() { | ||||
|     var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); | ||||
|     LOGGER.info(weapon.toString()); | ||||
|   | ||||
| @@ -24,7 +24,7 @@ | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import java.util.Arrays; | ||||
| import java.util.HashMap; | ||||
| import java.util.EnumMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
| @@ -35,7 +35,7 @@ public class ElfBlacksmith implements Blacksmith { | ||||
|   private static final Map<WeaponType, ElfWeapon> ELFARSENAL; | ||||
|  | ||||
|   static { | ||||
|     ELFARSENAL = new HashMap<>(WeaponType.values().length); | ||||
|     ELFARSENAL = new EnumMap<>(WeaponType.class); | ||||
|     Arrays.stream(WeaponType.values()).forEach(type -> ELFARSENAL.put(type, new ElfWeapon(type))); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -23,24 +23,20 @@ | ||||
|  | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import lombok.Getter; | ||||
| import lombok.RequiredArgsConstructor; | ||||
|  | ||||
| /** | ||||
|  * ElfWeapon. | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Getter | ||||
| public class ElfWeapon implements Weapon { | ||||
|  | ||||
|   private final WeaponType weaponType; | ||||
|  | ||||
|   public ElfWeapon(WeaponType weaponType) { | ||||
|     this.weaponType = weaponType; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return "Elven " + weaponType; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WeaponType getWeaponType() { | ||||
|     return weaponType; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -24,7 +24,7 @@ | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import java.util.Arrays; | ||||
| import java.util.HashMap; | ||||
| import java.util.EnumMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
| @@ -35,10 +35,10 @@ public class OrcBlacksmith implements Blacksmith { | ||||
|   private static final Map<WeaponType, OrcWeapon> ORCARSENAL; | ||||
|  | ||||
|   static { | ||||
|     ORCARSENAL = new HashMap<>(WeaponType.values().length); | ||||
|     ORCARSENAL = new EnumMap<>(WeaponType.class); | ||||
|     Arrays.stream(WeaponType.values()).forEach(type -> ORCARSENAL.put(type, new OrcWeapon(type))); | ||||
|   } | ||||
|    | ||||
|  | ||||
|   @Override | ||||
|   public Weapon manufactureWeapon(WeaponType weaponType) { | ||||
|     return ORCARSENAL.get(weaponType); | ||||
|   | ||||
| @@ -23,24 +23,20 @@ | ||||
|  | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import lombok.Getter; | ||||
| import lombok.RequiredArgsConstructor; | ||||
|  | ||||
| /** | ||||
|  * OrcWeapon. | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Getter | ||||
| public class OrcWeapon implements Weapon { | ||||
|  | ||||
|   private final WeaponType weaponType; | ||||
|  | ||||
|   public OrcWeapon(WeaponType weaponType) { | ||||
|     this.weaponType = weaponType; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return "Orcish " + weaponType; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WeaponType getWeaponType() { | ||||
|     return weaponType; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -23,19 +23,21 @@ | ||||
|  | ||||
| package com.iluwatar.factory.method; | ||||
|  | ||||
| import lombok.RequiredArgsConstructor; | ||||
|  | ||||
| /** | ||||
|  * WeaponType enumeration. | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| public enum WeaponType { | ||||
|  | ||||
|   SHORT_SWORD("short sword"), SPEAR("spear"), AXE("axe"), UNDEFINED(""); | ||||
|   SHORT_SWORD("short sword"), | ||||
|   SPEAR("spear"), | ||||
|   AXE("axe"), | ||||
|   UNDEFINED(""); | ||||
|  | ||||
|   private final String title; | ||||
|  | ||||
|   WeaponType(String title) { | ||||
|     this.title = title; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return title; | ||||
|   | ||||
| @@ -40,14 +40,14 @@ import org.junit.jupiter.api.Test; | ||||
|  * implementation it is referring to. | ||||
|  * </p> | ||||
|  */ | ||||
| public class FactoryMethodTest { | ||||
| class FactoryMethodTest { | ||||
|  | ||||
|   /** | ||||
|    * Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance of | ||||
|    * {@link OrcWeapon}. | ||||
|    */ | ||||
|   @Test | ||||
|   public void testOrcBlacksmithWithSpear() { | ||||
|   void testOrcBlacksmithWithSpear() { | ||||
|     var blacksmith = new OrcBlacksmith(); | ||||
|     var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); | ||||
|     verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class); | ||||
| @@ -58,7 +58,7 @@ public class FactoryMethodTest { | ||||
|    * {@link OrcWeapon}. | ||||
|    */ | ||||
|   @Test | ||||
|   public void testOrcBlacksmithWithAxe() { | ||||
|   void testOrcBlacksmithWithAxe() { | ||||
|     var blacksmith = new OrcBlacksmith(); | ||||
|     var weapon = blacksmith.manufactureWeapon(WeaponType.AXE); | ||||
|     verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class); | ||||
| @@ -69,7 +69,7 @@ public class FactoryMethodTest { | ||||
|    * of {@link ElfWeapon}. | ||||
|    */ | ||||
|   @Test | ||||
|   public void testElfBlacksmithWithShortSword() { | ||||
|   void testElfBlacksmithWithShortSword() { | ||||
|     var blacksmith = new ElfBlacksmith(); | ||||
|     var weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); | ||||
|     verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class); | ||||
| @@ -80,7 +80,7 @@ public class FactoryMethodTest { | ||||
|    * {@link ElfWeapon}. | ||||
|    */ | ||||
|   @Test | ||||
|   public void testElfBlacksmithWithSpear() { | ||||
|   void testElfBlacksmithWithSpear() { | ||||
|     var blacksmith = new ElfBlacksmith(); | ||||
|     var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); | ||||
|     verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user