{
ArgumentChecks.ensureNonNull("authority", authority);
String old = null;
final Iterator<? extends Identifier> it = identifiers.iterator();
while (it.hasNext()) {
final Identifier identifier = it.next();
if (identifier == null) {
it.remove(); // Opportunist cleaning, but should not happen.
} else if (Objects.equals(authority, identifier.getAuthority())) {
if (code != null && identifier instanceof IdentifierMapEntry) {
return ((IdentifierMapEntry) identifier).setValue(code);
// No need to suppress other occurrences of the key (if any)
// because we made a replacement in the first entry, so the
// new value will be visible by the getter methods.
}
if (old == null) {
old = identifier.getCode();
}
it.remove();
// Continue the iteration in order to remove all other occurrences,
// in order to ensure that the getter methods will see the new value.
}