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

com.github.cosycode.ext.hub.OnceExecutes Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.hub;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Description : 
 * 

* created in 2020/12/10 *

* * @author CPF **/ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class OnceExecutes { public static Consumer consumer(@NonNull Consumer then) { OnceExecutorForConsumer onceExecutor = new OnceExecutorForConsumer<>(then); return onceExecutor::onceExe; } public static Consumer consumer(@NonNull Consumer then, Consumer skip) { OnceExecutorForConsumer onceExecutor = new OnceExecutorForConsumer<>(then); onceExecutor.setSkip(skip); return onceExecutor::onceExe; } public static Runnable runnable(@NonNull Runnable then) { OnceExecutorForRunnable onceExecutor = new OnceExecutorForRunnable(then); return onceExecutor::onceExe; } public static Runnable runnable(@NonNull Runnable then, Runnable skip) { OnceExecutorForRunnable onceExecutor = new OnceExecutorForRunnable(then); onceExecutor.setSkip(skip); return onceExecutor::onceExe; } public static Function function(@NonNull Function then) { OnceExecutorForFunction onceExecutor = new OnceExecutorForFunction<>(then); return onceExecutor::onceExe; } public static Function function(@NonNull Function then, Function skip) { OnceExecutorForFunction onceExecutor = new OnceExecutorForFunction<>(then); onceExecutor.setSkip(skip); return onceExecutor::onceExe; } public static Supplier supplier(@NonNull Supplier then) { OnceExecutorForSupplier onceExecutor = new OnceExecutorForSupplier<>(then); return onceExecutor::onceExe; } public static Supplier supplier(@NonNull Supplier then, Supplier skip) { OnceExecutorForSupplier onceExecutor = new OnceExecutorForSupplier<>(then); onceExecutor.setSkip(skip); return onceExecutor::onceExe; } public static Function exec(@NonNull T then, @NonNull BiFunction function) { OnceExecutorForCommon onceExecutor = new OnceExecutorForCommon<>(then, null, function); return onceExecutor::onceExe; } public static Function exec(@NonNull T then, T skip, @NonNull BiFunction function) { OnceExecutorForCommon onceExecutor = new OnceExecutorForCommon<>(then, skip, function); return onceExecutor::onceExe; } public static class OnceExecutorForCommon { private final Lock lock = new ReentrantLock(); private final T then; private final BiFunction biFunction; @Setter private T skip; public OnceExecutorForCommon(T then, BiFunction function) { this.then = then; this.biFunction = function; } public OnceExecutorForCommon(T then, T skip, BiFunction function) { this.then = then; this.skip = skip; this.biFunction = function; } public R onceExe(P params) { if (lock.tryLock()) { try { if (then != null) { return biFunction.apply(then, params); } } finally { lock.unlock(); } } else { if (skip != null) { return biFunction.apply(skip, params); } } return null; } } public static class OnceExecutorForRunnable { private final Lock lock = new ReentrantLock(); private final Runnable then; @Setter private Runnable skip; public OnceExecutorForRunnable(Runnable then) { this.then = then; } public void onceExe() { if (lock.tryLock()) { try { if (then != null) { then.run(); } } finally { lock.unlock(); } } else { if (skip != null) { skip.run(); } } } } public static class OnceExecutorForConsumer { private final Lock lock = new ReentrantLock(); private final Consumer then; @Setter private Consumer skip; public OnceExecutorForConsumer(Consumer then) { this.then = then; } public void onceExe(T e) { if (lock.tryLock()) { try { if (then != null) { then.accept(e); } } finally { lock.unlock(); } } else { if (skip != null) { skip.accept(e); } } } } public static class OnceExecutorForSupplier { private final Lock lock = new ReentrantLock(); private final Supplier then; @Setter private Supplier skip; public OnceExecutorForSupplier(Supplier then) { this.then = then; } public T onceExe() { if (lock.tryLock()) { try { return then.get(); } finally { lock.unlock(); } } else { if (skip != null) { return skip.get(); } } return null; } } public static class OnceExecutorForFunction { private final Lock lock = new ReentrantLock(); private final Function then; @Setter private Function skip; public OnceExecutorForFunction(Function then) { this.then = then; } public R onceExe(T e) { if (lock.tryLock()) { try { return then.apply(e); } finally { lock.unlock(); } } else { if (skip != null) { return skip.apply(e); } } return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy