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 com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
27 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
30 public abstract NamespaceStorageNode getParentNamespaceStorage();
33 * Return the registry of a source context.
35 * @return registry of source context
37 public abstract Registry getBehaviourRegistry();
39 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
44 * Occurs when an item is added to model namespace.
46 * @throws SourceException instance of SourceException
48 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
54 public final <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
55 final KT key) throws NamespaceNotAvailableException {
56 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
59 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<Entry<K, V>> getFromNamespace(
60 final Class<N> type, final NamespaceKeyCriterion<K> criterion) {
61 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, criterion);
64 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
65 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
68 @SuppressWarnings("unchecked")
69 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
70 final Class<N> type) {
71 return (Map<K, V>) namespaces.get(type);
74 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type,
75 final KT key, final VT value) throws NamespaceNotAvailableException {
76 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
79 @SuppressWarnings({ "unchecked", "rawtypes" })
80 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
81 final StmtContext<?, ?, ?> value) throws NamespaceNotAvailableException {
82 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
85 @SuppressWarnings("unchecked")
87 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
88 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
89 return localNamespace == null ? null : localNamespace.get(key);
93 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
94 @SuppressWarnings("unchecked")
95 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
96 return localNamespace;
99 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
100 @SuppressWarnings("unchecked")
101 Map<K, V> ret = (Map<K,V>) namespaces.get(type);
103 checkLocalNamespaceAllowed(type);
104 ret = new HashMap<>(1);
106 if (namespaces.isEmpty()) {
107 namespaces = new HashMap<>(1);
109 namespaces.put(type, ret);
116 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
118 final V ret = ensureLocalNamespace(type).put(key, value);
119 onNamespaceElementAdded(type, key, value);
124 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
126 final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
128 onNamespaceElementAdded(type, key, value);