001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.databind.model.info;
007
008import dev.metaschema.core.datatype.IDataTypeAdapter;
009import dev.metaschema.core.model.IBoundObject;
010import dev.metaschema.databind.io.BindingException;
011import dev.metaschema.databind.model.IValuedMutable;
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014/**
015 * A feature interface for handling scalar item values during binding
016 * operations.
017 * <p>
018 * Scalar items have simple values that can be converted directly by a data type
019 * adapter.
020 */
021public interface IFeatureScalarItemValueHandler
022    extends IItemValueHandler<Object>, IValuedMutable {
023
024  /**
025   * Apply the string value.
026   * <p>
027   * This first parses the value using the underlying data type implementation and
028   * then applies the parsed value.
029   *
030   * @param parent
031   *          the parent object to apply the value to
032   * @param text
033   *          the value to parse
034   * @throws IllegalArgumentException
035   *           if the text was malformed
036   * @see #getJavaTypeAdapter()
037   */
038  default void setValue(@NonNull Object parent, @NonNull String text) {
039    Object item = getValueFromString(text);
040    setValue(parent, item);
041  }
042
043  /**
044   * Parse a string value using the underlying data type implementation.
045   *
046   * @param text
047   *          the value to parse
048   * @return the parsed value
049   * @throws IllegalArgumentException
050   *           if the text was malformed
051   * @see #getJavaTypeAdapter()
052   */
053  default Object getValueFromString(@NonNull String text) {
054    return getJavaTypeAdapter().parse(text);
055  }
056
057  /**
058   * Get the data type adapter supporting the scalar value.
059   *
060   * @return the data type adapter
061   */
062  @NonNull
063  IDataTypeAdapter<?> getJavaTypeAdapter();
064
065  @Override
066  default Object deepCopyItem(Object source, IBoundObject parentInstance) throws BindingException {
067    return getJavaTypeAdapter().copy(source);
068  }
069}