package com.base.orbit.thread;

import org.springframework.core.task.TaskDecorator;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 组合多个spring task包装类
 *
 * @author jhzhu
 * @date 2022-01-27
 */
public class CompositeTaskDecorator implements TaskDecorator {

    private List<TaskDecorator> taskDecorators;

    public CompositeTaskDecorator(List<TaskDecorator> taskDecorators) {
        this.taskDecorators = taskDecorators;
    }

    @Override
    public Runnable decorate(Runnable runnable) {
        if (CollectionUtils.isEmpty(this.taskDecorators)) {
            return runnable;
        }
        Runnable finalRunnable = runnable;
        for (TaskDecorator taskDecorator : this.taskDecorators)  {
            finalRunnable = taskDecorator.decorate(finalRunnable);
        }
        return finalRunnable;
    }

}
