001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.model.info;
007
008import gov.nist.secauto.metaschema.core.util.ObjectUtils;
009import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel;
010
011import java.io.IOException;
012import java.util.Collection;
013import java.util.List;
014import java.util.Map;
015
016import edu.umd.cs.findbugs.annotations.NonNull;
017
018public abstract class AbstractModelInstanceWriteHandler<ITEM>
019    implements IModelInstanceWriteHandler<ITEM> {
020  @NonNull
021  private final IBoundInstanceModel<ITEM> instance;
022
023  public AbstractModelInstanceWriteHandler(@NonNull IBoundInstanceModel<ITEM> instance) {
024    this.instance = instance;
025  }
026
027  /**
028   * Get the associated instance.
029   *
030   * @return the instance
031   */
032  public IBoundInstanceModel<ITEM> getInstance() {
033    return instance;
034  }
035
036  /**
037   * Get the collection information.
038   *
039   * @return the info
040   */
041  @NonNull
042  public IModelInstanceCollectionInfo<ITEM> getCollectionInfo() {
043    return instance.getCollectionInfo();
044  }
045
046  @Override
047  public void writeList(List<ITEM> items) throws IOException {
048    writeCollection(items);
049  }
050
051  @Override
052  public void writeMap(Map<String, ITEM> items) throws IOException {
053    writeCollection(ObjectUtils.notNull(items.values()));
054  }
055
056  private void writeCollection(@NonNull Collection<ITEM> items) throws IOException {
057    for (ITEM item : items) {
058      writeItem(ObjectUtils.requireNonNull(item));
059    }
060  }
061}