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.IBoundProperty;
11  
12  import java.io.IOException;
13  import java.util.Collection;
14  
15  import edu.umd.cs.findbugs.annotations.NonNull;
16  
17  public abstract class AbstractProblemHandler implements IProblemHandler {
18  
19    @Override
20    public void handleMissingInstances(
21        IBoundDefinitionModelComplex parentDefinition,
22        IBoundObject targetObject,
23        Collection<? extends IBoundProperty<?>> unhandledInstances) throws IOException {
24      applyDefaults(targetObject, unhandledInstances);
25    }
26  
27    /**
28     * A utility method for applying default values for the provided
29     * {@code unhandledInstances}.
30     *
31     * @param targetObject
32     *          the Java object to apply default values to
33     * @param unhandledInstances
34     *          the collection of unhandled instances to assign default values for
35     * @throws IOException
36     *           if an error occurred while determining the default value for an
37     *           instance
38     */
39    protected static void applyDefaults(
40        @NonNull Object targetObject,
41        @NonNull Collection<? extends IBoundProperty<?>> unhandledInstances) throws IOException {
42      for (IBoundProperty<?> instance : unhandledInstances) {
43        Object value = instance.getResolvedDefaultValue();
44        if (value != null) {
45          instance.setValue(targetObject, value);
46        }
47      }
48    }
49  }