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

com.github.phantomthief.util.MoreLocks Maven / Gradle / Ivy

There is a newer version: 0.1.55
Show newest version
package com.github.phantomthief.util;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.function.Supplier;

/**
 * @author w.vela
 */
public final class MoreLocks {

    private MoreLocks() {
        throw new UnsupportedOperationException();
    }

    public static  void runWithLock(Lock lock, ThrowableRunnable runnable)
            throws X {
        supplyWithLock(lock, wrapAsRunnable(runnable));
    }

    public static  T supplyWithLock(Lock lock,
            ThrowableSupplier supplier) throws X {
        lock.lock();
        try {
            return supplier.get();
        } finally {
            lock.unlock();
        }
    }

    public static  void runWithTryLock(Lock lock, long time, TimeUnit unit,
            ThrowableRunnable runnable) throws X, InterruptedException {
        runWithTryLock(lock, time, unit, runnable, null);
    }

    public static  void runWithTryLock(Lock lock, long time, TimeUnit unit,
            ThrowableRunnable runnable, Runnable withoutAcquiredLock)
            throws X, InterruptedException {
        supplyWithTryLock(lock, time, unit, wrapAsRunnable(runnable), () -> {
            if (withoutAcquiredLock != null) {
                withoutAcquiredLock.run();
            }
            return null;
        });
    }

    public static  T supplyWithTryLock(Lock lock, long time, TimeUnit unit,
            ThrowableSupplier supplier) throws X, InterruptedException {
        return supplyWithTryLock(lock, time, unit, supplier, null);
    }

    public static  T supplyWithTryLock(Lock lock, long time, TimeUnit unit,
            ThrowableSupplier supplier, Supplier withoutAcquiredLock)
            throws X, InterruptedException {
        if (lock.tryLock(time, unit)) {
            try {
                return supplier.get();
            } finally {
                lock.unlock();
            }
        } else {
            if (withoutAcquiredLock != null) {
                return withoutAcquiredLock.get();
            } else {
                return null;
            }
        }
    }

    public static  void runWithTryLock(Lock lock,
            ThrowableRunnable runnable) throws X {
        runWithTryLock(lock, runnable, null);
    }

    public static  void runWithTryLock(Lock lock,
            ThrowableRunnable runnable, Runnable withoutAcquiredLock) throws X {
        supplyWithTryLock(lock, wrapAsRunnable(runnable), () -> {
            if (withoutAcquiredLock != null) {
                withoutAcquiredLock.run();
            }
            return null;
        });
    }

    public static  T supplyWithTryLock(Lock lock,
            ThrowableSupplier supplier) throws X {
        return supplyWithTryLock(lock, supplier, null);
    }

    public static  T supplyWithTryLock(Lock lock,
            ThrowableSupplier supplier, Supplier withoutAcquiredLock) throws X {
        if (lock.tryLock()) {
            try {
                return supplier.get();
            } finally {
                lock.unlock();
            }
        } else {
            if (withoutAcquiredLock != null) {
                return withoutAcquiredLock.get();
            } else {
                return null;
            }
        }
    }

    private static  ThrowableSupplier
            wrapAsRunnable(ThrowableRunnable runnable) {
        return () -> {
            runnable.run();
            return null;
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy