Do not force materialization when not needed
[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 static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
31     private static final Logger LOG = LoggerFactory.getLogger(NamespaceStorageSupport.class);
32
33     private Map<Class<?>, Map<?, ?>> namespaces = ImmutableMap.of();
34
35     /**
36      * {@inheritDoc}
37      *
38      * <p>
39      * This method override provides bimorphic invocation on this method invocation between
40      * {@link SourceSpecificContext} and the more general {@link NamespaceStorageSupport}. We typically do not expect
41      * the two accesses to overlap.
42      */
43     @Override
44     public abstract NamespaceStorageNode getParentNamespaceStorage();
45
46     /**
47      * Return the registry of a source context.
48      *
49      * @return registry of source context
50      */
51     public abstract @NonNull Registry getBehaviourRegistry();
52
53     protected void checkLocalNamespaceAllowed(final Class<? extends ParserNamespace<?, ?>> type) {
54         // NOOP
55     }
56
57     /**
58      * Occurs when an item is added to model namespace.
59      *
60      * @throws SourceException instance of SourceException
61      */
62     protected <K, V, N extends ParserNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
63             final V value) {
64         // NOOP
65     }
66
67     public final <K, V, N extends ParserNamespace<K, V>> Optional<Entry<K, V>> getFromNamespace(
68             final Class<N> type, final NamespaceKeyCriterion<K> criterion) {
69         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, criterion);
70     }
71
72     public final <K, V, N extends ParserNamespace<K, V>> Map<K, V> getNamespace(final Class<N> type) {
73         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
74     }
75
76     @SuppressWarnings("unchecked")
77     final <K, V, N extends ParserNamespace<K, V>> Map<K, V> getLocalNamespace(final Class<N> type) {
78         return (Map<K, V>) accessNamespaces().get(type);
79     }
80
81     final <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNamespace(
82             final Class<N> type, final T key, final U value) {
83         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this, key, value);
84     }
85
86     final <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNamespace(
87             final Class<N> type, final Map<T, U> map) {
88         final NamespaceBehaviour<K, V, N> behavior = getBehaviourRegistry().getNamespaceBehaviour(type);
89         for (final Entry<T, U> validationBundle : map.entrySet()) {
90             behavior.addTo(this, validationBundle.getKey(), validationBundle.getValue());
91         }
92     }
93
94     /**
95      * Associate a context with a key within a namespace.
96      *
97      * @param type Namespace type
98      * @param key Key
99      * @param value Context value
100      * @param <K> namespace key type
101      * @param <N> namespace type
102      * @throws NamespaceNotAvailableException when the namespace is not available.
103      */
104     @SuppressWarnings({ "unchecked", "rawtypes" })
105     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
106             final StmtContext<?, ?, ?> value) {
107         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
108     }
109
110     @SuppressWarnings("unchecked")
111     @Override
112     public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
113         final Map<K, V> localNamespace = (Map<K, V>) accessNamespaces().get(type);
114         return localNamespace == null ? null : localNamespace.get(key);
115     }
116
117     @Override
118     public <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
119         @SuppressWarnings("unchecked")
120         final Map<K, V> localNamespace = (Map<K, V>) accessNamespaces().get(type);
121         return localNamespace;
122     }
123
124     @Override
125     public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
126             final V value) {
127         final V ret = ensureLocalNamespace(type).put(key, value);
128         onNamespaceElementAdded(type, key, value);
129         return ret;
130     }
131
132     @Override
133     public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
134             final V value) {
135         final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
136         if (ret == null) {
137             onNamespaceElementAdded(type, key, value);
138         }
139         return ret;
140     }
141
142     void sweepNamespaces() {
143         namespaces = null;
144         LOG.trace("Swept namespace storages of {}", this);
145     }
146
147     void sweepNamespaces(final Map<Class<?>, SweptNamespace> toWipe) {
148         switch (namespaces.size()) {
149             case 0:
150                 namespaces = ImmutableMap.copyOf(toWipe);
151                 return;
152             case 1:
153                 namespaces = new HashMap<>(namespaces);
154                 break;
155             default:
156                 // No-op, we are ready
157         }
158
159         namespaces.putAll(toWipe);
160         LOG.trace("Trimmed namespace storages of {} to {}", this, namespaces.keySet());
161     }
162
163     private Map<Class<?>, Map<?, ?>> accessNamespaces() {
164         return verifyNotNull(namespaces, "Attempted to access swept namespaces of %s", this);
165     }
166
167     private <K, V, N extends ParserNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
168         @SuppressWarnings("unchecked")
169         Map<K, V> ret = (Map<K,V>) accessNamespaces().get(type);
170         if (ret == null) {
171             checkLocalNamespaceAllowed(type);
172             ret = new HashMap<>(1);
173
174             switch (namespaces.size()) {
175                 case 0:
176                     // We typically have small population of namespaces, use a singleton map
177                     namespaces = ImmutableMap.of(type, ret);
178                     break;
179                 case 1:
180                     // Alright, time to grow to a full HashMap
181                     final Map<Class<?>, Map<?,?>> newNamespaces = new HashMap<>(4);
182                     final Entry<Class<?>, Map<?, ?>> entry = namespaces.entrySet().iterator().next();
183                     newNamespaces.put(entry.getKey(), entry.getValue());
184                     namespaces = newNamespaces;
185                     // fall through
186                 default:
187                     // Already expanded, just put the new namespace
188                     namespaces.put(type, ret);
189             }
190         }
191
192         return ret;
193     }
194 }