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.IBoundProperty;
011
012import java.io.IOException;
013import java.util.Collection;
014
015import edu.umd.cs.findbugs.annotations.NonNull;
016
017public abstract class AbstractProblemHandler implements IProblemHandler {
018
019  @Override
020  public void handleMissingInstances(
021      IBoundDefinitionModelComplex parentDefinition,
022      IBoundObject targetObject,
023      Collection<? extends IBoundProperty<?>> unhandledInstances) throws IOException {
024    applyDefaults(targetObject, unhandledInstances);
025  }
026
027  /**
028   * A utility method for applying default values for the provided
029   * {@code unhandledInstances}.
030   *
031   * @param targetObject
032   *          the Java object to apply default values to
033   * @param unhandledInstances
034   *          the collection of unhandled instances to assign default values for
035   * @throws IOException
036   *           if an error occurred while determining the default value for an
037   *           instance
038   */
039  protected static void applyDefaults(
040      @NonNull Object targetObject,
041      @NonNull Collection<? extends IBoundProperty<?>> unhandledInstances) throws IOException {
042    for (IBoundProperty<?> instance : unhandledInstances) {
043      Object value = instance.getResolvedDefaultValue();
044      if (value != null) {
045        instance.setValue(targetObject, value);
046      }
047    }
048  }
049}