Add AnnotationSchemaNodeAware interface
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
index 6f3cc009fd4942fffad1cbfbd6c5971409b741a4..035173508f9bfbd8b548bf9f3180afc7249f2ee4 100644 (file)
@@ -8,23 +8,24 @@
 
 package org.opendaylight.yangtools.yang.model.util;
 
+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.Objects;
+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.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;
@@ -39,14 +40,30 @@ 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) {
-            return -1;
-        }
+    /**
+     * 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());
 
-        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);
     };
 
+    /**
+     * 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);
     }
@@ -65,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<>();
@@ -101,11 +125,10 @@ public abstract class AbstractSchemaContext implements SchemaContext {
         return extensions;
     }
 
-
     @Override
-    public Optional<Module> findModule(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 (Objects.equals(revision, module.getRevision())) {
+            if (revision.equals(module.getRevision())) {
                 return Optional.of(module);
             }
         }
@@ -113,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;
@@ -134,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<>();
@@ -205,14 +220,15 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     }
 
     @Override
-    public DataSchemaNode getDataChildByName(final QName 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
@@ -226,7 +242,7 @@ public abstract class AbstractSchemaContext implements SchemaContext {
     }
 
     @Override
-    public Set<AugmentationSchema> getAvailableAugmentations() {
+    public Set<AugmentationSchemaNode> getAvailableAugmentations() {
         return Collections.emptySet();
     }
 }