Cleanup NamespaceStorageSupport/StmtContext API conflict
[yangtools.git] / yang / yang-parser-reactor / 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 org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
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     public final <K, V, N extends IdentifierNamespace<K, V>> Optional<Entry<K, V>> getFromNamespace(
54             final Class<N> type, final NamespaceKeyCriterion<K> criterion) {
55         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, criterion);
56     }
57
58     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getNamespace(final Class<N> type) {
59         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
60     }
61
62     @SuppressWarnings("unchecked")
63     final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getLocalNamespace(final Class<N> type) {
64         return (Map<K, V>) namespaces.get(type);
65     }
66
67     final <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNamespace(
68             final Class<N> type, final T key, final U value) {
69         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this, key, value);
70     }
71
72     final <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNamespace(
73             final Class<N> type, final Map<T, U> map) {
74         final NamespaceBehaviour<K, V, N> behavior = getBehaviourRegistry().getNamespaceBehaviour(type);
75         for (final Entry<T, U> validationBundle : map.entrySet()) {
76             behavior.addTo(this, validationBundle.getKey(), validationBundle.getValue());
77         }
78     }
79
80     /**
81      * Associate a context with a key within a namespace.
82      *
83      * @param type Namespace type
84      * @param key Key
85      * @param value Context value
86      * @param <K> namespace key type
87      * @param <N> namespace type
88      * @throws NamespaceNotAvailableException when the namespace is not available.
89      */
90     @SuppressWarnings({ "unchecked", "rawtypes" })
91     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
92             final StmtContext<?, ?, ?> value) {
93         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
94     }
95
96     @SuppressWarnings("unchecked")
97     @Override
98     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
99         final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
100         return localNamespace == null ? null : localNamespace.get(key);
101     }
102
103     @Override
104     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
105         @SuppressWarnings("unchecked")
106         final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
107         return localNamespace;
108     }
109
110     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
111         @SuppressWarnings("unchecked")
112         Map<K, V> ret = (Map<K,V>) namespaces.get(type);
113         if (ret == null) {
114             checkLocalNamespaceAllowed(type);
115             ret = new HashMap<>(1);
116
117             switch (namespaces.size()) {
118                 case 0:
119                     // We typically have small population of namespaces, use a singleton map
120                     namespaces = ImmutableMap.of(type, ret);
121                     break;
122                 case 1:
123                     // Alright, time to grow to a full HashMap
124                     final Map<Class<?>, Map<?,?>> newNamespaces = new HashMap<>(4);
125                     final Entry<Class<?>, Map<?, ?>> entry = namespaces.entrySet().iterator().next();
126                     newNamespaces.put(entry.getKey(), entry.getValue());
127                     namespaces = newNamespaces;
128                     // fall through
129                 default:
130                     // Already expanded, just put the new namespace
131                     namespaces.put(type, ret);
132             }
133         }
134
135         return ret;
136     }
137
138     @Override
139     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
140             final V value) {
141         final V ret = ensureLocalNamespace(type).put(key, value);
142         onNamespaceElementAdded(type, key, value);
143         return ret;
144     }
145
146     @Override
147     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
148             final V value) {
149         final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
150         if (ret == null) {
151             onNamespaceElementAdded(type, key, value);
152         }
153         return ret;
154     }
155 }