+ * This is meant to be used with try-with-resources, like such: + *
{@code + * try (final LockAccess lockAccess = lockRepository.lock(LockKey.of("your.key"))) { + * // use lock + * } + * }+ * + * @param key Lock key + * @return Lock access. Must be closed. + */ + @Nonnull public LockAccess lock(@Nonnull final LockKey key) { + final Lock lock = this.getLock(key); + lock.lock(); + return new LockAccess(lock); + } + + + public static class LockAccess implements AutoCloseable { + + private final Lock lock; + + private LockAccess(@Nonnull final Lock lock) { + this.lock = lock; + } + + @Override public void close() { + this.lock.unlock(); + } + + } + +}