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.util.ObjectUtils;
11  import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
12  import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
13  
14  import java.io.IOException;
15  import java.util.Map;
16  import java.util.function.Function;
17  import java.util.function.Predicate;
18  import java.util.stream.Collectors;
19  import java.util.stream.Stream;
20  
21  import javax.xml.namespace.QName;
22  
23  import edu.umd.cs.findbugs.annotations.NonNull;
24  import edu.umd.cs.findbugs.annotations.Nullable;
25  
26  /**
27   * Represents an assembly definition bound to a Java class.
28   */
29  public interface IBoundDefinitionModelAssembly
30      extends IBoundDefinitionModelComplex, IBoundContainerModelAssembly, IAssemblyDefinition {
31  
32    // Assembly Definition Features
33    // ============================
34    @Override
35    @NonNull
36    default IBoundDefinitionModelAssembly getOwningDefinition() {
37      return this;
38    }
39  
40    @Override
41    @NonNull
42    default IBoundDefinitionModelAssembly getDefinition() {
43      return this;
44    }
45  
46    @Override
47    @Nullable
48    default IBoundInstanceModelAssembly getInlineInstance() {
49      // never inline
50      return null;
51    }
52    //
53    // @Override
54    // @NonNull
55    // default QName getXmlQName() {
56    // return ObjectUtils.requireNonNull(getRootXmlQName());
57    // }
58  
59    @Override
60    @NonNull
61    default Map<String, IBoundProperty<?>> getJsonProperties(@Nullable Predicate<IBoundInstanceFlag> flagFilter) {
62      Stream<? extends IBoundInstanceFlag> flagStream = getFlagInstances().stream();
63  
64      if (flagFilter != null) {
65        flagStream = flagStream.filter(flagFilter);
66      }
67  
68      return ObjectUtils.notNull(Stream.concat(flagStream, getModelInstances().stream())
69          .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity())));
70    }
71  
72    @Override
73    @NonNull
74    default IBoundObject readItem(@Nullable IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
75      return handler.readItemAssembly(parent, this);
76    }
77  
78    @Override
79    default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
80      handler.writeItemAssembly(item, this);
81    }
82  
83    @Override
84    default boolean canHandleXmlQName(@NonNull QName qname) {
85      return qname.equals(getRootXmlQName());
86    }
87  }