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 1151 2025-10-01 21:32:15Z tquadrat $
041 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
042 *  @UMLGraph.link
043 *  @since 0.1.0
044 */
045@ClassVersion( sourceVersion = "$Id: LockExecutorImpl.java 1151 2025-10-01 21:32:15Z 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 void execute( final Action action ) throws ExecutionFailedException { m_Lock.execute( action ); }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public <T> Optional<T> execute( final Operation<T> operation ) throws ExecutionFailedException
097    {
098        final var retValue = m_Lock.execute( operation );
099
100        //---* Done *----------------------------------------------------------
101        return retValue;
102    }   //  execute()
103
104    /**
105     *  Creates a new {@code LockExecutor} from the given
106     *  {@link Lock}
107     *  instance.
108     *
109     *  @param  lock    The lock.
110     *  @return The new {@code LockExecutor}.
111     */
112    public static final LockExecutor of( final Lock lock )
113    {
114        final var autoLock = AutoLock.of( lock );
115        final var retValue = new LockExecutorImpl( autoLock );
116
117        //---* Done *----------------------------------------------------------
118        return retValue;
119    }   //  of()
120
121    /**
122     *  Creates a new {@code LockExecutor} from the given
123     *  {@link AutoLock}
124     *  instance.
125     *
126     *  @param  lock    The lock.
127     *  @return The new {@code LockExecutor}.
128     */
129    public static final LockExecutor of( final AutoLock lock )
130    {
131        final var retValue = new LockExecutorImpl( requireNonNullArgument( lock, "lock" ) );
132
133        //---* Done *----------------------------------------------------------
134        return retValue;
135    }   //  of()
136}
137//  class LockExecutorImpl
138
139/*
140 *  End of File
141 */