Search in sources :

Example 1 with CaseInsensitiveMap

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

the class FollowerOptionFacet method getAvailableFollowers.

/**
	 * Returns a non-null copy of the available FollowerOptions of a given type
	 * for the Player Character represented by the given CharID. This method
	 * returns an empty Map if no objects are in this FollowerOptionFacet for
	 * the Player Character identified by the given CharID.
	 * 
	 * This method is value-semantic in that ownership of the returned Map is
	 * transferred to the class calling this method. Modification of the
	 * returned Map will not modify this FollowerOptionFacet and modification of
	 * this FollowerOptionFacet will not modify the returned Map. Modifications
	 * to the returned Set will also not modify any future or previous objects
	 * returned by this (or other) methods on FollowerOptionFacet. If you wish
	 * to modify the information stored in this FollowerOptionFacet, you must
	 * use the add*() and remove*() methods of FollowerOptionFacet.
	 * 
	 * @param id
	 *            The CharID representing the Player Character for which the
	 *            items in this FollowerOptionFacet should be returned
	 * @param type
	 *            The type of FollowerOption that should be returned
	 * @param comp
	 *            An optional Comparator to be used to sort the FollowerOption
	 *            objects in the returned Map. null is a legal value, and will
	 *            result in the FollowerOptions being sorted by their type
	 * @return A non-null copy of the Map of FollowerOptions in this
	 *         FollowerOptionFacet for the Player Character represented by the
	 *         given CharID
	 */
public Map<FollowerOption, CDOMObject> getAvailableFollowers(CharID id, String type, Comparator<FollowerOption> comp) {
    CaseInsensitiveMap<Map<FollowerOption, Set<CDOMObject>>> componentMap = getCachedMap(id);
    if (componentMap == null) {
        return Collections.emptyMap();
    }
    Map<FollowerOption, Set<CDOMObject>> foMap = componentMap.get(type);
    if (foMap == null) {
        return Collections.emptyMap();
    }
    Map<FollowerOption, CDOMObject> ret = new TreeMap<>(comp);
    for (Map.Entry<FollowerOption, Set<CDOMObject>> me : foMap.entrySet()) {
        FollowerOption fo = me.getKey();
        Set<CDOMObject> target = me.getValue();
        Collection<FollowerOption> expanded = fo.getExpandedOptions();
        for (CDOMObject source : target) {
            for (FollowerOption efo : expanded) {
                /*
					 * TODO This is a bug, and will overwrite the first source
					 * :(
					 */
                ret.put(efo, source);
            }
        }
    }
    return ret;
}
Also used : Set(java.util.Set) WrappedMapSet(pcgen.base.util.WrappedMapSet) FollowerOption(pcgen.core.FollowerOption) CDOMObject(pcgen.cdom.base.CDOMObject) TreeMap(java.util.TreeMap) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) CaseInsensitiveMap(pcgen.base.util.CaseInsensitiveMap)

Example 2 with CaseInsensitiveMap

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

the class DamageReductionFacet method getDRMap.

private CaseInsensitiveMap<Integer> getDRMap(CharID id, Map<DamageReduction, Set<Object>> componentMap) {
    CaseInsensitiveMap<Integer> andMap = new CaseInsensitiveMap<>();
    if (componentMap == null || componentMap.isEmpty()) {
        return andMap;
    }
    CaseInsensitiveMap<Integer> orMap = new CaseInsensitiveMap<>();
    for (Map.Entry<DamageReduction, Set<Object>> me : componentMap.entrySet()) {
        DamageReduction dr = me.getKey();
        for (Object source : me.getValue()) {
            if (prerequisiteFacet.qualifies(id, dr, source)) {
                String sourceString = (source instanceof CDOMObject) ? ((CDOMObject) source).getQualifiedKey() : "";
                int rawDrValue = formulaResolvingFacet.resolve(id, dr.getReduction(), sourceString).intValue();
                String bypass = dr.getBypass();
                if (OR_PATTERN.matcher(bypass).find()) {
                    Integer current = orMap.get(bypass);
                    if ((current == null) || (current.intValue() < rawDrValue)) {
                        orMap.put(dr.getBypass(), rawDrValue);
                    }
                } else {
                    /*
						 * TODO Shouldn't this expansion be done in the DR
						 * token? (since it's static?)
						 */
                    String[] splits = AND_PATTERN.split(bypass);
                    if (splits.length == 1) {
                        Integer current = andMap.get(dr.getBypass());
                        if ((current == null) || (current.intValue() < rawDrValue)) {
                            andMap.put(dr.getBypass(), rawDrValue);
                        }
                    } else {
                        for (String split : splits) {
                            Integer current = andMap.get(split);
                            if ((current == null) || (current.intValue() < rawDrValue)) {
                                andMap.put(split, rawDrValue);
                            }
                        }
                    }
                }
            }
        }
    }
    // e.g. 10/magic or good or lawful + 5/good = 10/good; 5/magic or good
    for (Map.Entry<Object, Integer> me : orMap.entrySet()) {
        String origBypass = me.getKey().toString();
        Integer reduction = me.getValue();
        String[] orValues = OR_PATTERN.split(origBypass);
        boolean shouldAdd = true;
        for (String orValue : orValues) {
            // See if we already have a value for this type from the 'and'
            // processing.
            Integer andDR = andMap.get(orValue);
            if (andDR != null && andDR >= reduction) {
                shouldAdd = false;
                break;
            }
        }
        if (shouldAdd) {
            andMap.put(origBypass, reduction);
        }
    }
    return andMap;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) DamageReduction(pcgen.cdom.content.DamageReduction) CaseInsensitiveMap(pcgen.base.util.CaseInsensitiveMap) CDOMObject(pcgen.cdom.base.CDOMObject) CDOMObject(pcgen.cdom.base.CDOMObject) Map(java.util.Map) CaseInsensitiveMap(pcgen.base.util.CaseInsensitiveMap)

Aggregations

Map (java.util.Map)2 Set (java.util.Set)2 CaseInsensitiveMap (pcgen.base.util.CaseInsensitiveMap)2 CDOMObject (pcgen.cdom.base.CDOMObject)2 IdentityHashMap (java.util.IdentityHashMap)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 WrappedMapSet (pcgen.base.util.WrappedMapSet)1 DamageReduction (pcgen.cdom.content.DamageReduction)1 FollowerOption (pcgen.core.FollowerOption)1