Cleanup checkstyle in yang-{data,model}-api
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaContext.java
index ecafe233a4fa210b52943408f63a7d0ba7e03964..ead8fc7beedf2a2fa113456e21c4d31a4ea9e2be 100644 (file)
@@ -8,20 +8,27 @@
 package org.opendaylight.yangtools.yang.model.api;
 
 import com.google.common.base.Optional;
-import org.opendaylight.yangtools.yang.common.QName;
-
+import com.google.common.collect.Sets;
 import java.net.URI;
 import java.util.Date;
 import java.util.Set;
+import javax.annotation.concurrent.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
 
 /**
- * The interface contains the methods for manipulating all the top level context
- * data (data from all red modules) like YANG notifications, extensions,
+ * The interface represents static view of compiled yang files,
+ * contains the methods for obtaining all the top level context
+ * data (data from all modules) like YANG notifications, extensions,
  * operations...
+ * Instances MUST be immutable and thus usage in multi threaded
+ * environment is safe.
  */
+@Immutable
 public interface SchemaContext extends ContainerSchemaNode {
-
-    public static final QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data");
+    /**
+     * QName of NETCONF top-level data node.
+     */
+    QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data").intern();
 
     /**
      * Returns data schema node instances which represents direct subnodes (like
@@ -39,17 +46,6 @@ public interface SchemaContext extends ContainerSchemaNode {
      */
     Set<Module> getModules();
 
-    /**
-     *
-     * Returns notification definition instances which are defined as the direct
-     * subelements in all YANG modules in the context.
-     *
-     * @return set of <code>NotificationDefinition</code> instances which
-     *         represents nodes defined via <code>notification</code> YANG
-     *         keyword
-     */
-    Set<NotificationDefinition> getNotifications();
-
     /**
      * Returns rpc definition instances which are defined as the direct
      * subelements in all YANG modules in the context.
@@ -61,7 +57,7 @@ public interface SchemaContext extends ContainerSchemaNode {
 
     /**
      * Returns extencion definition instances which are defined as the direct
-     * subelements in all YANG modules in the context
+     * subelements in all YANG modules in the context.
      *
      * @return set of <code>ExtensionDefinition</code> instances which
      *         represents nodes defined via <code>extension</code> YANG keyword
@@ -85,7 +81,6 @@ public interface SchemaContext extends ContainerSchemaNode {
     Module findModuleByName(final String name, final Date revision);
 
     /**
-     *
      * Returns module instance (from the context) with concrete namespace.
      *
      * @param namespace
@@ -93,25 +88,37 @@ public interface SchemaContext extends ContainerSchemaNode {
      * @return module instance which has namespace equal to the
      *         <code>namespace</code> or <code>null</code> in other cases
      */
-    Set<Module> findModuleByNamespace(final URI namespace);
+    default Set<Module> findModuleByNamespace(final URI namespace) {
+        return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
+    }
 
     /**
      * Returns module instance based on given namespace and revision. If
      * revision is not specified, returns module with newest revision.
      *
-     * @param namespace
-     * @param revision
-     * @return
+     * @param namespace Module namespace, may be null
+     * @param revision Module revision, may be null
+     * @return Matching module or null if a match is not found
      */
-    Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision);
-
+    default 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;
+    }
 
     /**
      * Get yang source code represented as string for matching
      * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier}.
+     *
      * @param moduleIdentifier must provide a non-null
-     * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier#getName()},
-     * other methods might return null.
+     *        {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier#getName()}, other methods might return
+     *        null.
      * @return value iif matching module is found in schema context.
      */
     Optional<String> getModuleSource(ModuleIdentifier moduleIdentifier);
@@ -120,5 +127,4 @@ public interface SchemaContext extends ContainerSchemaNode {
      * Get all module and submodule identifiers.
      */
     Set<ModuleIdentifier> getAllModuleIdentifiers();
-
 }