001/*
002 * ============================================================================
003 *  Copyright © 2002-2021 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.lang.Action;
029import org.tquadrat.foundation.lang.AutoLock;
030import org.tquadrat.foundation.lang.AutoLock.ExecutionFailedException;
031import org.tquadrat.foundation.lang.Constraint;
032import org.tquadrat.foundation.lang.LockExecutor;
033import org.tquadrat.foundation.lang.Operation;
034
035/**
036 *  The implementation of
037 *  {@link LockExecutor}.
038 *
039 *  @version $Id: LockExecutorImpl.java 1097 2024-02-06 20:10:12Z tquadrat $
040 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
041 *  @UMLGraph.link
042 *  @since 0.1.0
043 */
044@ClassVersion( sourceVersion = "$Id: LockExecutorImpl.java 1097 2024-02-06 20:10:12Z tquadrat $" )
045@API( status = INTERNAL, since = "0.1.0" )
046public final class LockExecutorImpl implements LockExecutor
047{
048        /*------------*\
049    ====** Attributes **=======================================================
050        \*------------*/
051    /**
052     *  The lock.
053     */
054    private final AutoLock m_Lock;
055
056        /*--------------*\
057    ====** Constructors **=====================================================
058        \*--------------*/
059    /**
060     *  Creates an instance of {@code LockExecutorImpl}.
061     *
062     *  @param  lock    The lock.
063     */
064    private LockExecutorImpl( final AutoLock lock )
065    {
066        m_Lock = lock;
067    }   //  LockExecutorImpl()
068
069        /*---------*\
070    ====** Methods **==========================================================
071        \*---------*/
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public boolean evaluate( final Constraint constraint ) throws ExecutionFailedException
077    {
078        final var retValue = m_Lock.evaluate( constraint );
079
080        //---* Done *----------------------------------------------------------
081        return retValue;
082    }   //  execute()
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public void execute( final Action action ) throws ExecutionFailedException { m_Lock.execute( action ); }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public <T> Optional<T> execute( final Operation<T> operation ) throws ExecutionFailedException
095    {
096        final var retValue = m_Lock.execute( operation );
097
098        //---* Done *----------------------------------------------------------
099        return retValue;
100    }   //  execute()
101
102    /**
103     *  Creates a new {@code LockExecutor} from the given
104     *  {@link Lock}
105     *  instance.
106     *
107     *  @param  lock    The lock.
108     *  @return The new {@code LockExecutor}.
109     */
110    public static final LockExecutor of( final Lock lock )
111    {
112        final var autoLock = AutoLock.of( lock );
113        final var retValue = new LockExecutorImpl( autoLock );
114
115        //---* Done *----------------------------------------------------------
116        return retValue;
117    }   //  of()
118
119    /**
120     *  Creates a new {@code LockExecutor} from the given
121     *  {@link AutoLock}
122     *  instance.
123     *
124     *  @param  lock    The lock.
125     *  @return The new {@code LockExecutor}.
126     */
127    public static final LockExecutor of( final AutoLock lock )
128    {
129        final var retValue = new LockExecutorImpl( requireNonNullArgument( lock, "lock" ) );
130
131        //---* Done *----------------------------------------------------------
132        return retValue;
133    }   //  of()
134}
135//  class LockExecutorImpl
136
137/*
138 *  End of File
139 */