1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model;
7   
8   import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
9   import gov.nist.secauto.metaschema.core.model.IBoundObject;
10  import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
11  import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly;
12  import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
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.info.IItemReadHandler;
16  import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
17  
18  import java.io.IOException;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.function.Function;
22  import java.util.function.Predicate;
23  import java.util.stream.Collectors;
24  import java.util.stream.Stream;
25  
26  import edu.umd.cs.findbugs.annotations.NonNull;
27  import edu.umd.cs.findbugs.annotations.Nullable;
28  
29  /**
30   * Represents an assembly definition bound to a Java class.
31   */
32  public interface IBoundDefinitionModelAssembly
33      extends IBoundDefinitionModelComplex, IAssemblyDefinition,
34      IFeatureContainerModelAssembly<
35          IBoundInstanceModel<?>,
36          IBoundInstanceModelNamed<?>,
37          IBoundInstanceModelField<?>,
38          IBoundInstanceModelAssembly,
39          IChoiceInstance,
40          IBoundInstanceModelChoiceGroup> { // , IBoundContainerModelAssembly
41  
42    // Assembly Definition Features
43    // ============================
44    @Override
45    @NonNull
46    default IBoundDefinitionModelAssembly getOwningDefinition() {
47      return this;
48    }
49  
50    @Override
51    @NonNull
52    default IBoundDefinitionModelAssembly getDefinition() {
53      return this;
54    }
55  
56    @Override
57    @Nullable
58    default IBoundInstanceModelAssembly getInlineInstance() {
59      // never inline
60      return null;
61    }
62  
63    @Override
64    @NonNull
65    default List<IChoiceInstance> getChoiceInstances() {
66      // not supported
67      return CollectionUtil.emptyList();
68    }
69  
70    @Override
71    @NonNull
72    default Map<String, IBoundProperty<?>> getJsonProperties(@Nullable Predicate<IBoundInstanceFlag> flagFilter) {
73      Stream<? extends IBoundInstanceFlag> flagStream = getFlagInstances().stream();
74  
75      if (flagFilter != null) {
76        flagStream = flagStream.filter(flagFilter);
77      }
78  
79      return ObjectUtils.notNull(Stream.concat(flagStream, getModelInstances().stream())
80          .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity())));
81    }
82  
83    @Override
84    @NonNull
85    default IBoundObject readItem(@Nullable IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
86      return handler.readItemAssembly(parent, this);
87    }
88  
89    @Override
90    default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
91      handler.writeItemAssembly(item, this);
92    }
93  
94    @Override
95    default boolean canHandleXmlQName(@NonNull IEnhancedQName qname) {
96      return qname.equals(getRootQName());
97    }
98  }