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 static java.util.Objects.requireNonNull;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
20 abstract class NamespaceBehaviourWithListeners<K, V, N extends IdentifierNamespace<K, V>>
21 extends NamespaceBehaviour<K, V, N> {
23 abstract static class ValueAddedListener<K> {
24 private final NamespaceStorageNode ctxNode;
26 ValueAddedListener(final NamespaceStorageNode contextNode) {
27 this.ctxNode = requireNonNull(contextNode);
30 final NamespaceStorageNode getCtxNode() {
35 abstract static class KeyedValueAddedListener<K> extends ValueAddedListener<K> {
38 KeyedValueAddedListener(final NamespaceStorageNode contextNode, final K key) {
40 this.key = requireNonNull(key);
47 final <V> boolean isRequestedValue(final NamespaceBehaviour<K, ? , ?> behavior,
48 final NamespaceStorageNode storage, final V value) {
49 return value == behavior.getFrom(getCtxNode(), key);
52 abstract void onValueAdded(Object value);
55 abstract static class PredicateValueAddedListener<K, V> extends ValueAddedListener<K> {
56 PredicateValueAddedListener(final NamespaceStorageNode contextNode) {
60 abstract boolean onValueAdded(@Nonnull K key, @Nonnull V value);
63 protected final NamespaceBehaviour<K, V, N> delegate;
65 private List<VirtualNamespaceContext<?, V, ?, K>> derivedNamespaces;
67 protected NamespaceBehaviourWithListeners(final NamespaceBehaviour<K, V, N> delegate) {
68 super(delegate.getIdentifier());
69 this.delegate = delegate;
72 abstract void addListener(KeyedValueAddedListener<K> listener);
74 abstract void addListener(PredicateValueAddedListener<K, V> listener);
77 public abstract void addTo(NamespaceStorageNode storage, K key, V value);
79 protected void notifyListeners(final NamespaceStorageNode storage,
80 final Iterator<? extends KeyedValueAddedListener<K>> keyListeners, final V value) {
81 List<KeyedValueAddedListener<K>> toNotify = new ArrayList<>();
82 while (keyListeners.hasNext()) {
83 final KeyedValueAddedListener<K> listener = keyListeners.next();
84 if (listener.isRequestedValue(this, storage, value)) {
85 keyListeners.remove();
86 toNotify.add(listener);
89 for (KeyedValueAddedListener<K> listener : toNotify) {
90 listener.onValueAdded(value);
94 protected void notifyDerivedNamespaces(final NamespaceStorageNode storage, final K key, final V value) {
95 if (derivedNamespaces != null) {
96 for (VirtualNamespaceContext<?, V, ?, K> derived : derivedNamespaces) {
97 derived.addedToSourceNamespace(storage, key, value);
102 final void addDerivedNamespace(final VirtualNamespaceContext<?, V, ?, K> namespace) {
103 if (derivedNamespaces == null) {
104 derivedNamespaces = new ArrayList<>();
106 derivedNamespaces.add(namespace);
110 public V getFrom(final NamespaceStorageNode storage, final K key) {
111 return delegate.getFrom(storage, key);
115 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
116 return delegate.getAllFrom(storage);