2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
17 final class SimpleNamespaceContext<K, V, N extends IdentifierNamespace<K, V>>
18 extends NamespaceBehaviourWithListeners<K, V, N> {
19 // FIXME: Change this to Multimap, once issue with modules is resolved.
20 private List<KeyedValueAddedListener<K>> listeners;
22 private Collection<PredicateValueAddedListener<K, V>> predicateListeners;
24 SimpleNamespaceContext(final NamespaceBehaviour<K, V, N> delegate) {
29 void addListener(final KeyedValueAddedListener<K> listener) {
30 if (listeners == null) {
31 listeners = new ArrayList<>();
33 listeners.add(listener);
37 void addListener(final PredicateValueAddedListener<K, V> listener) {
38 if (predicateListeners == null) {
39 predicateListeners = new ArrayList<>();
41 predicateListeners.add(listener);
45 public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
46 delegate.addTo(storage, key, value);
48 if (listeners != null) {
49 notifyListeners(storage, listeners.iterator(), value);
50 if (listeners != null && listeners.isEmpty()) {
55 if (predicateListeners != null) {
56 final Iterator<PredicateValueAddedListener<K, V>> it = predicateListeners.iterator();
57 while (it.hasNext()) {
58 if (it.next().onValueAdded(key, value)) {
62 if (predicateListeners != null && predicateListeners.isEmpty()) {
63 predicateListeners = null;
67 notifyDerivedNamespaces(storage, key, value);