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

com.taotao.boot.sms.common.loadbalancer.AbstractLoadBalancer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.sms.common.loadbalancer;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;

/**
 * random Load Balancer
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:52:49
 */
public abstract class AbstractLoadBalancer implements ILoadBalancer {

    protected final List> targetList;

    /** instantiation Load Balancer with CopyOnWriteArrayList */
    public AbstractLoadBalancer() {
        this(new CopyOnWriteArrayList<>());
    }

    /**
     * instantiation Load Balancer with appoint list
     *
     * @param targetList target object list
     */
    public AbstractLoadBalancer(List> targetList) {
        this.targetList = targetList;
    }

    @Override
    public void addTargetWrapper(TargetWrapper wrapper) {
        if (wrapper == null) {
            return;
        }

        if (!targetList.contains(wrapper)) {
            targetList.add(wrapper);
            afterAdd(wrapper);
        }
    }

    protected void afterAdd(TargetWrapper wrapper) {}

    @Override
    public void removeTargetWrapper(TargetWrapper wrapper) {
        if (wrapper == null) {
            return;
        }

        targetList.remove(wrapper);
        afterRemove(wrapper);
    }

    protected void afterRemove(TargetWrapper wrapper) {}

    @Override
    public void clear() {
        targetList.clear();
    }

    @Override
    public void setWeight(T target, int weight) {}

    @Override
    public T choose(Predicate predicate, C chooseReferenceObject) {
        List> activeTargetList;

        if (predicate == null) {
            activeTargetList =
                    targetList.stream().filter(TargetWrapper::isActive).toList();
        } else {
            activeTargetList = targetList.stream()
                    .filter(TargetWrapper::isActive)
                    .filter(wrapper -> predicate.test(wrapper.getTarget()))
                    .toList();
        }

        if (activeTargetList.isEmpty()) {
            return null;
        }

        return choose0(activeTargetList, chooseReferenceObject);
    }

    /**
     * select target object by rule implement
     *
     * @param activeTargetList active target object List
     * @param chooseReferenceObject choose reference object
     * @return target object
     */
    protected abstract T choose0(List> activeTargetList, C chooseReferenceObject);

    @Override
    public void markReachable(TargetWrapper wrapper) {
        if (wrapper == null) {
            return;
        }

        targetList.stream().filter(wrapper::equals).forEach(item -> item.setActive(true));
    }

    @Override
    public void markDown(TargetWrapper wrapper) {
        if (wrapper == null) {
            return;
        }

        targetList.stream().filter(wrapper::equals).forEach(item -> item.setActive(false));
    }

    @Override
    public List> getTargetWrappers(Boolean active) {
        List> wrappers;
        if (active == null) {
            wrappers = targetList;
        } else {
            wrappers = targetList.stream()
                    .filter(wrapper -> wrapper.isActive() == active)
                    .toList();
        }

        return Collections.unmodifiableList(wrappers);
    }

    @Override
    public List getTargets(Boolean active) {
        List targets;
        if (active == null) {
            targets = targetList.stream().map(TargetWrapper::getTarget).toList();
        } else {
            targets = targetList.stream()
                    .filter(wrapper -> wrapper.isActive() == active)
                    .map(TargetWrapper::getTarget)
                    .toList();
        }

        return Collections.unmodifiableList(targets);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy