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 java.lang.Boolean.TRUE; 021import static org.apiguardian.api.API.Status.STABLE; 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.util.stringconverter.BooleanStringConverter; 034 035/** 036 * An implementation of 037 * {@link CmdLineValueHandler} 038 * for {@code boolean} and 039 * {@link Boolean} 040 * values. 041 * 042 * @extauthor Kohsuke Kawaguchi - kk@kohsuke.org 043 * @modified Thomas Thrien - thomas.thrien@tquadrat.org 044 * @version $Id: BooleanValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $ 045 * @since 0.0.1 046 * 047 * @UMLGraph.link 048 */ 049@ClassVersion( sourceVersion = "$Id: BooleanValueHandler.java 1061 2023-09-25 16:32:43Z tquadrat $" ) 050@API( status = STABLE, since = "0.0.1" ) 051public class BooleanValueHandler extends CmdLineValueHandler<Boolean> 052{ 053 /*--------------*\ 054 ====** Constructors **===================================================== 055 \*--------------*/ 056 /** 057 * Creates a new {@code BooleanValueHandler} instance. 058 * 059 * @param context The CLI definition that provides the context for this 060 * value handler. 061 * @param valueSetter The function that places the translated value to 062 * the property. 063 */ 064 public BooleanValueHandler( final CLIDefinition context, final BiConsumer<String,Boolean> valueSetter ) 065 { 066 //---* Daddy will do the null check *---------------------------------- 067 super( context, valueSetter ); 068 } // BooleanValueHandler() 069 070 /** 071 * Creates a new {@code BooleanValueHandler} instance. 072 * 073 * @param valueSetter The function that places the translated value to 074 * the property. 075 */ 076 public BooleanValueHandler( final BiConsumer<String,Boolean> valueSetter ) 077 { 078 //---* Daddy will do the null check *---------------------------------- 079 super( valueSetter ); 080 } // BooleanValueHandler() 081 082 /*---------*\ 083 ====** Methods **========================================================== 084 \*---------*/ 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public int parseCmdLine( final Parameters params ) 090 { 091 var retValue = -1; 092 try 093 { 094 final var result = translate( requireNonNullArgument( params, "params" ) ); 095 retValue = result.size(); 096 if( retValue == 0 ) 097 { 098 getValueSetter().accept( getPropertyName(), TRUE ); 099 } 100 else 101 { 102 result.forEach( r -> getValueSetter().accept( getPropertyName(), r ) ); 103 } 104 } 105 catch( final CmdLineException e ) { throw e; } 106 catch( final RuntimeException e ) 107 { 108 throw new CmdLineException( getCLIDefinition().orElse( null ), e ); 109 } 110 111 //---* Done *---------------------------------------------------------- 112 return retValue; 113 } // parseCmdLine() 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 protected Collection<Boolean> translate( final Parameters params ) throws CmdLineException 120 { 121 Collection<Boolean> retValue; 122 try 123 { 124 final var param = params.getParameter( 0 ); 125 retValue = List.of( BooleanStringConverter.INSTANCE.fromString( param ) ); 126 } 127 catch( @SuppressWarnings( "unused" ) final CmdLineException ignored ) 128 { 129 retValue = List.of(); 130 } 131 132 //---* Done *---------------------------------------------------------- 133 return retValue; 134 } // translate() 135} 136// class BooleanValueHandler 137 138/* 139 * End of File 140 */