Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
shz.core.lock.Locker Maven / Gradle / Ivy
package shz.core.lock;
import shz.core.NullHelp;
import shz.core.ToSet;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public final class Locker {
private Locker() {
throw new IllegalStateException();
}
public static R apply(boolean readOnly, LockManager manager, Supplier extends R> supplier, long time, TimeUnit unit, String key, Object... keys) {
return readOnly ? manager.applyRead(supplier, time, unit, key, keys) : manager.applyWrite(supplier, time, unit, key, keys);
}
public static R apply(Supplier extends R> supplier, String key, Object... keys) {
return apply(false, ReadWriteLockManager.INSTANCE, supplier, 0L, TimeUnit.MILLISECONDS, key, keys);
}
public static void accept(boolean readOnly, LockManager manager, Runnable runnable, long time, TimeUnit unit, String key, Object... keys) {
if (readOnly) manager.acceptRead(runnable, time, unit, key, keys);
else manager.acceptWrite(runnable, time, unit, key, keys);
}
public static void accept(Runnable runnable, String key, Object... keys) {
accept(false, ReadWriteLockManager.INSTANCE, runnable, 0L, TimeUnit.MILLISECONDS, key, keys);
}
@FunctionalInterface
interface Applies {
T apply(Supplier extends T> s, long time, TimeUnit unit, String key);
}
/**
* @param identity 全局唯一标识(业务标识)
*/
public static R applies(boolean readOnly, LockManager manager, Supplier extends R> supplier, String identity, Key key, Key... keys) {
NullHelp.requireNonNull(key);
Applies applies = readOnly ? manager::applyRead : manager::applyWrite;
Supplier extends R> s = supplier;
int len = NullHelp.length(keys);
if (len > 0) {
NullHelp.requireNonAnyNull(keys);
Set set = ToSet.explicitCollect(Arrays.stream(keys).map(Key::getKey), len);
NullHelp.requireNon(set.size() != len || set.contains(key.getKey()), "存在死锁风险,请确保锁键唯一");
for (int i = len - 1; i >= 0; --i) {
if (NullHelp.isBlank(keys[i].getValue())) continue;
Supplier extends R> t = s;
int idx = i;
s = () -> applies.apply(t, keys[idx].getTime(), keys[idx].getUnit(), identity + "-" + keys[idx].getKey() + "-" + keys[idx].getValue());
}
}
return applies.apply(s, key.getTime(), key.getUnit(), identity + "-" + key.getKey() + "-" + key.getValue());
}
public static R applies(Supplier extends R> supplier, String identity, Key key, Key... keys) {
return applies(false, ReadWriteLockManager.INSTANCE, supplier, identity, key, keys);
}
@FunctionalInterface
interface Accepts {
void apply(Runnable r, long time, TimeUnit unit, String key);
}
public static void accepts(boolean readOnly, LockManager manager, Runnable runnable, String identity, Key key, Key... keys) {
NullHelp.requireNonNull(key);
Accepts accepts = readOnly ? manager::acceptRead : manager::acceptWrite;
Runnable r = runnable;
int len = NullHelp.length(keys);
if (len > 0) {
NullHelp.requireNonAnyNull(keys);
Set set = ToSet.explicitCollect(Arrays.stream(keys).map(Key::getKey), len);
NullHelp.requireNon(set.size() != len || set.contains(key.getKey()), "存在死锁风险,请确保锁键唯一");
for (int i = len - 1; i >= 0; --i) {
if (NullHelp.isBlank(keys[i].getValue())) continue;
Runnable t = r;
int idx = i;
r = () -> accepts.apply(t, keys[idx].getTime(), keys[idx].getUnit(), identity + "-" + keys[idx].getKey() + "-" + keys[idx].getValue());
}
}
accepts.apply(r, key.getTime(), key.getUnit(), identity + "-" + key.getKey() + "-" + key.getValue());
}
public static void accepts(Runnable runnable, String identity, Key key, Key... keys) {
accepts(false, ReadWriteLockManager.INSTANCE, runnable, identity, key, keys);
}
}