final <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type,
final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
final OnNamespaceItemAdded listener) {
final Optional<Entry<K, V>> existing = getFromNamespace(type, criterion);
if (existing.isPresent()) {
final Entry<K, V> entry = existing.get();
LOG.debug("Listener on {} criterion {} found a pre-existing match: {}", type, criterion, entry);
waitForPhase(entry.getValue(), type, phase, criterion, listener);
return;
}
final NamespaceBehaviourWithListeners<K, V, N> behaviour = getBehaviour(type);
behaviour.addListener(new PredicateValueAddedListener<K, V>(this) {
@Override
boolean onValueAdded(final K key, final V value) {
if (criterion.match(key)) {
LOG.debug("Listener on {} criterion {} matched added key {}", type, criterion, key);
waitForPhase(value, type, phase, criterion, listener);
return true;
}
return false;
}
});
}