BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
index 737461b873a11cdc25c48928b4d7e9cd91916f3c..6f3cc009fd4942fffad1cbfbd6c5971409b741a4 100644 (file)
@@ -8,8 +8,6 @@
 
 package org.opendaylight.yangtools.yang.model.util;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Supplier;
 import com.google.common.collect.SetMultimap;
 import java.net.URI;
 import java.util.ArrayList;
@@ -19,9 +17,11 @@ import java.util.Date;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
@@ -29,7 +29,6 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -39,7 +38,6 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 
-
 public abstract class AbstractSchemaContext implements SchemaContext {
     protected static final Comparator<Module> REVISION_COMPARATOR = (o1, o2) -> {
         if (o2.getRevision() == null) {
@@ -49,19 +47,20 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return o2.getRevision().compareTo(o1.getRevision());
     };
 
-    protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = () -> new TreeSet<>(REVISION_COMPARATOR);
-
-    /**
-     * @return yang sources where key is ModuleIdentifier
-     */
-    protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
+    protected static final TreeSet<Module> createModuleSet() {
+        return new TreeSet<>(REVISION_COMPARATOR);
+    }
 
     /**
+     * Returns the namespace-to-module mapping.
+     *
      * @return Map of modules where key is namespace
      */
     protected abstract SetMultimap<URI, Module> getNamespaceToModules();
 
     /**
+     * Returns the module name-to-module mapping.
+     *
      * @return Map of modules where key is name of module
      */
     protected abstract SetMultimap<String, Module> getNameToModules();
@@ -102,36 +101,24 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return extensions;
     }
 
+
     @Override
-    public Module findModuleByName(final String name, final Date revision) {
+    public Optional<Module> findModule(final String name, final Date revision) {
         for (final Module module : getNameToModules().get(name)) {
-            if (revision == null || revision.equals(module.getRevision())) {
-                return module;
+            if (Objects.equals(revision, module.getRevision())) {
+                return Optional.of(module);
             }
         }
 
-        return null;
+        return Optional.empty();
     }
 
     @Override
-    public Set<Module> findModuleByNamespace(final URI namespace) {
+    public Set<Module> findModules(final URI namespace) {
         final Set<Module> ret = getNamespaceToModules().get(namespace);
         return ret == null ? Collections.emptySet() : ret;
     }
 
-    @Override
-    public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
-        if (namespace == null) {
-            return null;
-        }
-        for (Module module : findModuleByNamespace(namespace)) {
-            if (revision == null || revision.equals(module.getRevision())) {
-                return module;
-            }
-        }
-        return null;
-    }
-
     @Override
     public boolean isAugmenting() {
         return false;
@@ -152,11 +139,13 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return null;
     }
 
+    @Nonnull
     @Override
     public QName getQName() {
         return SchemaContext.NAME;
     }
 
+    @Nonnull
     @Override
     public SchemaPath getPath() {
         return SchemaPath.ROOT;
@@ -172,11 +161,13 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return null;
     }
 
+    @Nonnull
     @Override
     public Status getStatus() {
         return Status.CURRENT;
     }
 
+    @Nonnull
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
         final List<UnknownSchemaNode> result = new ArrayList<>();
@@ -238,17 +229,4 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     public Set<AugmentationSchema> getAvailableAugmentations() {
         return Collections.emptySet();
     }
-
-    //FIXME: should work for submodules too
-    @Override
-    public Set<ModuleIdentifier> getAllModuleIdentifiers() {
-        return getIdentifiersToSources().keySet();
-    }
-
-    @Override
-    public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
-        String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
-        return Optional.fromNullable(maybeSource);
-    }
-
 }