Merge "Make sure YangInstanceIdentifier.EMPTY is retained"
[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     public final <K, VT, V extends VT ,N extends IdentifierNamespace<K, V>> VT getFromNamespace(Class<N> type, K key)
38             throws NamespaceNotAvailableException {
39         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
40     }
41
42     public final <K,V,VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, K key, VT value)
43             throws NamespaceNotAvailableException {
44         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
45     }
46
47     @SuppressWarnings({ "unchecked", "rawtypes" })
48     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(Class<N> type, K key, StmtContext<?, ?, ?> value)
49             throws NamespaceNotAvailableException {
50         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
51     }
52
53     @Override
54     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key) {
55         @SuppressWarnings("unchecked")
56         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
57         if(localNamespace != null) {
58             return localNamespace.get(key);
59         }
60         return null;
61     }
62
63
64
65     @Override
66     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value) {
67         @SuppressWarnings("unchecked")
68         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
69         if(localNamespace == null) {
70             checkLocalNamespaceAllowed(type);
71             localNamespace = new HashMap<>();
72             namespaces.put(type, localNamespace);
73         }
74         localNamespace.put(key,value);
75         onNamespaceElementAdded(type,key,value);
76     }
77
78 }