From: <th...@us...> - 2009-08-21 21:52:07
|
Revision: 10269 http://pcgen.svn.sourceforge.net/pcgen/?rev=10269&view=rev Author: thpr Date: 2009-08-21 21:51:58 +0000 (Fri, 21 Aug 2009) Log Message: ----------- Make ITEM and PROHIBITED_ITEM type safe Modified Paths: -------------- Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/ListKey.java Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/Type.java Trunk/pcgen/code/src/java/pcgen/core/Equipment.java Trunk/pcgen/code/src/java/pcgen/core/spell/Spell.java Trunk/pcgen/code/src/java/pcgen/gui/ChooseSpellDialog.java Trunk/pcgen/code/src/java/pcgen/gui/EqBuilder.java Trunk/pcgen/code/src/java/pcgen/gui/editor/SpellBasePanel.java Trunk/pcgen/code/src/java/plugin/lsttokens/spell/ItemToken.java Trunk/pcgen/code/src/utest/plugin/lsttokens/spell/ItemTokenTest.java Modified: Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/ListKey.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/ListKey.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/ListKey.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -177,8 +177,8 @@ public static final ListKey<String> SPELL_SCHOOL = new ListKey<String>(); public static final ListKey<String> SPELL_SUBSCHOOL = new ListKey<String>(); public static final ListKey<String> SPELL_DESCRIPTOR = new ListKey<String>(); - public static final ListKey<String> PROHIBITED_ITEM = new ListKey<String>(); - public static final ListKey<String> ITEM = new ListKey<String>(); + public static final ListKey<Type> PROHIBITED_ITEM = new ListKey<Type>(); + public static final ListKey<Type> ITEM = new ListKey<Type>(); public static final ListKey<Integer> HITDICE_ADVANCEMENT = new ListKey<Integer>(); public static final ListKey<String> ITEM_TYPES = new ListKey<String>(); public static final ListKey<CDOMSingleRef<EquipmentModifier>> REPLACED_KEYS = new ListKey<CDOMSingleRef<EquipmentModifier>>(); Modified: Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/Type.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/Type.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/cdom/enumeration/Type.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -80,6 +80,14 @@ public static final Type DIVINE = getConstant("Divine"); + public static final Type POTION = getConstant("Potion"); + + public static final Type RING = getConstant("Ring"); + + public static final Type SCROLL = getConstant("Scroll"); + + public static final Type WAND = getConstant("Wand"); + /** * This is used to provide a unique ordinal to each constant in this class */ Modified: Trunk/pcgen/code/src/java/pcgen/core/Equipment.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/core/Equipment.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/core/Equipment.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -449,6 +449,7 @@ * * @return The type */ + @Override public String getType() { return getType(true); } Modified: Trunk/pcgen/code/src/java/pcgen/core/spell/Spell.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/core/spell/Spell.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/core/spell/Spell.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -25,6 +25,7 @@ import pcgen.base.lang.StringUtil; import pcgen.cdom.base.CDOMReference; import pcgen.cdom.enumeration.ListKey; +import pcgen.cdom.enumeration.Type; import pcgen.cdom.list.SpellList; import pcgen.cdom.reference.CDOMDirectSingleRef; import pcgen.core.Globals; @@ -115,31 +116,11 @@ return hasSpellPointCost; } - public boolean isAllowed(String string) + public boolean isAllowed(Type t) { - /* - * Due to case insensitivity and lack of type safety so far on ITEM & - * PROHIBITED_ITEM we need this method in order to properly calculate - * what is allowed - */ - for (String s : getSafeListFor(ListKey.ITEM)) - { - if (s.equalsIgnoreCase(string)) - { - return true; - } - } - if ("potion".equalsIgnoreCase(string)) - { - return false; - } - for (String s : getSafeListFor(ListKey.PROHIBITED_ITEM)) - { - if (s.equalsIgnoreCase(string)) - { - return false; - } - } - return true; + boolean allowed = containsInList(ListKey.ITEM, t); + boolean prohibited = Type.POTION.equals(t) + || containsInList(ListKey.PROHIBITED_ITEM, t); + return allowed || !prohibited; } } Modified: Trunk/pcgen/code/src/java/pcgen/gui/ChooseSpellDialog.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/gui/ChooseSpellDialog.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/gui/ChooseSpellDialog.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -61,6 +61,7 @@ import pcgen.cdom.enumeration.ListKey; import pcgen.cdom.enumeration.ObjectKey; import pcgen.cdom.enumeration.SourceFormat; +import pcgen.cdom.enumeration.Type; import pcgen.cdom.list.ClassSpellList; import pcgen.cdom.list.DomainSpellList; import pcgen.core.Ability; @@ -453,7 +454,7 @@ @SuppressWarnings("fallthrough") private boolean canCreateItem(Spell aSpell) { - String itemType; + Type itemType; switch (eqType) { Modified: Trunk/pcgen/code/src/java/pcgen/gui/EqBuilder.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/gui/EqBuilder.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/gui/EqBuilder.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -120,7 +120,7 @@ static final long serialVersionUID = -369105812700996734L; private static TableSorter sorter; /** The types of equipment that are valid for creation based on a spell. */ - static String[] validEqTypes = { "potion", "scroll", "wand", "ring" }; + static Type[] validEqTypes = { Type.POTION, Type.SCROLL, Type.WAND, Type.RING }; private DefaultListModel listModel1; private DefaultListModel listModel2; private Equipment aNewEq = null; @@ -280,7 +280,7 @@ for (int idx = 0; idx < validEqTypes.length; ++idx) { - if (aEq.isType(validEqTypes[idx])) + if (aEq.isType(validEqTypes[idx].toString())) { eqType = idx; Modified: Trunk/pcgen/code/src/java/pcgen/gui/editor/SpellBasePanel.java =================================================================== --- Trunk/pcgen/code/src/java/pcgen/gui/editor/SpellBasePanel.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/pcgen/gui/editor/SpellBasePanel.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -53,6 +53,7 @@ import pcgen.cdom.enumeration.ListKey; import pcgen.cdom.enumeration.ObjectKey; import pcgen.cdom.enumeration.StringKey; +import pcgen.cdom.enumeration.Type; import pcgen.core.Description; import pcgen.core.Globals; import pcgen.core.PCStat; @@ -188,22 +189,22 @@ s.removeListFor(ListKey.PROHIBITED_ITEM); if (chkPotionAllowed.isSelected()) { - s.addToListFor(ListKey.ITEM, "potion"); + s.addToListFor(ListKey.ITEM, Type.POTION); } if (!chkRingAllowed.isSelected()) { - s.addToListFor(ListKey.PROHIBITED_ITEM, "ring"); + s.addToListFor(ListKey.PROHIBITED_ITEM, Type.RING); } if (!chkScrollAllowed.isSelected()) { - s.addToListFor(ListKey.PROHIBITED_ITEM, "scroll"); + s.addToListFor(ListKey.PROHIBITED_ITEM, Type.SCROLL); } if (!chkWandAllowed.isSelected()) { - s.addToListFor(ListKey.PROHIBITED_ITEM, "wand"); + s.addToListFor(ListKey.PROHIBITED_ITEM, Type.WAND); } } @@ -243,10 +244,10 @@ txtCost.setValue(thisSpell.getSafe(ObjectKey.COST).doubleValue()); txtXpCost.setValue(thisSpell.getSafe(IntegerKey.XP_COST)); - chkPotionAllowed.setSelected(thisSpell.isAllowed("potion")); - chkRingAllowed.setSelected(thisSpell.isAllowed( "ring")); - chkScrollAllowed.setSelected(thisSpell.isAllowed("scroll")); - chkWandAllowed.setSelected(thisSpell.isAllowed("wand")); + chkPotionAllowed.setSelected(thisSpell.isAllowed(Type.POTION)); + chkRingAllowed.setSelected(thisSpell.isAllowed(Type.RING)); + chkScrollAllowed.setSelected(thisSpell.isAllowed(Type.SCROLL)); + chkWandAllowed.setSelected(thisSpell.isAllowed(Type.WAND)); } private void initComponentContents() Modified: Trunk/pcgen/code/src/java/plugin/lsttokens/spell/ItemToken.java =================================================================== --- Trunk/pcgen/code/src/java/plugin/lsttokens/spell/ItemToken.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/java/plugin/lsttokens/spell/ItemToken.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -22,6 +22,7 @@ import pcgen.cdom.base.Constants; import pcgen.cdom.enumeration.ListKey; +import pcgen.cdom.enumeration.Type; import pcgen.core.spell.Spell; import pcgen.rules.context.Changes; import pcgen.rules.context.LoadContext; @@ -73,7 +74,7 @@ return false; } context.getObjectContext().addToList(spell, - ListKey.PROHIBITED_ITEM, substring); + ListKey.PROHIBITED_ITEM, Type.getConstant(substring)); } else { @@ -85,7 +86,7 @@ return false; } context.getObjectContext().addToList(spell, ListKey.ITEM, - tokString); + Type.getConstant(tokString)); } } return true; @@ -93,17 +94,17 @@ public String[] unparse(LoadContext context, Spell spell) { - Changes<String> changes = context.getObjectContext().getListChanges( + Changes<Type> changes = context.getObjectContext().getListChanges( spell, ListKey.ITEM); - Changes<String> proChanges = context.getObjectContext().getListChanges( + Changes<Type> proChanges = context.getObjectContext().getListChanges( spell, ListKey.PROHIBITED_ITEM); - Collection<String> changeAdded = changes.getAdded(); - Collection<String> proAdded = proChanges.getAdded(); + Collection<Type> changeAdded = changes.getAdded(); + Collection<Type> proAdded = proChanges.getAdded(); StringBuilder sb = new StringBuilder(); boolean needComma = false; if (changeAdded != null) { - for (String t : changeAdded) + for (Type t : changeAdded) { if (needComma) { @@ -115,7 +116,7 @@ } if (proAdded != null) { - for (String t : proAdded) + for (Type t : proAdded) { if (needComma) { Modified: Trunk/pcgen/code/src/utest/plugin/lsttokens/spell/ItemTokenTest.java =================================================================== --- Trunk/pcgen/code/src/utest/plugin/lsttokens/spell/ItemTokenTest.java 2009-08-21 21:51:07 UTC (rev 10268) +++ Trunk/pcgen/code/src/utest/plugin/lsttokens/spell/ItemTokenTest.java 2009-08-21 21:51:58 UTC (rev 10269) @@ -22,6 +22,7 @@ import org.junit.Test; import pcgen.cdom.enumeration.ListKey; +import pcgen.cdom.enumeration.Type; import pcgen.core.spell.Spell; import pcgen.persistence.PersistenceLayerException; import pcgen.rules.persistence.CDOMLoader; @@ -57,7 +58,7 @@ @Override public Object getConstant(String string) { - return string; + return Type.getConstant(string); } @Override This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |