2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.Map.Entry;
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.NamespaceKeyCriterion;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage.GlobalStorage;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
25 * A {@link NamespaceAccess} backed by a {@link NamespaceBehaviour}. Also holds reference to {@link BuildGlobalContext}.
27 final class BehaviourNamespaceAccess<K, V> extends NamespaceAccess<K, V> {
28 private final @NonNull NamespaceBehaviour<K, V> behaviour;
29 private final @NonNull GlobalStorage globalStorage;
31 // FIXME: Change this to Multimap, once issue with modules is resolved.
32 private List<KeyedValueAddedListener<K>> listeners;
33 private List<PredicateValueAddedListener<K, V>> predicateListeners;
35 BehaviourNamespaceAccess(final GlobalStorage globalStorage, final NamespaceBehaviour<K, V> behaviour) {
36 this.globalStorage = requireNonNull(globalStorage);
37 this.behaviour = requireNonNull(behaviour);
41 ParserNamespace<K, V> namespace() {
42 return behaviour.namespace();
46 V valueFrom(final NamespaceStorage storage, final K key) {
47 return behaviour.getFrom(globalStorage, storage, key);
51 void valueTo(final NamespaceStorage storage, final K key, final V value) {
52 behaviour.addTo(globalStorage, storage, key, value);
54 if (listeners != null) {
55 final var toNotify = new ArrayList<KeyedValueAddedListener<K>>();
56 final var it = listeners.iterator();
57 while (it.hasNext()) {
58 final var listener = it.next();
59 if (listener.isRequestedValue(this, storage, value)) {
61 toNotify.add(listener);
64 for (var listener : toNotify) {
65 listener.onValueAdded(value);
68 if (listeners != null && listeners.isEmpty()) {
73 if (predicateListeners != null) {
74 final var it = predicateListeners.iterator();
75 while (it.hasNext()) {
76 if (it.next().onValueAdded(key, value)) {
80 if (predicateListeners != null && predicateListeners.isEmpty()) {
81 predicateListeners = null;
87 Map<K, V> allFrom(final NamespaceStorage storage) {
88 return behaviour.getAllFrom(globalStorage, storage);
92 Entry<K, V> entryFrom(final NamespaceStorage storage, final NamespaceKeyCriterion<K> criterion) {
93 return behaviour.getFrom(globalStorage, storage, criterion);
97 void addListener(final KeyedValueAddedListener<K> listener) {
98 if (listeners == null) {
99 listeners = new ArrayList<>();
101 listeners.add(listener);
105 void addListener(final PredicateValueAddedListener<K, V> listener) {
106 if (predicateListeners == null) {
107 predicateListeners = new ArrayList<>();
109 predicateListeners.add(listener);
113 public String toString() {
114 return MoreObjects.toStringHelper(this).add("behaviour", behaviour).toString();