1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model.info;
7   
8   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
9   import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel;
10  
11  import java.io.IOException;
12  import java.util.Collection;
13  import java.util.List;
14  import java.util.Map;
15  
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  
18  public abstract class AbstractModelInstanceWriteHandler<ITEM>
19      implements IModelInstanceWriteHandler<ITEM> {
20    @NonNull
21    private final IBoundInstanceModel<ITEM> instance;
22  
23    public AbstractModelInstanceWriteHandler(@NonNull IBoundInstanceModel<ITEM> instance) {
24      this.instance = instance;
25    }
26  
27    /**
28     * Get the associated instance.
29     *
30     * @return the instance
31     */
32    public IBoundInstanceModel<ITEM> getInstance() {
33      return instance;
34    }
35  
36    /**
37     * Get the collection information.
38     *
39     * @return the info
40     */
41    @NonNull
42    public IModelInstanceCollectionInfo<ITEM> getCollectionInfo() {
43      return instance.getCollectionInfo();
44    }
45  
46    @Override
47    public void writeList(List<ITEM> items) throws IOException {
48      writeCollection(items);
49    }
50  
51    @Override
52    public void writeMap(Map<String, ITEM> items) throws IOException {
53      writeCollection(ObjectUtils.notNull(items.values()));
54    }
55  
56    private void writeCollection(@NonNull Collection<ITEM> items) throws IOException {
57      for (ITEM item : items) {
58        writeItem(ObjectUtils.requireNonNull(item));
59      }
60    }
61  }