All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.criteria.action.AssemAction Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.criteria.action;

import com.github.dennisit.vplus.data.criteria.AssemCriteria;
import com.github.dennisit.vplus.data.criteria.AtomicCriteria;
import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by Elon.su on 17/9/16.
 */
public class AssemAction {

    private AssemAction(){}

    /**
     * assemble,assem assemblyList for baseDataList,
     * if baseDataList is empty, return empty list,
     * if element in assemblyList, not in baseDataList, skip it.
     * @param baseDataList target list
     * @param assembleList extend list
     * @param  基础数据集
     * @param  目标数据集
     * @return assembled list
     */
    public static > List assem(List baseDataList, List assembleList) {

        Map baseMap = Optional.ofNullable(baseDataList).orElse(Lists.newArrayList())
                .stream().collect(Collectors.toMap(K::uniquely, Function.identity()));

        Optional.ofNullable(assembleList).orElse(Lists.newArrayList())
                .stream().forEach(x ->{
                    K k = baseMap.get(x.uniquely());
                    if(null != k) {
                        x.assemble(k);
                    }
                }
        );
        return baseDataList;
    }

    /**
     * 示例行为
     */
    @Data
    static class AssemMock implements AssemCriteria {

        private Integer id;
        private String name;
        private Integer age;

        public AssemMock() {

        }

        public AssemMock(Integer id) {
            this.id = id;
        }

        public AssemMock(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        public AssemMock(Integer id, Integer age) {
            this.id = id;
            this.age = age;
        }

        public AssemMock(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        /**
         * define unique key
         * @return
         */
        @Override
        public String uniquely() {
            return String.valueOf(id);
        }

        /**
         * define action for assembly
         * @param assemMock
         * @return
         */
        @Override
        public AssemMock assemble(AssemMock assemMock) {
            if(null != getId()){
                assemMock.setId(getId());
            }
            if(null != getName()){
                assemMock.setName(getName());
            }
            if(null != getAge()){
                assemMock.setAge(getAge());
            }
            return assemMock;
        }

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy