Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / NamespaceStorageSupport.java
index 0f9b948496fb372be3ca2e3e2852738e98b34fde..0fe03bec79a97db2aa2a59bbd6da39dcc8fa292d 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
+import com.google.common.collect.ImmutableMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
@@ -22,8 +24,7 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
 
-    private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
-
+    private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
 
     @Override
     public abstract NamespaceStorageNode getParentNamespaceStorage();
@@ -38,8 +39,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         // NOOP
     }
 
-    //<K,V,N extends IdentifierNamespace<K, V>> V
-    //public final <K, VT, V extends VT ,N extends IdentifierNamespace<K, V>> VT getFromNamespace(Class<N> type, K key)
+    @Nonnull
     public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type, final KT key)
             throws NamespaceNotAvailableException {
         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
@@ -49,6 +49,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
     }
 
+    @SuppressWarnings("unchecked")
     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(final Class<N> type){
         return (Map<K, V>) namespaces.get(type);
     }
@@ -70,11 +71,11 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
 
         V potential = null;
-        if(localNamespace != null) {
+        if (localNamespace != null) {
             potential = localNamespace.get(key);
         }
 
-        if(potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
+        if (potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
             potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
         }
 
@@ -110,9 +111,13 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
         @SuppressWarnings("unchecked")
         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
-        if(localNamespace == null) {
+        if (localNamespace == null) {
             checkLocalNamespaceAllowed(type);
-            localNamespace = new HashMap<>();
+            localNamespace = new HashMap<>(1);
+
+            if (namespaces.isEmpty()) {
+                namespaces = new HashMap<>(1);
+            }
             namespaces.put(type, localNamespace);
         }
         localNamespace.put(key,value);