package com.alibaba.csp.sentinel.dashboard.rule.nacos.util;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;

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

/**
 * @author Clay
 * @date 2022/11/13
 */
public class NacosUtils {

    /**
     * 找到自己
     *
     * @param app
     * @param ip
     * @param port
     * @param entity
     * @return
     */
    public static boolean oneSelf(String app, String ip, Integer port, RuleEntity entity) {
        return app.equals(entity.getApp()) && ip.equals(entity.getIp()) && port.equals(entity.getPort());
    }

    /**
     * 从list中找到自己
     *
     * @param app
     * @param ip
     * @param port
     * @param rules
     * @param <T>
     * @return
     */
    public static <T> List<T> filterSelf(String app, String ip, Integer port, List<T> rules) {
        return rules.stream().filter(entity -> oneSelf(app, ip, port, (RuleEntity) entity))
                .collect(Collectors.toList());
    }

    /**
     * 从list中找起到
     *
     * @param app
     * @param ip
     * @param port
     * @param rules
     * @param <T>
     * @return
     */
    public static <T> List<T> filterOther(String app, String ip, Integer port, List<T> rules) {
        return rules.stream().filter(entity -> !oneSelf(app, ip, port, (RuleEntity) entity))
                .collect(Collectors.toList());
    }
}
