1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen.json.impl;
7   
8   import com.fasterxml.jackson.databind.node.ObjectNode;
9   
10  import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance;
11  import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped;
12  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
13  import gov.nist.secauto.metaschema.schemagen.json.IDefineableJsonSchema.IKey;
14  import gov.nist.secauto.metaschema.schemagen.json.IDefinitionJsonSchema;
15  import gov.nist.secauto.metaschema.schemagen.json.IJsonGenerationState;
16  import gov.nist.secauto.metaschema.schemagen.json.impl.builder.IModelInstanceBuilder;
17  
18  import java.util.Map;
19  
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22  
23  public class ChoiceGroupInstanceJsonProperty
24      extends AbstractJsonProperty<IChoiceGroupInstance>
25      implements IGroupableModelInstanceJsonProperty<IChoiceGroupInstance> {
26  
27    private final IModelInstanceBuilder<?> collectionBuilder;
28  
29    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
30    public ChoiceGroupInstanceJsonProperty(
31        @NonNull IChoiceGroupInstance instance,
32        @NonNull IJsonGenerationState state) {
33      super(instance);
34      this.collectionBuilder = IModelInstanceBuilder.newCollectionBuilder(instance);
35  
36      for (INamedModelInstanceGrouped groupedInstance : instance.getNamedModelInstances()) {
37        assert groupedInstance != null;
38        this.collectionBuilder.addItemType(groupedInstance);
39      }
40    }
41  
42    protected IModelInstanceBuilder<?> getCollectionBuilder() {
43      return collectionBuilder;
44    }
45  
46    @Override
47    public String getName() {
48      return ObjectUtils.requireNonNull(getInstance().getGroupAsName());
49    }
50  
51    @Override
52    protected void generateBody(
53        ObjectNode obj,
54        IJsonGenerationState state) {
55      getCollectionBuilder().build(obj, state);
56    }
57  
58    @Override
59    public void gatherDefinitions(
60        @NonNull Map<IKey, IDefinitionJsonSchema<?>> gatheredDefinitions,
61        @NonNull IJsonGenerationState state) {
62      for (IModelInstanceBuilder.IType type : collectionBuilder.getTypes()) {
63        type.gatherDefinitions(gatheredDefinitions, state);
64      }
65    }
66  }