Bug 2366 - new parser API - implementation of declared statements
[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,N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, K 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<?, ?> getAllFromNamespace(Class<N> type){
45         return namespaces.get(type);
46     }
47
48     public final <K,V,VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, K key, VT value)
49             throws NamespaceNotAvailableException {
50         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
51     }
52
53     @SuppressWarnings({ "unchecked", "rawtypes" })
54     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(Class<N> type, K key, StmtContext<?, ?, ?> value)
55             throws NamespaceNotAvailableException {
56         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
57     }
58
59     @Override
60     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key) {
61         @SuppressWarnings("unchecked")
62         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
63         if(localNamespace != null) {
64             return localNamespace.get(key);
65         }
66         return null;
67     }
68
69
70
71     @Override
72     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value) {
73         @SuppressWarnings("unchecked")
74         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
75         if(localNamespace == null) {
76             checkLocalNamespaceAllowed(type);
77             localNamespace = new HashMap<>();
78             namespaces.put(type, localNamespace);
79         }
80         localNamespace.put(key,value);
81         onNamespaceElementAdded(type,key,value);
82     }
83
84 }