1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model.impl;
7   
8   import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
9   import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
10  import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance;
11  import gov.nist.secauto.metaschema.core.model.IAttributable;
12  import gov.nist.secauto.metaschema.core.model.IBoundObject;
13  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
14  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
15  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
16  import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup;
17  import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly;
18  import gov.nist.secauto.metaschema.databind.model.IBoundProperty;
19  import gov.nist.secauto.metaschema.databind.model.annotations.BoundGroupedAssembly;
20  import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil;
21  
22  import java.util.Arrays;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.stream.Collectors;
27  
28  import edu.umd.cs.findbugs.annotations.NonNull;
29  import nl.talsmasoftware.lazy4j.Lazy;
30  
31  /**
32   * Represents an assembly model instance that is a member of a choice group
33   * instance.
34   */
35  public class InstanceModelGroupedAssembly
36      extends AbstractAssemblyInstance<
37          IBoundInstanceModelChoiceGroup,
38          IBoundDefinitionModelAssembly,
39          IBoundInstanceModelGroupedAssembly,
40          IBoundDefinitionModelAssembly>
41      implements IBoundInstanceModelGroupedAssembly {
42    @NonNull
43    private final BoundGroupedAssembly annotation;
44    @NonNull
45    private final IBoundDefinitionModelAssembly definition;
46    @NonNull
47    private final Lazy<Map<String, IBoundProperty<?>>> jsonProperties;
48    @NonNull
49    private final Lazy<Map<IAttributable.Key, Set<String>>> properties;
50  
51    /**
52     * Construct a new assembly model instance instance that is a member of a choice
53     * group instance.
54     *
55     * @param annotation
56     *          the Java annotation the instance is bound to
57     * @param definition
58     *          the assembly definition this instance is bound to
59     * @param container
60     *          the choice group instance containing the instance
61     */
62    public InstanceModelGroupedAssembly(
63        @NonNull BoundGroupedAssembly annotation,
64        @NonNull IBoundDefinitionModelAssembly definition,
65        @NonNull IBoundInstanceModelChoiceGroup container) {
66      super(container);
67      this.annotation = annotation;
68      this.definition = definition;
69      // IBoundInstanceFlag jsonKey = getEffectiveJsonKey();
70      // Predicate<IBoundInstanceFlag> flagFilter = jsonKey == null ? null : (flag) ->
71      // !jsonKey.equals(flag);
72      // return getDefinition().getJsonProperties(flagFilter);
73      this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> getDefinition().getJsonProperties(null)));
74      this.properties = ObjectUtils.notNull(
75          Lazy.lazy(() -> CollectionUtil.unmodifiableMap(ObjectUtils.notNull(
76              Arrays.stream(annotation.properties())
77                  .map(ModelUtil::toPropertyEntry)
78                  .collect(
79                      Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2, LinkedHashMap::new))))));
80    }
81  
82    private BoundGroupedAssembly getAnnotation() {
83      return annotation;
84    }
85  
86    // ------------------------------------------
87    // - Start annotation driven code - CPD-OFF -
88    // ------------------------------------------
89  
90    @Override
91    public Class<? extends IBoundObject> getBoundClass() {
92      return getAnnotation().binding();
93    }
94  
95    @Override
96    public Map<String, IBoundProperty<?>> getJsonProperties() {
97      return ObjectUtils.notNull(jsonProperties.get());
98    }
99  
100   @Override
101   public IBoundDefinitionModelAssembly getDefinition() {
102     return definition;
103   }
104 
105   @Override
106   public String getFormalName() {
107     return ModelUtil.resolveNoneOrValue(getAnnotation().formalName());
108   }
109 
110   @Override
111   public MarkupLine getDescription() {
112     return ModelUtil.resolveToMarkupLine(getAnnotation().description());
113   }
114 
115   @Override
116   public Map<Key, Set<String>> getProperties() {
117     return ObjectUtils.notNull(properties.get());
118   }
119 
120   @Override
121   public MarkupMultiline getRemarks() {
122     return ModelUtil.resolveToMarkupMultiline(getAnnotation().remarks());
123   }
124 
125   @Override
126   public String getDiscriminatorValue() {
127     return ModelUtil.resolveNoneOrValue(getAnnotation().discriminatorValue());
128   }
129 
130   @Override
131   public String getUseName() {
132     return ModelUtil.resolveNoneOrValue(getAnnotation().useName());
133   }
134 
135   @Override
136   public Integer getUseIndex() {
137     return ModelUtil.resolveDefaultInteger(getAnnotation().useIndex());
138   }
139 }