CardType: use stringTo Maps as Cache again

This commit is contained in:
Hans Mackowiak
2020-05-29 09:44:59 +02:00
parent 3d32915d9f
commit 57b4135f95

View File

@@ -72,6 +72,14 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
private static Map<String, CoreType> stringToCoreType = EnumUtils.getEnumMap(CoreType.class); private static Map<String, CoreType> stringToCoreType = EnumUtils.getEnumMap(CoreType.class);
private static final Set<String> allCoreTypeNames = stringToCoreType.keySet(); private static final Set<String> allCoreTypeNames = stringToCoreType.keySet();
public static CoreType getEnum(String name) {
return stringToCoreType.get(name);
}
public static boolean isValidEnum(String name) {
return stringToCoreType.containsKey(name);
}
CoreType(final boolean permanent) { CoreType(final boolean permanent) {
isPermanent = permanent; isPermanent = permanent;
} }
@@ -87,6 +95,15 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
private static Map<String, Supertype> stringToSupertype = EnumUtils.getEnumMap(Supertype.class); private static Map<String, Supertype> stringToSupertype = EnumUtils.getEnumMap(Supertype.class);
private static final Set<String> allSuperTypeNames = stringToSupertype.keySet(); private static final Set<String> allSuperTypeNames = stringToSupertype.keySet();
public static Supertype getEnum(String name) {
return stringToSupertype.get(name);
}
public static boolean isValidEnum(String name) {
return stringToSupertype.containsKey(name);
}
} }
private final Set<CoreType> coreTypes = EnumSet.noneOf(CoreType.class); private final Set<CoreType> coreTypes = EnumSet.noneOf(CoreType.class);
@@ -108,12 +125,12 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
public boolean add(final String t) { public boolean add(final String t) {
boolean changed; boolean changed;
final CoreType ct = EnumUtils.getEnum(CoreType.class, t); final CoreType ct = CoreType.getEnum(t);
if (ct != null) { if (ct != null) {
changed = coreTypes.add(ct); changed = coreTypes.add(ct);
} }
else { else {
final Supertype st = EnumUtils.getEnum(Supertype.class, t); final Supertype st = Supertype.getEnum(t);
if (st != null) { if (st != null) {
changed = supertypes.add(st); changed = supertypes.add(st);
} }
@@ -183,11 +200,11 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
if (subtypes.remove(str)) { if (subtypes.remove(str)) {
changed = true; changed = true;
} else { } else {
Supertype st = EnumUtils.getEnum(Supertype.class, str); Supertype st = Supertype.getEnum(str);
if (st != null && supertypes.remove(st)) { if (st != null && supertypes.remove(st)) {
changed = true; changed = true;
} }
CoreType ct = EnumUtils.getEnum(CoreType.class, str); CoreType ct = CoreType.getEnum(str);
if (ct != null && coreTypes.remove(ct)) { if (ct != null && coreTypes.remove(ct)) {
changed = true; changed = true;
} }
@@ -265,11 +282,11 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
} }
t = StringUtils.capitalize(t); t = StringUtils.capitalize(t);
final CoreType type = EnumUtils.getEnum(CoreType.class, t); final CoreType type = CoreType.getEnum(t);
if (type != null) { if (type != null) {
return hasType(type); return hasType(type);
} }
final Supertype supertype = EnumUtils.getEnum(Supertype.class, t); final Supertype supertype = Supertype.getEnum(t);
if (supertype != null) { if (supertype != null) {
return hasSupertype(supertype); return hasSupertype(supertype);
} }
@@ -660,7 +677,7 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
///////// Utility methods ///////// Utility methods
public static boolean isACardType(final String cardType) { public static boolean isACardType(final String cardType) {
return EnumUtils.isValidEnum(CoreType.class, cardType); return CoreType.isValidEnum(cardType);
} }
public static Set<String> getAllCardTypes() { public static Set<String> getAllCardTypes() {
@@ -708,7 +725,7 @@ public final class CardType implements Comparable<CardType>, CardTypeView {
} }
public static boolean isASupertype(final String cardType) { public static boolean isASupertype(final String cardType) {
return EnumUtils.isValidEnum(Supertype.class, cardType); return Supertype.isValidEnum(cardType);
} }
public static boolean isASubType(final String cardType) { public static boolean isASubType(final String cardType) {