com.helger.commons.concurrent.SimpleLock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.8+ Library with tons of utility classes required in all projects
/*
* Copyright (C) 2014-2022 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.commons.concurrent;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.callback.IThrowingRunnable;
import com.helger.commons.functional.IThrowingSupplier;
/**
* This is an extension around {@link ReentrantLock} that allows for easy usage
* with Java 8 :) See {@link #locked(Runnable)} and {@link #locked(Runnable)}
* method. Also methods for suppliers are available.
*
* @author Philip Helger
*/
public class SimpleLock extends ReentrantLock
{
/**
* Default constructor creating a default {@link ReentrantLock}
*/
public SimpleLock ()
{}
/**
* Constructor creating a {@link ReentrantLock} with the provided fairness
*
* @param bFair
* true
if this lock should use a fair ordering policy
*/
public SimpleLock (final boolean bFair)
{
super (bFair);
}
/**
* Execute the provided runnable in a read lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
*/
public void locked (@Nonnull final Runnable aRunnable)
{
ValueEnforcer.notNull (aRunnable, "Runnable");
lock ();
try
{
aRunnable.run ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided runnable in a read lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
* @throws EXTYPE
* If the callable throws the exception
* @param
* Exception type to be thrown
*/
public void lockedThrowing (@Nonnull final IThrowingRunnable aRunnable) throws EXTYPE
{
ValueEnforcer.notNull (aRunnable, "Runnable");
lock ();
try
{
aRunnable.run ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock. Note: no
* nullable/non-nullable can be assumed.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @param
* Return type
*/
public T lockedGet (@Nonnull final Supplier extends T> aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.get ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock. Note: no
* nullable/non-nullable can be assumed.
*
* @param aCallable
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @throws EXTYPE
* If the callable throws the exception
* @param
* Return type
* @param
* Exception type to be thrown
*/
public T lockedGetThrowing (@Nonnull final IThrowingSupplier extends T, EXTYPE> aCallable) throws EXTYPE
{
ValueEnforcer.notNull (aCallable, "Callable");
lock ();
try
{
return aCallable.get ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
*/
public boolean lockedBoolean (@Nonnull final BooleanSupplier aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.getAsBoolean ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
*/
public double lockedDouble (@Nonnull final DoubleSupplier aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.getAsDouble ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
*/
public int lockedInt (@Nonnull final IntSupplier aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.getAsInt ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
*/
public long lockedLong (@Nonnull final LongSupplier aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.getAsLong ();
}
finally
{
unlock ();
}
}
}