Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / NamespaceStorageSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18
19 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
20
21     private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
22
23
24     @Override
25     public abstract NamespaceStorageNode getParentNamespaceStorage();
26
27     public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
28
29     protected void checkLocalNamespaceAllowed(Class<? extends IdentifierNamespace<?, ?>> type) {
30         // NOOP
31     }
32
33     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(Class<N> type, K key, V value) {
34         // NOOP
35     }
36
37     //<K,V,N extends IdentifierNamespace<K, V>> V
38     //public final <K, VT, V extends VT ,N extends IdentifierNamespace<K, V>> VT getFromNamespace(Class<N> type, K key)
39     public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, KT key)
40             throws NamespaceNotAvailableException {
41         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
42     }
43
44     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type){
45         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
46     }
47
48     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type){
49         return (Map<K, V>) namespaces.get(type);
50     }
51
52     public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, KT key, VT value)
53             throws NamespaceNotAvailableException {
54         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
55     }
56
57     @SuppressWarnings({ "unchecked", "rawtypes" })
58     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(Class<N> type, K key, StmtContext<?, ?, ?> value)
59             throws NamespaceNotAvailableException {
60         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
61     }
62
63     @Override
64     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key) {
65         @SuppressWarnings("unchecked")
66         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
67         if(localNamespace != null) {
68             return localNamespace.get(key);
69         }
70         return null;
71     }
72
73     @Override
74     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(Class<N> type) {
75         @SuppressWarnings("unchecked")
76         Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
77         return localNamespace;
78     }
79
80     @Override
81     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value) {
82         @SuppressWarnings("unchecked")
83         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
84         if(localNamespace == null) {
85             checkLocalNamespaceAllowed(type);
86             localNamespace = new HashMap<>();
87             namespaces.put(type, localNamespace);
88         }
89         localNamespace.put(key,value);
90         onNamespaceElementAdded(type,key,value);
91     }
92
93 }