Add AnnotationSchemaNodeAware interface
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
index fa69b5354576d5bdf83aac1fa6a22695d5313ad6..035173508f9bfbd8b548bf9f3180afc7249f2ee4 100644 (file)
@@ -8,28 +8,28 @@
 
 package org.opendaylight.yangtools.yang.model.util;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Supplier;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.SetMultimap;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.Date;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 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,42 +39,56 @@ 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 {
+    /**
+     * A {@link Module} comparator based on {@link Module#getRevision()}, placing latest revision first. Note this
+     * comparator does not take into account module name and so two modules with different names but same revisions
+     * compare as equal.
+     */
+    protected static final Comparator<Module> REVISION_COMPARATOR =
+        (first, second) -> Revision.compare(second.getRevision(), first.getRevision());
 
-    protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = new Supplier<TreeSet<Module>>() {
-        @Override
-        public TreeSet<Module> get() {
-            return new TreeSet<>(REVISION_COMPARATOR);
-        }
-    };
-
-    protected static final Comparator<Module> REVISION_COMPARATOR = new Comparator<Module>() {
-        @Override
-        public int compare(final Module o1, final Module o2) {
-            if (o2.getRevision() == null) {
-                return -1;
-            }
-
-            return o2.getRevision().compareTo(o1.getRevision());
-        }
+    /**
+     * A {@link Module} comparator based on {@link Module#getName()} and {@link Module#getRevision()}, ordering modules
+     * lexicographically by their name and then in order of descending revision. This comparator assumes that
+     * the combination of these two attributes is sufficient to be consistent with hashCode/equals.
+     */
+    protected static final Comparator<Module> NAME_REVISION_COMPARATOR = (first, second) -> {
+        final int cmp = first.getName().compareTo(second.getName());
+        return cmp != 0 ? cmp : REVISION_COMPARATOR.compare(first, second);
     };
 
     /**
-     * @return yang sources where key is ModuleIdentifier
+     * Create a TreeSet for containing Modules with the same name, such that the set is ordered
+     * by {@link #REVISION_COMPARATOR}.
+     *
+     * @return A fresh TreeSet instance.
      */
-    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();
 
+    /**
+     * Returns the namespace+revision-to-module mapping.
+     *
+     * @return Map of modules where key is Module's QNameModule.
+     */
+    protected abstract Map<QNameModule, Module> getModuleMap();
+
     @Override
     public Set<DataSchemaNode> getDataDefinitions() {
         final Set<DataSchemaNode> dataDefs = new HashSet<>();
@@ -112,40 +126,38 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     }
 
     @Override
-    public Module findModuleByName(final String name, final Date revision) {
+    public Optional<Module> findModule(final String name, final Optional<Revision> revision) {
         for (final Module module : getNameToModules().get(name)) {
-            if (revision == null || revision.equals(module.getRevision())) {
-                return module;
+            if (revision.equals(module.getRevision())) {
+                return Optional.of(module);
             }
         }
 
-        return null;
+        return Optional.empty();
     }
 
     @Override
-    public Set<Module> findModuleByNamespace(final URI namespace) {
-        final Set<Module> ret = getNamespaceToModules().get(namespace);
-        return ret == null ? Collections.<Module>emptySet() : ret;
+    public Optional<Module> findModule(final QNameModule qnameModule) {
+        return Optional.ofNullable(getModuleMap().get(qnameModule));
     }
 
     @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;
+    public Set<Module> findModules(final URI namespace) {
+        return getNamespaceToModules().get(namespace);
     }
 
+    @Override
+    public Set<Module> findModules(final String name) {
+        return getNameToModules().get(name);
+    }
+
+    @Deprecated
     @Override
     public boolean isAugmenting() {
         return false;
     }
 
+    @Deprecated
     @Override
     public boolean isAddedByUses() {
         return false;
@@ -156,11 +168,6 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return false;
     }
 
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return null;
-    }
-
     @Override
     public QName getQName() {
         return SchemaContext.NAME;
@@ -171,16 +178,6 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return SchemaPath.ROOT;
     }
 
-    @Override
-    public String getDescription() {
-        return null;
-    }
-
-    @Override
-    public String getReference() {
-        return null;
-    }
-
     @Override
     public Status getStatus() {
         return Status.CURRENT;
@@ -223,25 +220,15 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     }
 
     @Override
-    public DataSchemaNode getDataChildByName(final QName name) {
-        for (Module module : getModules()) {
-            final DataSchemaNode result = module.getDataChildByName(name);
-            if (result != null) {
-                return result;
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public DataSchemaNode getDataChildByName(final String name) {
+    public Optional<DataSchemaNode> findDataChildByName(final QName name) {
+        requireNonNull(name);
         for (Module module : getModules()) {
-            final DataSchemaNode result = module.getDataChildByName(name);
-            if (result != null) {
+            final Optional<DataSchemaNode> result = module.findDataChildByName(name);
+            if (result.isPresent()) {
                 return result;
             }
         }
-        return null;
+        return Optional.empty();
     }
 
     @Override
@@ -255,20 +242,7 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     }
 
     @Override
-    public Set<AugmentationSchema> getAvailableAugmentations() {
+    public Set<AugmentationSchemaNode> 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);
-    }
-
 }