001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.model;
007
008import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
009import gov.nist.secauto.metaschema.core.model.IBoundObject;
010import gov.nist.secauto.metaschema.core.util.ObjectUtils;
011import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
012import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
013
014import java.io.IOException;
015import java.util.Map;
016import java.util.function.Function;
017import java.util.function.Predicate;
018import java.util.stream.Collectors;
019import java.util.stream.Stream;
020
021import javax.xml.namespace.QName;
022
023import edu.umd.cs.findbugs.annotations.NonNull;
024import edu.umd.cs.findbugs.annotations.Nullable;
025
026/**
027 * Represents an assembly definition bound to a Java class.
028 */
029public interface IBoundDefinitionModelAssembly
030    extends IBoundDefinitionModelComplex, IBoundContainerModelAssembly, IAssemblyDefinition {
031
032  // Assembly Definition Features
033  // ============================
034  @Override
035  @NonNull
036  default IBoundDefinitionModelAssembly getOwningDefinition() {
037    return this;
038  }
039
040  @Override
041  @NonNull
042  default IBoundDefinitionModelAssembly getDefinition() {
043    return this;
044  }
045
046  @Override
047  @Nullable
048  default IBoundInstanceModelAssembly getInlineInstance() {
049    // never inline
050    return null;
051  }
052  //
053  // @Override
054  // @NonNull
055  // default QName getXmlQName() {
056  // return ObjectUtils.requireNonNull(getRootXmlQName());
057  // }
058
059  @Override
060  @NonNull
061  default Map<String, IBoundProperty<?>> getJsonProperties(@Nullable Predicate<IBoundInstanceFlag> flagFilter) {
062    Stream<? extends IBoundInstanceFlag> flagStream = getFlagInstances().stream();
063
064    if (flagFilter != null) {
065      flagStream = flagStream.filter(flagFilter);
066    }
067
068    return ObjectUtils.notNull(Stream.concat(flagStream, getModelInstances().stream())
069        .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity())));
070  }
071
072  @Override
073  @NonNull
074  default IBoundObject readItem(@Nullable IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
075    return handler.readItemAssembly(parent, this);
076  }
077
078  @Override
079  default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
080    handler.writeItemAssembly(item, this);
081  }
082
083  @Override
084  default boolean canHandleXmlQName(@NonNull QName qname) {
085    return qname.equals(getRootXmlQName());
086  }
087}