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.Map.Entry;
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
14 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
15 import java.util.HashMap;
17 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
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;
24 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
26 private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
30 public abstract NamespaceStorageNode getParentNamespaceStorage();
32 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
34 protected void checkLocalNamespaceAllowed(Class<? extends IdentifierNamespace<?, ?>> type) {
38 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(Class<N> type, K key, V value) {
42 //<K,V,N extends IdentifierNamespace<K, V>> V
43 //public final <K, VT, V extends VT ,N extends IdentifierNamespace<K, V>> VT getFromNamespace(Class<N> type, K key)
44 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, KT key)
45 throws NamespaceNotAvailableException {
46 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
49 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type){
50 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
53 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type){
54 return (Map<K, V>) namespaces.get(type);
57 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, KT key, VT value)
58 throws NamespaceNotAvailableException {
59 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
62 @SuppressWarnings({ "unchecked", "rawtypes" })
63 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(Class<N> type, K key, StmtContext<?, ?, ?> value)
64 throws NamespaceNotAvailableException {
65 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
68 @SuppressWarnings("unchecked")
70 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key) {
71 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
74 if(localNamespace != null) {
75 potential = localNamespace.get(key);
78 if(potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
79 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
85 private <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(ModuleIdentifier key,
86 Map<ModuleIdentifier, V> localNamespace) {
88 if(localNamespace == null) {
92 Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
93 for (Entry<ModuleIdentifier, V> entry : entrySet) {
94 ModuleIdentifier moduleIdentifierInMap = entry.getKey();
95 if(moduleIdentifierInMap.getName().equals(key.getName())) {
96 return entry.getValue();
104 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(Class<N> type) {
105 @SuppressWarnings("unchecked")
106 Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
107 return localNamespace;
111 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value) {
112 @SuppressWarnings("unchecked")
113 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
114 if(localNamespace == null) {
115 checkLocalNamespaceAllowed(type);
116 localNamespace = new HashMap<>();
117 namespaces.put(type, localNamespace);
119 localNamespace.put(key,value);
120 onNamespaceElementAdded(type,key,value);