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.model.IBoundObject;
009import dev.metaschema.databind.model.IBoundInstanceFlag;
010import dev.metaschema.databind.model.IBoundInstanceModel;
011import dev.metaschema.databind.model.IBoundInstanceModelNamed;
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014/**
015 * An abstract base class for reading model instance collections.
016 * <p>
017 * This class provides the framework for reading collections of items during
018 * deserialization, with support for different collection types.
019 *
020 * @param <ITEM>
021 *          the Java type of items being read
022 */
023public abstract class AbstractModelInstanceReadHandler<ITEM> implements IModelInstanceReadHandler<ITEM> {
024  @NonNull
025  private final IBoundInstanceModel<ITEM> instance;
026  @NonNull
027  private final IBoundObject parentObject;
028
029  /**
030   * Construct a new read handler for the provided model instance.
031   *
032   * @param instance
033   *          the model instance to read
034   * @param parentObject
035   *          the parent object that will contain the read data
036   */
037  protected AbstractModelInstanceReadHandler(
038      @NonNull IBoundInstanceModel<ITEM> instance,
039      @NonNull IBoundObject parentObject) {
040    this.instance = instance;
041    this.parentObject = parentObject;
042  }
043
044  /**
045   * Get the model instance associated with this handler.
046   *
047   * @return the collection information
048   */
049  @NonNull
050  public IBoundInstanceModel<ITEM> getInstance() {
051    return instance;
052  }
053
054  /**
055   * Get the collection Java type information associated with this handler.
056   *
057   * @return the collection information
058   */
059  @NonNull
060  public IModelInstanceCollectionInfo<ITEM> getCollectionInfo() {
061    return getInstance().getCollectionInfo();
062  }
063
064  /**
065   * Get the object onto which parsed data will be stored.
066   *
067   * @return the parentObject
068   */
069  @NonNull
070  public IBoundObject getParentObject() {
071    return parentObject;
072  }
073
074  @Override
075  public String getJsonKeyFlagName() {
076    IBoundInstanceModel<?> instance = getInstance();
077    String retval = null;
078    if (instance instanceof IBoundInstanceModelNamed) {
079      IBoundInstanceFlag jsonKey = ((IBoundInstanceModelNamed<?>) instance).getEffectiveJsonKey();
080      if (jsonKey != null) {
081        retval = jsonKey.getEffectiveName();
082      }
083    }
084    return retval;
085  }
086}