com.github.cosycode.ext.hub.OnceExecClosureProxy Maven / Gradle / Ivy
Show all versions of extend-mod Show documentation
package com.github.cosycode.ext.hub;
import lombok.NonNull;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
/**
* Description :
*
* created in 2021/4/6
*
* @author CPF
**/
public class OnceExecClosureProxy extends AbstractClosureProxy {
private final Lock lock = new ReentrantLock();
private T skip;
public OnceExecClosureProxy(@NonNull T then) {
super(then);
}
public OnceExecClosureProxy(@NonNull T then, @NonNull BiFunction function) {
super(then, function);
}
public OnceExecClosureProxy(@NonNull T then, @NonNull BiConsumer biConsumer) {
super(then, biConsumer);
}
public static T of(T then) {
return new OnceExecClosureProxy<>(then).proxy();
}
public OnceExecClosureProxy skip(T skip) {
this.skip = skip;
return this;
}
@Override
public R closureFunction(P params) {
if (lock.tryLock()) {
try {
if (functional != null) {
return biFunction.apply(functional, params);
}
} finally {
lock.unlock();
}
} else {
if (skip != null) {
return biFunction.apply(skip, params);
}
}
return null;
}
}