1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.io;
7   
8   import gov.nist.secauto.metaschema.core.model.IBoundObject;
9   import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
10  import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler;
11  
12  import java.io.IOException;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  
16  public interface IWritingContext<WRITER> {
17    /**
18     * Get the writer associated with the writing context.
19     *
20     * @return the writer
21     */
22    @NonNull
23    WRITER getWriter();
24  
25    /**
26     * Write the data described by the provided {@code targetObject} as an XML
27     * element.
28     *
29     * @param definition
30     *          the bound Module definition describing the data to write
31     * @param targetObject
32     *          the Java object data to write
33     * @throws IOException
34     *           if an error occurred while writing
35     */
36    void write(
37        @NonNull IBoundDefinitionModelComplex definition,
38        @NonNull IBoundObject targetObject) throws IOException;
39  
40    @FunctionalInterface
41    interface ObjectWriter<T extends IFeatureComplexItemValueHandler> {
42  
43      void accept(@NonNull IBoundObject parentItem, @NonNull T handler) throws IOException;
44  
45      /**
46       * Perform a series of property write operations, starting first with this
47       * operation and followed by the {@code after} operation.
48       *
49       * @param after
50       *          the secondary property write operation to perform
51       * @return an aggregate property write operation
52       */
53      @NonNull
54      default ObjectWriter<T> andThen(@NonNull ObjectWriter<? super T> after) {
55        return (parentItem, handler) -> {
56          accept(parentItem, handler);
57          after.accept(parentItem, handler);
58        };
59      }
60    }
61  }