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

shz.core.lock.Locker Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
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 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 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 s, long time, TimeUnit unit, String key);
    }

    /**
     * @param identity 全局唯一标识(业务标识)
     */
    public static  R applies(boolean readOnly, LockManager manager, Supplier supplier, String identity, Key key, Key... keys) {
        NullHelp.requireNonNull(key);
        Applies applies = readOnly ? manager::applyRead : manager::applyWrite;
        Supplier 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 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 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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy