package org.opendaylight.yangtools.yang.model.util;
-import com.google.common.base.Optional;
-import com.google.common.base.Supplier;
import com.google.common.collect.SetMultimap;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
-import java.util.Map;
+import java.util.Objects;
+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.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;
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 o2.getRevision().compareTo(o1.getRevision());
};
- protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = () -> new TreeSet<>(REVISION_COMPARATOR);
-
- /**
- * @return yang sources where key is ModuleIdentifier
- */
- 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();
return extensions;
}
+
@Override
- public Module findModuleByName(final String name, final Date revision) {
+ public Optional<Module> findModule(final String name, final Date revision) {
for (final Module module : getNameToModules().get(name)) {
- if (revision == null || revision.equals(module.getRevision())) {
- return module;
+ if (Objects.equals(revision, module.getRevision())) {
+ return Optional.of(module);
}
}
- return null;
+ return Optional.empty();
}
@Override
- public Set<Module> findModuleByNamespace(final URI namespace) {
+ public Set<Module> findModules(final URI namespace) {
final Set<Module> ret = getNamespaceToModules().get(namespace);
return ret == null ? Collections.emptySet() : ret;
}
- @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;
- }
-
@Override
public boolean isAugmenting() {
return false;
return null;
}
+ @Nonnull
@Override
public QName getQName() {
return SchemaContext.NAME;
}
+ @Nonnull
@Override
public SchemaPath getPath() {
return SchemaPath.ROOT;
return null;
}
+ @Nonnull
@Override
public Status getStatus() {
return Status.CURRENT;
}
+ @Nonnull
@Override
public List<UnknownSchemaNode> getUnknownSchemaNodes() {
final List<UnknownSchemaNode> result = new ArrayList<>();
public Set<AugmentationSchema> 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);
- }
-
}