001/*
002 * ============================================================================
003 *  Copyright © 2002-2026 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.lang.Objects.requireNonNullArgument;
022import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument;
023import static org.tquadrat.foundation.util.IOUtils.getPathMatcher;
024
025import java.nio.file.PathMatcher;
026import java.util.Collection;
027import java.util.List;
028import java.util.function.BiConsumer;
029
030import org.apiguardian.api.API;
031import org.tquadrat.foundation.annotation.ClassVersion;
032import org.tquadrat.foundation.config.CmdLineException;
033import org.tquadrat.foundation.config.spi.CLIDefinition;
034import org.tquadrat.foundation.config.spi.Parameters;
035
036/**
037 *  <p>{@summary This class is an implementation of
038 *  {@link CmdLineValueHandler}
039 *  for
040 *  {@link PathMatcher}.}</p>
041 *
042 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
043 *  @version $Id: PathMatcherValueHandler.java 1231 2026-05-05 14:28:23Z tquadrat $
044 *  @since 0.1.0
045 *
046 *  @UMLGraph.link
047 */
048@ClassVersion( sourceVersion = "$Id: PathMatcherValueHandler.java 1231 2026-05-05 14:28:23Z tquadrat $" )
049@API( status = STABLE, since = "0.25.5" )
050public final class PathMatcherValueHandler extends CmdLineValueHandler<PathMatcher>
051{
052        /*--------------*\
053    ====** Constructors **=====================================================
054        \*--------------*/
055    /**
056     *  Creates a new instance of {@code PathMatcherHandler}.
057     *
058     *  @param  valueSetter The
059     *      {@link BiConsumer Consumer}
060     *      that places the translated value to the property.
061     */
062    public PathMatcherValueHandler( final BiConsumer<String,PathMatcher> valueSetter )
063    {
064        super( valueSetter );
065    }   //  PathMatcherHandler()
066
067    /**
068     *  Creates a new instance of {@code PathMatcherHandler}.
069     *
070     *  @param  context The CLI definition that provides the context for this
071     *      value handler.
072     *  @param  valueSetter The
073     *      {@link BiConsumer Consumer}
074     *      that places the translated value to the property.
075     */
076    public PathMatcherValueHandler( final CLIDefinition context, final BiConsumer<String,PathMatcher> valueSetter )
077    {
078        super( context, valueSetter );
079    }   //  PathMatcherHandler()
080
081        /*---------*\
082    ====** Methods **==========================================================
083        \*---------*/
084    /**
085     *  {@inheritDoc}
086     */
087    @Override
088    protected Collection<PathMatcher> translate( final Parameters params ) throws CmdLineException
089    {
090        final Collection<PathMatcher> retValue;
091        final var argument = requireNonNullArgument( params, "params" ).getParameter( 0 );
092        try
093        {
094            final var pathMatcher = getPathMatcher( requireNotEmptyArgument( argument, "argument" ) );
095            retValue = List.of( pathMatcher );
096        }
097        catch( final IllegalArgumentException | UnsupportedOperationException e )
098        {
099            throw new CmdLineException( getCLIDefinition(), e );
100        }
101
102        //---* Done *----------------------------------------------------------
103        return retValue;
104    }   //  translate()
105}
106//  class PathMatcherHandler
107
108/*
109 *  End of File
110 */