package com.bocloud.cmp.provider.convertor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: BeanConvertor<T>
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2019/7/26 18:05
 */
public interface BeanConvertor<T, K> {

    default List<T> convertList(List<? extends K> models) {
        return Optional.ofNullable(models)
                .map(list -> list
                        .stream()
                        .map(this::convertModel)
                        .collect(Collectors.toList()))
                .orElse(new ArrayList<>());
    }

    default T convertModel(K model) {
        return null;
    }

    //需要形参时 使用
    default List<T> convertListParams(List<? extends K> models, String... params) {
        return Optional.ofNullable(models)
                .map(list -> list
                        .stream()
                        .map(model -> this.convertModelParams(model, params))
                        .collect(Collectors.toList()))
                .orElse(new ArrayList<>());
    }

    //需要形参时 使用
    default T convertModelParams(K model, String... params) {
        return null;
    }

}
