77693e8dc5583eceb251b3c761020994a965fc21
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / VirtualNamespaceContext.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 com.google.common.collect.HashMultimap;
11 import com.google.common.collect.Multimap;
12 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
14
15 final class VirtualNamespaceContext<K, V, N extends IdentifierNamespace<K, V>, D>
16         extends NamespaceBehaviourWithListeners<K, V, N> {
17
18     private final Multimap<D, ValueAddedListener<K>> listeners = HashMultimap.create();
19     private final DerivedNamespaceBehaviour<K, V, D, N, ?> derivedDelegate;
20
21     VirtualNamespaceContext(final DerivedNamespaceBehaviour<K, V, D, N, ?> delegate) {
22         super(delegate);
23         this.derivedDelegate = delegate;
24     }
25
26     @Override
27     void addListener(final K key, final ValueAddedListener<K> listener) {
28         listeners.put(derivedDelegate.getSignificantKey(key), listener);
29     }
30
31     void addedToSourceNamespace(final NamespaceStorageNode storage, final D key, final V value) {
32         notifyListeners(storage, listeners.get(key).iterator(), value);
33     }
34
35     @Override
36     public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
37         delegate.addTo(storage, key, value);
38         notifyListeners(storage, listeners.get(derivedDelegate.getSignificantKey(key)).iterator(), value);
39         notifyDerivedNamespaces(storage, key, value);
40     }
41 }