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;
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);
}
*/
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<>();
return extensions;
}
-
@Override
public Optional<Module> findModule(final String name, final Optional<Revision> revision) {
for (final Module module : getNameToModules().get(name)) {
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;
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<>();