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.Iterator;
12 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 abstract class NamespaceBehaviourWithListeners<K, V, N extends IdentifierNamespace<K, V>>
18 extends NamespaceBehaviour<K, V, N> {
20 abstract static class ValueAddedListener<K> {
21 private final NamespaceStorageNode ctxNode;
24 public ValueAddedListener(final NamespaceStorageNode contextNode, final K key) {
25 this.ctxNode = contextNode;
29 public NamespaceStorageNode getCtxNode() {
37 void trigger(final Object value) {
38 onValueAdded(key, value);
41 abstract void onValueAdded(Object key, Object value);
44 protected final NamespaceBehaviour<K, V, N> delegate;
45 private final List<VirtualNamespaceContext<?, V, ?, K>> derivedNamespaces = new ArrayList<>();
48 protected NamespaceBehaviourWithListeners(final NamespaceBehaviour<K, V, N> delegate) {
49 super(delegate.getIdentifier());
50 this.delegate = delegate;
53 protected abstract void addListener(K key, ValueAddedListener<K> listener);
55 protected abstract boolean isRequestedValue(ValueAddedListener<K> listener, NamespaceStorageNode storage, V value);
58 public abstract void addTo(final NamespaceStorageNode storage, final K key, final V value);
60 protected void notifyListeners(final NamespaceStorageNode storage, final Iterator<ValueAddedListener<K>> keyListeners,
62 List<ValueAddedListener<K>> toNotify = new ArrayList<>();
63 while (keyListeners.hasNext()) {
64 ValueAddedListener<K> listener = keyListeners.next();
65 if (isRequestedValue(listener, storage, value)) {
66 keyListeners.remove();
67 toNotify.add(listener);
70 for (ValueAddedListener<K> listener : toNotify) {
71 listener.trigger(value);
75 protected void notifyDerivedNamespaces(final NamespaceStorageNode storage, final K key, final V value) {
76 for (VirtualNamespaceContext<?, V, ?, K> derived : derivedNamespaces) {
77 derived.addedToSourceNamespace(storage, key, value);
81 final void addValueListener(final ValueAddedListener<K> listener) {
82 addListener(listener.key, listener);
85 final void addDerivedNamespace(final VirtualNamespaceContext<?, V, ?, K> namespace) {
86 derivedNamespaces.add(namespace);
90 public V getFrom(final NamespaceStorageNode storage, final K key) {
91 return delegate.getFrom(storage, key);
95 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
96 return delegate.getAllFrom(storage);