001/*
002 * ============================================================================
003 *  Copyright © 2002-2023 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.config.cli;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.config.internal.Commons.createException;
022import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
023
024import java.util.Collection;
025import java.util.List;
026import java.util.function.BiConsumer;
027
028import org.apiguardian.api.API;
029import org.tquadrat.foundation.annotation.ClassVersion;
030import org.tquadrat.foundation.config.CmdLineException;
031import org.tquadrat.foundation.config.spi.CLIDefinition;
032import org.tquadrat.foundation.config.spi.Parameters;
033import org.tquadrat.foundation.lang.StringConverter;
034
035/**
036 *  The implementation of
037 *  {@link CmdLineValueHandler}
038 *  for all those types for that an implementation of
039 *  {@link StringConverter}
040 *  exists.
041 *
042 *  @param  <T> The target type.
043 *
044 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
045 *  @version $Id: SimpleCmdLineValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $
046 *  @since 0.1.0
047 *
048 *  @UMLGraph.link
049 */
050@ClassVersion( sourceVersion = "$Id: SimpleCmdLineValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $" )
051@API( status = STABLE, since = "0.1.0" )
052public sealed class SimpleCmdLineValueHandler<T> extends CmdLineValueHandler<T>
053    permits YesNoValueHandler
054{
055        /*------------*\
056    ====** Attributes **=======================================================
057        \*------------*/
058    /**
059     *  The implementation of
060     *  {@link StringConverter}
061     *  that is used to translate the String value from the command line into
062     *  the desired object instance.
063     */
064    private final StringConverter<? extends T> m_StringConverter;
065
066        /*--------------*\
067    ====** Constructors **=====================================================
068        \*--------------*/
069    /**
070     *  Creates a new {@code SimpleCmdLineValueHandler} instance.
071     *
072     *  @param  valueSetter The
073     *      {@link BiConsumer Consumer}
074     *      that places the translated value to the property.
075     *  @param  stringConverter The implementation of
076     *      {@link StringConverter}
077     *      that is used to translate the String value from the command line
078     *      into the desired object instance.
079     */
080    public SimpleCmdLineValueHandler( final BiConsumer<String,T> valueSetter, final StringConverter<? extends T> stringConverter )
081    {
082        super( valueSetter );
083        m_StringConverter = stringConverter;
084    }   //  SimpleCmdLineValueHandler()
085
086    /**
087     *  Creates a new {@code SimpleCmdLineValueHandler} instance.
088     *
089     *  @param  context The CLI definition that provides the context for this
090     *      value handler.
091     *  @param  valueSetter The
092     *      {@link BiConsumer Consumer}
093     *      that places the translated value to the property.
094     *  @param  stringConverter The implementation of
095     *      {@link StringConverter}
096     *      that is used to translate the String value from the command line
097     *      into the desired object instance.
098     */
099    public SimpleCmdLineValueHandler( final CLIDefinition context, final BiConsumer<String,T> valueSetter, final StringConverter<? extends T> stringConverter )
100    {
101        super( context, valueSetter );
102        m_StringConverter = stringConverter;
103    }   //  SimpleCmdLineValueHandler()
104
105        /*---------*\
106    ====** Methods **==========================================================
107        \*---------*/
108    /**
109     *  Translates the command line values that can be referenced via the
110     *  {@code params} argument to the target type.
111     *
112     *  @param  params  The command line values to translate.
113     *  @return A collection with the result; each entry in the collection
114     *      corresponds to one value from the command line.
115     *  @throws CmdLineException    The given parameters cannot be parsed to
116     *      the target type.
117     */
118    @Override
119    protected final Collection<T> translate( final Parameters params ) throws CmdLineException
120    {
121        Collection<T> retValue = List.of();
122        final var source = requireNonNullArgument( params, "params" ).getParameter( 0 );
123        try
124        {
125            final var result = m_StringConverter.fromString( source );
126            retValue = List.of( result );
127        }
128        catch( final IllegalArgumentException e )
129        {
130            throw createException( m_StringConverter.getClass(), e, source );
131        }
132
133        //---* Done *----------------------------------------------------------
134        return retValue;
135    }   //  translate()
136}
137//  class SimpleCmdLineValueHandler
138
139/*
140 *  End of File
141 */