BUG-4688: Add flexible match support to NamespaceStorageSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / NamespaceStorageSupport.java
index 82dd703048ae43cb4825ea82682acd6ca5c9f569..889dcc67212c8aeac8991b28150035aa31941c39 100644 (file)
@@ -8,20 +8,22 @@
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
 import com.google.common.collect.ImmutableMap;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 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;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
 
@@ -31,26 +33,38 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
     public abstract NamespaceStorageNode getParentNamespaceStorage();
 
     /**
+     * Return the registry of a source context.
+     *
      * @return registry of source context
      */
-    public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
+    public abstract Registry getBehaviourRegistry();
 
     protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
         // NOOP
     }
 
+    /**
+     * Occurs when an item is added to model namespace.
+     *
+     * @throws SourceException instance of SourceException
+     */
     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
             final V value) {
         // NOOP
     }
 
     @Nonnull
-    public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
+    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);
+        return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
+    }
+
+    public final <K, V, N extends IdentifierNamespace<K, V>> Optional<Entry<K, V>> getFromNamespace(
+            final Class<N> type, final NamespaceKeyCriterion<K> criterion) {
+        return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, criterion);
     }
 
-    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
+    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
     }
 
@@ -74,7 +88,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
     @SuppressWarnings("unchecked")
     @Override
     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
-        final Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
+        final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
 
         V potential = null;
         if (localNamespace != null) {
@@ -82,19 +96,15 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         }
 
         if (potential == null && isModuleIdentifierWithoutSpecifiedRevision(key)) {
-            potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
+            potential = getRegardlessOfRevision((ModuleIdentifier)key, (Map<ModuleIdentifier,V>)localNamespace);
         }
 
         return potential;
     }
 
     private static boolean isModuleIdentifierWithoutSpecifiedRevision(final Object obj) {
-        if (!(obj instanceof ModuleIdentifier)) {
-            return false;
-        }
-
-        final Date rev = ((ModuleIdentifier) obj).getRevision();
-        return rev == SimpleDateFormatUtil.DEFAULT_DATE_IMP || rev == SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE;
+        return obj instanceof ModuleIdentifier
+                && ((ModuleIdentifier) obj).getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP;
     }
 
     private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,