Update DataTreeFactory
[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 com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
12 import java.util.Map;
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;
24
25 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
26
27     private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
28
29     @Override
30     public abstract NamespaceStorageNode getParentNamespaceStorage();
31
32     /**
33      * Return the registry of a source context.
34      *
35      * @return registry of source context
36      */
37     public abstract Registry getBehaviourRegistry();
38
39     protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
40         // NOOP
41     }
42
43     /**
44      * Occurs when an item is added to model namespace.
45      *
46      * @throws SourceException instance of SourceException
47      */
48     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
49             final V value) {
50         // NOOP
51     }
52
53     @Nonnull
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);
57     }
58
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);
62     }
63
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);
66     }
67
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);
72     }
73
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);
77     }
78
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);
83     }
84
85     @SuppressWarnings("unchecked")
86     @Override
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);
90     }
91
92     @Override
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;
97     }
98
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);
102         if (ret == null) {
103             checkLocalNamespaceAllowed(type);
104             ret = new HashMap<>(1);
105
106             if (namespaces.isEmpty()) {
107                 namespaces = new HashMap<>(1);
108             }
109             namespaces.put(type, ret);
110         }
111
112         return ret;
113     }
114
115     @Override
116     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
117             final V value) {
118         final V ret = ensureLocalNamespace(type).put(key, value);
119         onNamespaceElementAdded(type, key, value);
120         return ret;
121     }
122
123     @Override
124     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
125             final V value) {
126         final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
127         if (ret == null) {
128             onNamespaceElementAdded(type, key, value);
129         }
130         return ret;
131     }
132 }