Search in sources :

Example 1 with MapToList

use of pcgen.base.util.MapToList in project pcgen by PCGen.

the class AbilityLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Collection<CDOMReference<? extends CDOMList<?>>> changedLists = context.getListContext().getChangedLists(obj, AbilityList.class);
    Changes<ListKey<ChooseSelectionActor<?>>> actors = context.getObjectContext().getListChanges(obj, ListKey.GA_CAKEYS);
    Set<String> returnSet = new TreeSet<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> m = new TripleKeyMapToList<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> clear = new TripleKeyMapToList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> csa : listAdded) {
            if (csa.getSource().equals(getTokenName())) {
                try {
                    AbilitySelector as = (AbilitySelector) csa;
                    StringBuilder sb = new StringBuilder();
                    sb.append(as.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                    sb.append(as.getNature()).append(Constants.PIPE);
                    sb.append(as.getLstFormat());
                    returnSet.add(sb.toString());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                    return null;
                }
            }
        }
    }
    for (CDOMReference ref : changedLists) {
        AssociatedChanges<CDOMReference<Ability>> changes = context.getListContext().getChangesInList(getTokenName(), obj, ref);
        if (changes.includesGlobalClear()) {
            CDOMDirectSingleRef<AbilityList> dr = (CDOMDirectSingleRef<AbilityList>) ref;
            AbilityList al = dr.get();
            StringBuilder sb = new StringBuilder();
            sb.append(al.getCategory().getLSTformat(false)).append(Constants.PIPE);
            sb.append(al.getNature()).append(Constants.PIPE);
            sb.append(Constants.LST_DOT_CLEAR);
            returnSet.add(sb.toString());
        }
        MapToList<CDOMReference<Ability>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    m.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
        mtl = changes.getRemovedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    clear.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
    }
    for (Nature nature : m.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : m.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : m.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature);
                List<CDOMReference<Ability>> clearList = clear.removeListFor(nature, category, prereqs);
                if (clearList != null && !clearList.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(Constants.LST_DOT_CLEAR_DOT);
                    sb.append(ReferenceUtilities.joinLstFormat(clearList, Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                }
                sb.append(Constants.PIPE);
                sb.append(ReferenceUtilities.joinLstFormat(m.getListFor(nature, category, prereqs), Constants.PIPE));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    for (Nature nature : clear.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : clear.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : clear.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature).append(Constants.PIPE).append(Constants.LST_DOT_CLEAR_DOT);
                sb.append(ReferenceUtilities.joinLstFormat(clear.getListFor(nature, category, prereqs), Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    Collection<ListKey<ChooseSelectionActor<?>>> addedActors = actors.getAdded();
    if (addedActors != null) {
        for (ListKey<ChooseSelectionActor<?>> lk : addedActors) {
            Changes<ChooseSelectionActor<?>> cras = context.getObjectContext().getListChanges(obj, lk);
            for (ChooseSelectionActor<?> cra : cras.getAdded()) {
                if (getTokenName().equals(cra.getSource())) {
                    try {
                        AbilityTargetSelector ats = (AbilityTargetSelector) cra;
                        StringBuilder sb = new StringBuilder();
                        sb.append(ats.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                        sb.append(ats.getNature()).append(Constants.PIPE).append(cra.getLstFormat());
                        List<Prerequisite> prereqs = ats.getPrerequisiteList();
                        if (prereqs != null && !prereqs.isEmpty()) {
                            sb.append(Constants.PIPE);
                            sb.append(getPrerequisiteString(context, prereqs));
                        }
                        returnSet.add(sb.toString());
                    } catch (PersistenceLayerException e) {
                        context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                        return null;
                    }
                }
            }
        }
    }
    if (returnSet.isEmpty()) {
        return null;
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) AbilitySelector(pcgen.cdom.helper.AbilitySelector) ListKey(pcgen.cdom.enumeration.ListKey) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) List(java.util.List) MapToList(pcgen.base.util.MapToList) AbilityList(pcgen.cdom.list.AbilityList) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) AbilityTargetSelector(pcgen.cdom.helper.AbilityTargetSelector) CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) AbilityList(pcgen.cdom.list.AbilityList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AbilityCategory(pcgen.core.AbilityCategory) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with MapToList

use of pcgen.base.util.MapToList in project pcgen by PCGen.

the class AbilityToken method getRetString.

/**
	 * Calculate the token value (return string) for the ability token.
	 * 
	 * @param tokenSource
	 *            The text of the export token.
	 * @param pc
	 *            The character being exported.
	 * @param eh
	 *            The export handler.
	 * @param abilityIndex
	 *            The location of the ability in the list.
	 * @param aList
	 *            The list of abilities to get the ability from.
	 * @return The token value.
	 */
private String getRetString(String tokenSource, PlayerCharacter pc, ExportHandler eh, int abilityIndex, MapToList<Ability, CNAbility> aMapToList) {
    String retString = "";
    Ability aAbility;
    List<Ability> aList = new ArrayList<>(aMapToList.getKeySet());
    // If the ability index given is within a valid range
    if (abilityIndex >= 0 && abilityIndex < aList.size()) {
        aAbility = aList.get(abilityIndex);
        List<CNAbility> abilities = aMapToList.getListFor(aAbility);
        if (abilities.isEmpty()) {
            return "";
        }
        // Then tell the ExportHandler that there is no more processing needed
        if (abilityIndex == aList.size() - 1 && eh != null && eh.getExistsOnly()) {
            eh.setNoMoreItems(true);
        }
        if (tokenSource.endsWith(".DESC")) {
            retString = pc.getDescription(abilities);
        } else if (tokenSource.endsWith(".BENEFIT")) {
            retString = BenefitFormatting.getBenefits(pc, abilities);
        } else if (tokenSource.endsWith(".TYPE")) {
            retString = aAbility.getType().toUpperCase();
        } else if (tokenSource.endsWith(".ASSOCIATED")) {
            List<String> assocs = new ArrayList<>();
            for (CNAbility cna : abilities) {
                assocs.addAll(pc.getAssociationExportList(cna));
            }
            Collections.sort(assocs);
            retString = StringUtil.join(assocs, ",");
        } else if (tokenSource.contains(".ASSOCIATED.")) {
            final String key = tokenSource.substring(tokenSource.indexOf(".ASSOCIATED.") + 12);
            retString = getAssociationString(pc, abilities, key);
        } else if (tokenSource.endsWith(".ASSOCIATEDCOUNT")) {
            int count = 0;
            for (CNAbility cna : abilities) {
                count += pc.getDetailedAssociationCount(cna);
            }
            retString = Integer.toString(count);
        } else if (tokenSource.endsWith(".SOURCE")) {
            retString = SourceFormat.getFormattedString(aAbility, Globals.getSourceDisplay(), true);
        } else if (tokenSource.endsWith(".SOURCESHORT")) {
            retString = SourceFormat.formatShort(aAbility, 8);
        } else if (tokenSource.endsWith(".ASPECT")) {
            retString = getAspectString(pc, abilities);
        } else if (tokenSource.contains(".ASPECT.")) {
            final String key = tokenSource.substring(tokenSource.indexOf(".ASPECT.") + 8);
            retString = getAspectString(pc, abilities, key);
        } else if (tokenSource.endsWith(".ASPECTCOUNT")) {
            retString = Integer.toString(aAbility.getSafeSizeOfMapFor(MapKey.ASPECT));
        } else if (tokenSource.contains(".HASASPECT.")) {
            final String key = tokenSource.substring(tokenSource.indexOf(".HASASPECT.") + 11);
            retString = getHasAspectString(pc, aAbility, AspectName.getConstant(key));
        } else if (tokenSource.contains(".NAME")) {
            retString = aAbility.getDisplayName();
        } else if (tokenSource.contains(".KEY")) {
            retString = aAbility.getKeyName();
        } else {
            retString = QualifiedName.qualifiedName(pc, abilities);
        }
    } else // ExportHandler that there are no more items to process
    if (eh != null && eh.getExistsOnly()) {
        eh.setNoMoreItems(true);
    }
    return retString;
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) CNAbility(pcgen.cdom.content.CNAbility) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) GenericMapToList(pcgen.base.util.GenericMapToList) HashMapToList(pcgen.base.util.HashMapToList) List(java.util.List) MapToList(pcgen.base.util.MapToList)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 MapToList (pcgen.base.util.MapToList)2 Ability (pcgen.core.Ability)2 TreeSet (java.util.TreeSet)1 GenericMapToList (pcgen.base.util.GenericMapToList)1 HashMapToList (pcgen.base.util.HashMapToList)1 TripleKeyMapToList (pcgen.base.util.TripleKeyMapToList)1 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)1 CDOMList (pcgen.cdom.base.CDOMList)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)1 CNAbility (pcgen.cdom.content.CNAbility)1 ListKey (pcgen.cdom.enumeration.ListKey)1 Nature (pcgen.cdom.enumeration.Nature)1 AbilitySelector (pcgen.cdom.helper.AbilitySelector)1 AbilityTargetSelector (pcgen.cdom.helper.AbilityTargetSelector)1 AbilityList (pcgen.cdom.list.AbilityList)1 CDOMDirectSingleRef (pcgen.cdom.reference.CDOMDirectSingleRef)1 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)1