package cn.pluss.platform.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * @author  Djh
 * @param <T> 类型
 *   便于从数据中取出需要的数据
 */
@Data
@RequiredArgsConstructor
public class DataFactory<T> {

    private List<T> srcList;

    public T get(T condition) {
        if (srcList == null || srcList.size() == 0) {
            srcList = configuration.getData();
        }

        for (T item: srcList) {
            if (configuration.compare(item, condition)) {
                return item;
            }
        }

        return null;
    }

    private final Configuration<T> configuration;


    public interface Configuration<T> {

        /**
         * 获取数据
         * @return
         */
        List<T> getData();

        /**
         * 数据比较
         * @param src
         * @param tar
         * @return
         */
        boolean compare(T src, T tar);
    }
}
