001/*
002 * ============================================================================
003 * Copyright © 2002-2023 by Thomas Thrien.
004 * All Rights Reserved.
005 * ============================================================================
006 *
007 * Licensed to the public under the agreements of the GNU Lesser General Public
008 * License, version 3.0 (the "License"). You may obtain a copy of the License at
009 *
010 *      http://www.gnu.org/licenses/lgpl.html
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015 * License for the specific language governing permissions and limitations
016 * under the License.
017 */
018
019package org.tquadrat.foundation.config.cli;
020
021import static java.lang.String.format;
022import static org.apiguardian.api.API.Status.STABLE;
023import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
024
025import java.util.Collection;
026import java.util.List;
027import java.util.function.BiConsumer;
028
029import org.apiguardian.api.API;
030import org.tquadrat.foundation.annotation.ClassVersion;
031import org.tquadrat.foundation.config.CmdLineException;
032import org.tquadrat.foundation.config.spi.CLIDefinition;
033import org.tquadrat.foundation.config.spi.Parameters;
034import org.tquadrat.foundation.i18n.Message;
035import org.tquadrat.foundation.i18n.Translation;
036import org.tquadrat.foundation.util.stringconverter.EnumStringConverter;
037
038/**
039 *  An implementation of
040 *  {@link CmdLineValueHandler}
041 *  for types that are derived from
042 *  {@link Enum}.
043 *
044 *  @param  <T> The concrete data type that is handled by this value handler
045 *      implementation.
046 *
047 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
048 *  @version $Id: EnumValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $
049 *  @since 0.0.1
050 *
051 *  @UMLGraph.link
052 */
053@ClassVersion( sourceVersion = "$Id: EnumValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $" )
054@API( status = STABLE, since = "0.0.1" )
055public final class EnumValueHandler<T extends Enum<T>> extends CmdLineValueHandler<T>
056{
057        /*------------*\
058    ====** Attributes **=======================================================
059        \*------------*/
060    /**
061     *  The implementation of
062     *  {@link org.tquadrat.foundation.lang.StringConverter}
063     *  that is used to translate the String value from the command line into
064     *  the desired object instance.
065     */
066    @SuppressWarnings( "UseOfConcreteClass" )
067    private final EnumStringConverter<T> m_StringConverter;
068
069        /*-----------*\
070    ====** Constants **========================================================
071        \*-----------*/
072    /**
073     *  The error message for the name of an unknown {@code enum} value on the
074     *  command line: {@value}.
075     */
076    public static final String MSG_UnknownValue = EnumStringConverter.MSG_UnknownValue;
077
078    /**
079     *  The resource bundle key for the message about the name for an unknown
080     *  {@code enum} value on the command line.
081     */
082    @Message
083    (
084        description = "The message about the name for an unknown enum value on the command line",
085        translations =
086        {
087            @Translation( language = "en", text = EnumStringConverter.MSG_UnknownValue ),
088            @Translation( language = "de", text = "Unbekannter/ungültiger Wert: %1$s" )
089        }
090    )
091    public static final int MSGKEY_UnknownValue = 2;
092
093        /*--------------*\
094    ====** Constructors **=====================================================
095        \*--------------*/
096    /**
097     *  Creates a new {@code EnumValueHandler} instance.
098     *
099     *  @param  enumType    The data type for the property.
100     *  @param  valueSetter The
101     *      {@link BiConsumer Consumer}
102     *      that places the translated value to the property.
103     */
104    public EnumValueHandler( final Class<T> enumType, final BiConsumer<String,T> valueSetter )
105    {
106        //---* Daddy will do the null check *----------------------------------
107        super( valueSetter );
108        m_StringConverter = new EnumStringConverter<>( enumType );
109    }   //  EnumValueHandler()
110
111    /**
112     *  Creates a new {@code EnumValueHandler} instance.
113     *
114     *  @param  context The CLI definition that provides the context for this
115     *      value handler.
116     *  @param  enumType    The data type for the property.
117     *  @param  valueSetter The
118     *      {@link BiConsumer Consumer}
119     *      that places the translated value to the property.
120     */
121    public EnumValueHandler( final CLIDefinition context, final Class<T> enumType, final BiConsumer<String,T> valueSetter )
122    {
123        super( context, valueSetter );
124        m_StringConverter = new EnumStringConverter<>( enumType );
125    }   //  EnumValueHandler()
126
127        /*---------*\
128    ====** Methods **==========================================================
129        \*---------*/
130    /**
131     *  {@inheritDoc}
132     */
133    @Override
134    protected final Collection<T> translate( final Parameters params ) throws CmdLineException
135    {
136        Collection<T> retValue = List.of();
137        final var value = requireNonNullArgument( params, "params" ).getParameter( 0 );
138        try
139        {
140            final var result = m_StringConverter.fromString( value );
141            retValue = List.of( result );
142        }
143        catch( final IllegalArgumentException e )
144        {
145            throw new CmdLineException( format( MSG_UnknownValue, value ), e, MSGKEY_UnknownValue, value );
146        }
147
148        //---* Done *----------------------------------------------------------
149        return retValue;
150    }   //  translate()
151}
152//  class EnumValueHandler
153
154/*
155 *  End of File
156 */