Add AnnotationSchemaNodeAware interface
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
index b379152ee69a1be6ee8b4e86124225193eae564c..035173508f9bfbd8b548bf9f3180afc7249f2ee4 100644 (file)
@@ -18,14 +18,14 @@ import java.util.Comparator;
 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 javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
+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.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
@@ -40,9 +40,30 @@ 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(first.getRevision(), second.getRevision());
+        (first, second) -> Revision.compare(second.getRevision(), first.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);
+    };
+
+    /**
+     * 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 static final TreeSet<Module> createModuleSet() {
         return new TreeSet<>(REVISION_COMPARATOR);
     }
@@ -61,6 +82,13 @@ public abstract class AbstractSchemaContext implements SchemaContext {
      */
     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<>();
@@ -97,7 +125,6 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return extensions;
     }
 
-
     @Override
     public Optional<Module> findModule(final String name, final Optional<Revision> revision) {
         for (final Module module : getNameToModules().get(name)) {
@@ -109,17 +136,28 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return Optional.empty();
     }
 
+    @Override
+    public Optional<Module> findModule(final QNameModule qnameModule) {
+        return Optional.ofNullable(getModuleMap().get(qnameModule));
+    }
+
     @Override
     public Set<Module> findModules(final URI namespace) {
-        final Set<Module> ret = getNamespaceToModules().get(namespace);
-        return ret == null ? Collections.emptySet() : ret;
+        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;
@@ -130,40 +168,21 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return false;
     }
 
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return null;
-    }
-
-    @Nonnull
     @Override
     public QName getQName() {
         return SchemaContext.NAME;
     }
 
-    @Nonnull
     @Override
     public SchemaPath getPath() {
         return SchemaPath.ROOT;
     }
 
-    @Override
-    public String getDescription() {
-        return null;
-    }
-
-    @Override
-    public String getReference() {
-        return null;
-    }
-
-    @Nonnull
     @Override
     public Status getStatus() {
         return Status.CURRENT;
     }
 
-    @Nonnull
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
         final List<UnknownSchemaNode> result = new ArrayList<>();