1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.model.info;
7   
8   import dev.metaschema.core.datatype.IDataTypeAdapter;
9   import dev.metaschema.core.model.IBoundObject;
10  import dev.metaschema.databind.io.BindingException;
11  import dev.metaschema.databind.model.IValuedMutable;
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  /**
15   * A feature interface for handling scalar item values during binding
16   * operations.
17   * <p>
18   * Scalar items have simple values that can be converted directly by a data type
19   * adapter.
20   */
21  public interface IFeatureScalarItemValueHandler
22      extends IItemValueHandler<Object>, IValuedMutable {
23  
24    /**
25     * Apply the string value.
26     * <p>
27     * This first parses the value using the underlying data type implementation and
28     * then applies the parsed value.
29     *
30     * @param parent
31     *          the parent object to apply the value to
32     * @param text
33     *          the value to parse
34     * @throws IllegalArgumentException
35     *           if the text was malformed
36     * @see #getJavaTypeAdapter()
37     */
38    default void setValue(@NonNull Object parent, @NonNull String text) {
39      Object item = getValueFromString(text);
40      setValue(parent, item);
41    }
42  
43    /**
44     * Parse a string value using the underlying data type implementation.
45     *
46     * @param text
47     *          the value to parse
48     * @return the parsed value
49     * @throws IllegalArgumentException
50     *           if the text was malformed
51     * @see #getJavaTypeAdapter()
52     */
53    default Object getValueFromString(@NonNull String text) {
54      return getJavaTypeAdapter().parse(text);
55    }
56  
57    /**
58     * Get the data type adapter supporting the scalar value.
59     *
60     * @return the data type adapter
61     */
62    @NonNull
63    IDataTypeAdapter<?> getJavaTypeAdapter();
64  
65    @Override
66    default Object deepCopyItem(Object source, IBoundObject parentInstance) throws BindingException {
67      return getJavaTypeAdapter().copy(source);
68    }
69  }