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.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;
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);
}
*/
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<>();
}
@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 Optional<Module> findModule(final QNameModule qnameModule) {
+ return Optional.ofNullable(getModuleMap().get(qnameModule));
}
@Override
- public Set<Module> findModuleByNamespace(final URI namespace) {
- final Set<Module> ret = getNamespaceToModules().get(namespace);
- return ret == null ? Collections.emptySet() : ret;
+ 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;
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<>();
}
@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
}
@Override
- public Set<AugmentationSchema> getAvailableAugmentations() {
+ public Set<AugmentationSchemaNode> getAvailableAugmentations() {
return Collections.emptySet();
}
}