Search in sources :

Example 1 with SortKeyRequired

use of pcgen.cdom.base.SortKeyRequired in project pcgen by PCGen.

the class SortKeyLst method process.

/**
	 * Enforces that SORTKEY exists on any object which carries the
	 * SortKeyRequired interface.
	 * 
	 * All such objects must have a SORTKEY and in PCGen 6.5/6.6, the file order
	 * must match the SORTKEY order.
	 * 
	 * @see pcgen.rules.persistence.token.PostValidationToken#process(pcgen.rules.context.LoadContext,
	 *      java.util.Collection)
	 */
@Override
public boolean process(LoadContext context, Collection<? extends CDOMObject> allObjects) {
    if (allObjects.isEmpty()) {
        return true;
    }
    CDOMObject sample = allObjects.iterator().next();
    Class<? extends CDOMObject> cl = sample.getClass();
    //This Interface tag is placed on classes where SORTKEY is required
    boolean sortKeyRequired = sample instanceof SortKeyRequired;
    for (CDOMObject obj : allObjects) {
        String sortkey = obj.get(stringKey());
        if (sortkey == null) {
            /*
				 * Do not join IFs, we want sortkey == null and not required to
				 * not process the map
				 */
            if (sortKeyRequired) {
                //This becomes an error in PCGen 6.7
                Logging.deprecationPrint("Objects of type " + obj.getClass().getName() + " will require a SORTKEY " + "in the next version of PCGen (6.7).  " + "Use without a SORTKEY is deprecated", context);
            }
        }
    }
    /*
		 * This is likely permanent, as certain objects (e.g. Alignment/Stat)
		 * will "always" need a sort unique from the order in the file, and this
		 * is a good nudge to indicate to data writers that the items are sort
		 * order sensitive.
		 */
    if (!sortKeyRequired) {
        //Break out now if these aren't SortKeyRequired objects
        return true;
    }
    /*
		 * Per the transition rules, the sort key must match the existing order
		 * in the files (PCGen 6.5/6.6)
		 */
    AbstractReferenceContext refContext = context.getReferenceContext();
    List<? extends CDOMObject> sortKeySort = new ArrayList<>(refContext.getSortOrderedList(cl));
    List<? extends CDOMObject> orderSort = refContext.getOrderSortedCDOMObjects(cl);
    //This IF is order sensitive ... want to have ArrayList first to use its .equals()
    if (!sortKeySort.equals(orderSort)) {
        Logging.log(Logging.LST_ERROR, "For " + sample.getClass().getSimpleName() + ", the file order was: " + StringUtil.join(new ArrayList<CDOMObject>(orderSort), ", ") + " while the order based on SORTKEY was: " + StringUtil.join(sortKeySort, ", ") + ".  These lists must match.");
        return false;
    }
    return true;
}
Also used : SortKeyRequired(pcgen.cdom.base.SortKeyRequired) AbstractReferenceContext(pcgen.rules.context.AbstractReferenceContext) CDOMObject(pcgen.cdom.base.CDOMObject) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 CDOMObject (pcgen.cdom.base.CDOMObject)1 SortKeyRequired (pcgen.cdom.base.SortKeyRequired)1 AbstractReferenceContext (pcgen.rules.context.AbstractReferenceContext)1