001/* 002 * ============================================================================ 003 * Copyright © 2002-2025 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.lang.internal; 019 020import static org.apiguardian.api.API.Status.INTERNAL; 021import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 022 023import java.util.Optional; 024import java.util.concurrent.locks.Lock; 025 026import org.apiguardian.api.API; 027import org.tquadrat.foundation.annotation.ClassVersion; 028import org.tquadrat.foundation.annotation.NotRecord; 029import org.tquadrat.foundation.lang.Action; 030import org.tquadrat.foundation.lang.AutoLock; 031import org.tquadrat.foundation.lang.AutoLock.ExecutionFailedException; 032import org.tquadrat.foundation.lang.Constraint; 033import org.tquadrat.foundation.lang.LockExecutor; 034import org.tquadrat.foundation.lang.Operation; 035 036/** 037 * The implementation of 038 * {@link LockExecutor}. 039 * 040 * @version $Id: LockExecutorImpl.java 1185 2026-04-06 10:26:47Z tquadrat $ 041 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 042 * @UMLGraph.link 043 * @since 0.1.0 044 */ 045@ClassVersion( sourceVersion = "$Id: LockExecutorImpl.java 1185 2026-04-06 10:26:47Z tquadrat $" ) 046@API( status = INTERNAL, since = "0.1.0" ) 047@NotRecord 048public final class LockExecutorImpl implements LockExecutor 049{ 050 /*------------*\ 051 ====** Attributes **======================================================= 052 \*------------*/ 053 /** 054 * The lock. 055 */ 056 private final AutoLock m_Lock; 057 058 /*--------------*\ 059 ====** Constructors **===================================================== 060 \*--------------*/ 061 /** 062 * Creates an instance of {@code LockExecutorImpl}. 063 * 064 * @param lock The lock. 065 */ 066 private LockExecutorImpl( final AutoLock lock ) 067 { 068 m_Lock = lock; 069 } // LockExecutorImpl() 070 071 /*---------*\ 072 ====** Methods **========================================================== 073 \*---------*/ 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public boolean evaluate( final Constraint constraint ) throws ExecutionFailedException 079 { 080 final var retValue = m_Lock.evaluate( constraint ); 081 082 //---* Done *---------------------------------------------------------- 083 return retValue; 084 } // execute() 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public <T> Optional<T> execute( final Operation<T> operation ) throws ExecutionFailedException 091 { 092 final var retValue = m_Lock.execute( operation ); 093 094 //---* Done *---------------------------------------------------------- 095 return retValue; 096 } // execute() 097 098 /** 099 * Creates a new {@code LockExecutor} from the given 100 * {@link Lock} 101 * instance. 102 * 103 * @param lock The lock. 104 * @return The new {@code LockExecutor}. 105 */ 106 public static final LockExecutor of( final Lock lock ) 107 { 108 final var autoLock = AutoLock.of( lock ); 109 final var retValue = new LockExecutorImpl( autoLock ); 110 111 //---* Done *---------------------------------------------------------- 112 return retValue; 113 } // of() 114 115 /** 116 * Creates a new {@code LockExecutor} from the given 117 * {@link AutoLock} 118 * instance. 119 * 120 * @param lock The lock. 121 * @return The new {@code LockExecutor}. 122 */ 123 public static final LockExecutor of( final AutoLock lock ) 124 { 125 final var retValue = new LockExecutorImpl( requireNonNullArgument( lock, "lock" ) ); 126 127 //---* Done *---------------------------------------------------------- 128 return retValue; 129 } // of() 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public void perform( final Action action ) throws ExecutionFailedException { m_Lock.perform( action ); } 136} 137// class LockExecutorImpl 138 139/* 140 * End of File 141 */