BUG-4688: Add flexible match support to NamespaceStorageSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SimpleNamespaceContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
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;
16
17 final class SimpleNamespaceContext<K, V, N extends IdentifierNamespace<K, V>>
18         extends NamespaceBehaviourWithListeners<K, V, N> {
19
20     // FIXME: Change this to Multimap, once issue with modules is resolved.
21     private final List<KeyedValueAddedListener<K>> listeners = new ArrayList<>();
22
23     private final Collection<PredicateValueAddedListener<K, V>> predicateListeners = new ArrayList<>();
24
25     SimpleNamespaceContext(final NamespaceBehaviour<K, V, N> delegate) {
26         super(delegate);
27     }
28
29     @Override
30     void addListener(final KeyedValueAddedListener<K> listener) {
31         listeners.add(listener);
32     }
33
34     @Override
35     void addListener(final PredicateValueAddedListener<K, V> listener) {
36         predicateListeners.add(listener);
37     }
38
39     @Override
40     public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
41         delegate.addTo(storage, key, value);
42         notifyListeners(storage, listeners.iterator(), value);
43
44         final Iterator<PredicateValueAddedListener<K, V>> it = predicateListeners.iterator();
45         while (it.hasNext()) {
46             if (it.next().onValueAdded(key, value)) {
47                 it.remove();
48             }
49         }
50
51         notifyDerivedNamespaces(storage, key, value);
52     }
53 }