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 com.google.common.base.MoreObjects.ToStringHelper;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
21 abstract class NamespaceBehaviourWithListeners<K, V, N extends ParserNamespace<K, V>>
22 extends NamespaceBehaviour<K, V, N> {
24 abstract static class ValueAddedListener<K> {
25 private final NamespaceStorageNode ctxNode;
27 ValueAddedListener(final NamespaceStorageNode contextNode) {
28 this.ctxNode = requireNonNull(contextNode);
31 final NamespaceStorageNode getCtxNode() {
36 abstract static class KeyedValueAddedListener<K> extends ValueAddedListener<K> {
39 KeyedValueAddedListener(final NamespaceStorageNode contextNode, final K key) {
41 this.key = requireNonNull(key);
48 final <V> boolean isRequestedValue(final NamespaceBehaviour<K, ? , ?> behavior,
49 final NamespaceStorageNode storage, final V value) {
50 return value == behavior.getFrom(getCtxNode(), key);
53 abstract void onValueAdded(Object value);
56 abstract static class PredicateValueAddedListener<K, V> extends ValueAddedListener<K> {
57 PredicateValueAddedListener(final NamespaceStorageNode contextNode) {
61 abstract boolean onValueAdded(@NonNull K key, @NonNull V value);
64 protected final NamespaceBehaviour<K, V, N> delegate;
66 private List<VirtualNamespaceContext<?, V, ?, K>> derivedNamespaces;
68 protected NamespaceBehaviourWithListeners(final NamespaceBehaviour<K, V, N> delegate) {
69 super(delegate.getIdentifier());
70 this.delegate = delegate;
73 abstract void addListener(KeyedValueAddedListener<K> listener);
75 abstract void addListener(PredicateValueAddedListener<K, V> listener);
78 public abstract void addTo(NamespaceStorageNode storage, K key, V value);
80 protected void notifyListeners(final NamespaceStorageNode storage,
81 final Iterator<? extends KeyedValueAddedListener<K>> keyListeners, final V value) {
82 List<KeyedValueAddedListener<K>> toNotify = new ArrayList<>();
83 while (keyListeners.hasNext()) {
84 final KeyedValueAddedListener<K> listener = keyListeners.next();
85 if (listener.isRequestedValue(this, storage, value)) {
86 keyListeners.remove();
87 toNotify.add(listener);
90 for (KeyedValueAddedListener<K> listener : toNotify) {
91 listener.onValueAdded(value);
95 protected void notifyDerivedNamespaces(final NamespaceStorageNode storage, final K key, final V value) {
96 if (derivedNamespaces != null) {
97 for (VirtualNamespaceContext<?, V, ?, K> derived : derivedNamespaces) {
98 derived.addedToSourceNamespace(storage, key, value);
103 final void addDerivedNamespace(final VirtualNamespaceContext<?, V, ?, K> namespace) {
104 if (derivedNamespaces == null) {
105 derivedNamespaces = new ArrayList<>();
107 derivedNamespaces.add(namespace);
111 public V getFrom(final NamespaceStorageNode storage, final K key) {
112 return delegate.getFrom(storage, key);
116 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
117 return delegate.getAllFrom(storage);
121 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
122 return super.addToStringAttributes(helper).add("delegate", delegate);