001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.io;
007
008import gov.nist.secauto.metaschema.core.model.IBoundObject;
009import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
010import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler;
011
012import java.io.IOException;
013
014import edu.umd.cs.findbugs.annotations.NonNull;
015
016public interface IWritingContext<WRITER> {
017  /**
018   * Get the writer associated with the writing context.
019   *
020   * @return the writer
021   */
022  @NonNull
023  WRITER getWriter();
024
025  /**
026   * Write the data described by the provided {@code targetObject} as an XML
027   * element.
028   *
029   * @param definition
030   *          the bound Module definition describing the data to write
031   * @param targetObject
032   *          the Java object data to write
033   * @throws IOException
034   *           if an error occurred while writing
035   */
036  void write(
037      @NonNull IBoundDefinitionModelComplex definition,
038      @NonNull IBoundObject targetObject) throws IOException;
039
040  @FunctionalInterface
041  interface ObjectWriter<T extends IFeatureComplexItemValueHandler> {
042
043    void accept(@NonNull IBoundObject parentItem, @NonNull T handler) throws IOException;
044
045    /**
046     * Perform a series of property write operations, starting first with this
047     * operation and followed by the {@code after} operation.
048     *
049     * @param after
050     *          the secondary property write operation to perform
051     * @return an aggregate property write operation
052     */
053    @NonNull
054    default ObjectWriter<T> andThen(@NonNull ObjectWriter<? super T> after) {
055      return (parentItem, handler) -> {
056        accept(parentItem, handler);
057        after.accept(parentItem, handler);
058      };
059    }
060  }
061}