Java教程

java 方法作为参数封装在map

本文主要是介绍java 方法作为参数封装在map,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.exe.competency.service.impl;

import com.exe.competency.domain.RuleParam;
import com.exe.competency.domain.RuleResult;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
public class RuleContext {

    @Inject
    LegalPersonServer legalPersonServer;
    @Inject
    AssignPostServer assignPostServer;
    @Inject
    AssignTagServer assignTagServer;
    @Inject
    AssignPositionServer assignPositionServer;
    @Inject
    ParentLegalPersonServer parentLegalPersonServer;
    @Inject
    AssignDeptAndPostServer assignDeptAndPostServer;
    @Inject
    LeaderServer leaderServer;


    Map<Integer, Function<RuleParam, RuleResult>> funcMap = new HashMap<>();

    @PostConstruct
    public void init() {
        funcMap.put(1, RuleParam -> legalPersonServer.getUserList(RuleParam));
        funcMap.put(2, RuleParam -> assignPostServer.getUserList(RuleParam));
        funcMap.put(3, RuleParam -> assignTagServer.getUserList(RuleParam));
        funcMap.put(4, RuleParam -> assignPositionServer.getUserList(RuleParam));
        funcMap.put(5, RuleParam -> parentLegalPersonServer.getUserList(RuleParam));
        funcMap.put(6, RuleParam -> assignDeptAndPostServer.getUserList(RuleParam));
        funcMap.put(7, RuleParam -> leaderServer.getUserList(RuleParam));
    }

    public RuleResult getUserList(int kind, String userId, List<String> fromIds, int type) {
        Function<RuleParam, RuleResult> stringListFunction = funcMap.get(kind);
        if (stringListFunction != null) {
            RuleParam rule = new RuleParam();
            rule.setUserId(userId);
            rule.setFromId(fromIds);
            rule.setType(type);
            return stringListFunction.apply(rule);
        }
        return null;
    }

}

 

这篇关于java 方法作为参数封装在map的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!