Repository: iluwatar/java-design-patterns
Issue: Minor enchancements to flyweight
Labels: type: refactoring
epic: code quality

Minor enhancements to flyweight:
- AlchemistShopTest : line 59: use a Set<>(potions).size() than streaming
- PotionFactory - small refactoring to reduce code duplication by adding the potion after each siwtch case.

Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/feature-toggle/src/main/java/com/iluwatar/featuretoggle/user/UserGroup.java b/feature-toggle/src/main/java/com/iluwatar/featuretoggle/user/UserGroup.java
index 91b75f548655..d01c5735d19e 100644
--- a/feature-toggle/src/main/java/com/iluwatar/featuretoggle/user/UserGroup.java
+++ b/feature-toggle/src/main/java/com/iluwatar/featuretoggle/user/UserGroup.java
@@ -48,7 +48,7 @@ public class UserGroup {
    */
   public static void addUserToFreeGroup(final User user) throws IllegalArgumentException {
     if (paidGroup.contains(user)) {
-      throw new IllegalArgumentException("User all ready member of paid group.");
+      throw new IllegalArgumentException("User already member of paid group.");
     } else {
       if (!freeGroup.contains(user)) {
         freeGroup.add(user);
@@ -65,7 +65,7 @@ public static void addUserToFreeGroup(final User user) throws IllegalArgumentExc
    */
   public static void addUserToPaidGroup(final User user) throws IllegalArgumentException {
     if (freeGroup.contains(user)) {
-      throw new IllegalArgumentException("User all ready member of free group.");
+      throw new IllegalArgumentException("User already member of free group.");
     } else {
       if (!paidGroup.contains(user)) {
         paidGroup.add(user);
diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java b/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java
index a05d99798604..814131732d62 100644
--- a/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java
+++ b/flyweight/src/main/java/com/iluwatar/flyweight/PotionFactory.java
@@ -46,27 +46,25 @@ Potion createPotion(PotionType type) {
       switch (type) {
         case HEALING:
           potion = new HealingPotion();
-          potions.put(type, potion);
           break;
         case HOLY_WATER:
           potion = new HolyWaterPotion();
-          potions.put(type, potion);
           break;
         case INVISIBILITY:
           potion = new InvisibilityPotion();
-          potions.put(type, potion);
           break;
         case POISON:
           potion = new PoisonPotion();
-          potions.put(type, potion);
           break;
         case STRENGTH:
           potion = new StrengthPotion();
-          potions.put(type, potion);
           break;
         default:
           break;
       }
+      if (potion != null) {
+        potions.put(type, potion);
+      }
     }
     return potion;
   }


Test patch:
diff --git a/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java b/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java
index 0448554384d3..79a70611ea95 100644
--- a/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java
+++ b/flyweight/src/test/java/com/iluwatar/flyweight/AlchemistShopTest.java
@@ -28,6 +28,7 @@
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -55,8 +56,6 @@ void testShop() {
 
     // There are 13 potion instances, but only 5 unique instance types
     assertEquals(13, allPotions.size());
-    assertEquals(5, allPotions.stream().map(System::identityHashCode).distinct().count());
-
+    assertEquals(5, new HashSet<>(allPotions).size());
   }
-
 }