refactoring: Issues #2326 and #2325 (#2327)

* Fix comment typo #2325

* Minor enchancements to flyweight #2326

* Minor enchancements to flyweight #2326
This commit is contained in:
Stefanel Stan
2022-11-20 15:14:13 +02:00
committed by GitHub
parent 3a446f8e9c
commit a05cf33643
3 changed files with 7 additions and 10 deletions
@@ -48,7 +48,7 @@ public class UserGroup {
*/ */
public static void addUserToFreeGroup(final User user) throws IllegalArgumentException { public static void addUserToFreeGroup(final User user) throws IllegalArgumentException {
if (paidGroup.contains(user)) { if (paidGroup.contains(user)) {
throw new IllegalArgumentException("User all ready member of paid group."); throw new IllegalArgumentException("User already member of paid group.");
} else { } else {
if (!freeGroup.contains(user)) { if (!freeGroup.contains(user)) {
freeGroup.add(user); freeGroup.add(user);
@@ -65,7 +65,7 @@ public class UserGroup {
*/ */
public static void addUserToPaidGroup(final User user) throws IllegalArgumentException { public static void addUserToPaidGroup(final User user) throws IllegalArgumentException {
if (freeGroup.contains(user)) { if (freeGroup.contains(user)) {
throw new IllegalArgumentException("User all ready member of free group."); throw new IllegalArgumentException("User already member of free group.");
} else { } else {
if (!paidGroup.contains(user)) { if (!paidGroup.contains(user)) {
paidGroup.add(user); paidGroup.add(user);
@@ -46,27 +46,25 @@ public class PotionFactory {
switch (type) { switch (type) {
case HEALING: case HEALING:
potion = new HealingPotion(); potion = new HealingPotion();
potions.put(type, potion);
break; break;
case HOLY_WATER: case HOLY_WATER:
potion = new HolyWaterPotion(); potion = new HolyWaterPotion();
potions.put(type, potion);
break; break;
case INVISIBILITY: case INVISIBILITY:
potion = new InvisibilityPotion(); potion = new InvisibilityPotion();
potions.put(type, potion);
break; break;
case POISON: case POISON:
potion = new PoisonPotion(); potion = new PoisonPotion();
potions.put(type, potion);
break; break;
case STRENGTH: case STRENGTH:
potion = new StrengthPotion(); potion = new StrengthPotion();
potions.put(type, potion);
break; break;
default: default:
break; break;
} }
if (potion != null) {
potions.put(type, potion);
}
} }
return potion; return potion;
} }
@@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@@ -55,8 +56,6 @@ class AlchemistShopTest {
// There are 13 potion instances, but only 5 unique instance types // There are 13 potion instances, but only 5 unique instance types
assertEquals(13, allPotions.size()); assertEquals(13, allPotions.size());
assertEquals(5, allPotions.stream().map(System::identityHashCode).distinct().count()); assertEquals(5, new HashSet<>(allPotions).size());
} }
} }