}
}));
for (JackrabbitAccessControlEntry ace : subList) {
ACE existing = (ACE) ace;
PrivilegeBits existingBits = getPrivilegeBits(existing);
PrivilegeBits entryBits = getPrivilegeBits(entry);
if (entry.getRestrictions().equals(existing.getRestrictions())) {
if (isRedundantOrExtending(existing, entry)) {
if (existingBits.includes(entryBits)) {
return false;
} else {
// merge existing and new ace
existingBits.add(entryBits);
int index = entries.indexOf(existing);
entries.remove(existing);
entries.add(index, createACE(existing, existingBits));
return true;
}
}
// clean up redundant privileges defined by the existing entry
// and append the new entry at the end of the list.
PrivilegeBits updated = PrivilegeBits.getInstance(existingBits).diff(entryBits);
if (updated.isEmpty()) {
// remove the existing entry as the new entry covers all privileges
entries.remove(ace);
} else if (!updated.includes(existingBits)) {
// replace the existing entry having it's privileges adjusted
int index = entries.indexOf(existing);
entries.remove(ace);
entries.add(index, createACE(existing, updated));
} /* else: no collision that requires adjusting the existing entry.*/