// note: the size of the sets are typically really small (usually between 1 and 3),
// so iterating through them is fast.
// look at all the configs to ensure we detect any changes to individual settings on the templates
Set<String> existingNames = new HashSet<String>(existingPluginDriftTemplates.size());
DriftDefinitionComparator dirComp = new DriftDefinitionComparator(CompareMode.ONLY_DIRECTORY_SPECIFICATIONS);
for (Iterator<DriftDefinitionTemplate> i = existingDriftTemplates.iterator(); i.hasNext();) {
DriftDefinitionTemplate existingTemplate = i.next();
String existingName = existingTemplate.getName();
DriftDefinition existingDef = existingTemplate.getTemplateDefinition();
Set<DriftDefinition> attachedDefs = existingTemplate.getDriftDefinitions();
boolean noAttachedDefs = (null == attachedDefs || attachedDefs.isEmpty());
boolean notPinned = !existingTemplate.isPinned();
boolean stillDefined = false;
// for later to determine if any existing templates are no longer defined in the plugin
existingNames.add(existingName);
for (DriftDefinitionTemplate newTemplate : newPluginDriftTemplates) {
String newName = newTemplate.getName();
// The new template existed previously. See if it has changed and if so, in what way:
//
// IF the existingTemplate
// has no attached defs AND
// is not pinned
// THEN we can update it with impunity
// ELSE IF the directories have not changed
// THEN we can update the base info fields only
// Note that in the latter case we update the template but we will not push the
// changes down to attached defs. This is a little odd because the template and defs can
// get out of sync, but we don't want a plugin change to affect existing defs in case
// the user has made manual changes, or wants it the way it is.
if (newName.equals(existingName)) {
stillDefined = true;
DriftDefinition newDef = newTemplate.getTemplateDefinition();
boolean noDirChanges = (0 == dirComp.compare(existingDef, newDef));
if ((noAttachedDefs && notPinned) || noDirChanges) {
existingTemplate.setTemplateDefinition(newDef);
} else {