1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model;
7   
8   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
9   
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  
12  /**
13   * A base class for name members of a containing model.
14   *
15   * @param <PARENT>
16   *          the Java type of the parent model container for this instance
17   * @param <PARENT_DEFINITION>
18   *          the Java type of the containing assembly definition
19   */
20  public abstract class AbstractNamedModelInstance<
21      PARENT extends IContainerModel,
22      PARENT_DEFINITION extends IAssemblyDefinition>
23      extends AbstractNamedInstance<PARENT>
24      implements INamedModelInstance {
25  
26    /**
27     * Construct a new instance.
28     *
29     * @param parent
30     *          the parent containing the instance
31     */
32    protected AbstractNamedModelInstance(@NonNull PARENT parent) {
33      super(parent, name -> parent.getOwningDefinition().getContainingModule().toModelQName(name));
34    }
35  
36    @Override
37    public final PARENT_DEFINITION getContainingDefinition() {
38      // TODO: look for ways to avoid this cast. The problem is that IContainerModel
39      // is not easily generalized, since this interface is extended by core model
40      // interfaces. Perhaps moving default implementation into abstract or concrete
41      // implementation is a possible path?
42      return ObjectUtils.asType(getParentContainer().getOwningDefinition());
43    }
44  
45    @Override
46    public IModule getContainingModule() {
47      return getContainingDefinition().getContainingModule();
48    }
49  }