package com.mongoplus.aggregate.pipeline;

import com.mongodb.lang.Nullable;
import com.mongoplus.support.SFunction;

/**
 * unwind管道可选项
 *
 * @author anwen
 */
public class UnwindOption {

    private Boolean preserveNullAndEmptyArrays;
    private String includeArrayIndex;

    /**
     * 如果为 true，则展开阶段将包含具有空值或空数组的文档
     *
     * @return 保留空值和空数组值或 null
     */
    @Nullable
    public Boolean isPreserveNullAndEmptyArrays() {
        return preserveNullAndEmptyArrays;
    }

    /**
     * 如果展开阶段应包含具有空值或空数组的文档，则设置为 true
     * @param preserveNullAndEmptyArrays 描述展开阶段是否应包含具有空值或空数组的文档的标志
     * @author anwen
     */
    public UnwindOption preserveNullAndEmptyArrays(@Nullable final Boolean preserveNullAndEmptyArrays) {
        this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays;
        return this;
    }

    /**
     * Gets the includeArrayIndex field if set or null
     *
     * @return the includeArrayIndex field if set or null
     */
    @Nullable
    public String getIncludeArrayIndex() {
        return includeArrayIndex;
    }

    /**
     * 设置用于存储展开项的数组索引的字段
     *
     * @param arrayIndexFieldName 用于存储展开项的数组索引的字段
     * @return this
     */
    public UnwindOption includeArrayIndex(@Nullable final String arrayIndexFieldName) {
        this.includeArrayIndex = arrayIndexFieldName;
        return this;
    }

    /**
     * 设置用于存储展开项的数组索引的字段
     *
     * @param arrayIndexFieldName 用于存储展开项的数组索引的字段
     * @return this
     */
    public <T> UnwindOption includeArrayIndex(@Nullable final SFunction<T,?> arrayIndexFieldName) {
        if (arrayIndexFieldName != null) {
            this.includeArrayIndex = arrayIndexFieldName.getFieldNameLine();
        }
        return this;
    }

    @Override
    public String toString() {
        return "UnwindOptions{"
                + "preserveNullAndEmptyArrays=" + preserveNullAndEmptyArrays
                + ", includeArrayIndex='" + includeArrayIndex + '\''
                + '}';
    }

}
