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.model.IChoiceInstance;
011import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly;
012import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
013import gov.nist.secauto.metaschema.core.util.CollectionUtil;
014import gov.nist.secauto.metaschema.core.util.ObjectUtils;
015import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
016import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021import java.util.function.Function;
022import java.util.function.Predicate;
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025
026import edu.umd.cs.findbugs.annotations.NonNull;
027import edu.umd.cs.findbugs.annotations.Nullable;
028
029/**
030 * Represents an assembly definition bound to a Java class.
031 */
032public interface IBoundDefinitionModelAssembly
033    extends IBoundDefinitionModelComplex, IAssemblyDefinition,
034    IFeatureContainerModelAssembly<
035        IBoundInstanceModel<?>,
036        IBoundInstanceModelNamed<?>,
037        IBoundInstanceModelField<?>,
038        IBoundInstanceModelAssembly,
039        IChoiceInstance,
040        IBoundInstanceModelChoiceGroup> { // , IBoundContainerModelAssembly
041
042  // Assembly Definition Features
043  // ============================
044  @Override
045  @NonNull
046  default IBoundDefinitionModelAssembly getOwningDefinition() {
047    return this;
048  }
049
050  @Override
051  @NonNull
052  default IBoundDefinitionModelAssembly getDefinition() {
053    return this;
054  }
055
056  @Override
057  @Nullable
058  default IBoundInstanceModelAssembly getInlineInstance() {
059    // never inline
060    return null;
061  }
062
063  @Override
064  @NonNull
065  default List<IChoiceInstance> getChoiceInstances() {
066    // not supported
067    return CollectionUtil.emptyList();
068  }
069
070  @Override
071  @NonNull
072  default Map<String, IBoundProperty<?>> getJsonProperties(@Nullable Predicate<IBoundInstanceFlag> flagFilter) {
073    Stream<? extends IBoundInstanceFlag> flagStream = getFlagInstances().stream();
074
075    if (flagFilter != null) {
076      flagStream = flagStream.filter(flagFilter);
077    }
078
079    return ObjectUtils.notNull(Stream.concat(flagStream, getModelInstances().stream())
080        .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity())));
081  }
082
083  @Override
084  @NonNull
085  default IBoundObject readItem(@Nullable IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
086    return handler.readItemAssembly(parent, this);
087  }
088
089  @Override
090  default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
091    handler.writeItemAssembly(item, this);
092  }
093
094  @Override
095  default boolean canHandleXmlQName(@NonNull IEnhancedQName qname) {
096    return qname.equals(getRootQName());
097  }
098}