All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.refcodes.console.impls.OrConditionImpl Maven / Gradle / Ivy

Go to download

Artifact for console issues regarding a CLI (command line interpreter) frame, CLI commands or CLI interaction and so on.

There is a newer version: 1.0.4
Show newest version
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// /////////////////////////////////////////////////////////////////////////////
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// -----------------------------------------------------------------------------
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// -----------------------------------------------------------------------------
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// -----------------------------------------------------------------------------
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////

package org.refcodes.console.impls;

import java.util.ArrayList;
import java.util.List;

import org.refcodes.console.AmbiguousArgsException;
import org.refcodes.console.Condition;
import org.refcodes.console.ConsoleUtility;
import org.refcodes.console.Operand;
import org.refcodes.console.ParseArgsException;
import org.refcodes.console.SyntaxNotation;
import org.refcodes.console.Syntaxable;
import org.refcodes.console.UnknownArgsException;

/**
 * An {@link OrConditionImpl} represents a list of {@link Syntaxable} instances
 * of which at least one must be parsed successfully when the {@link Syntaxable}
 * s' {@link Syntaxable#parseArgs(String[])} methods are invoked. The command
 * line arguments syntax { -a | -b | -c } specifies that at least "-a", "-b" or
 * "-c" must be set. In case none is set , then the {@link OrConditionImpl} will
 * terminate the {@link #parseArgs(String[])} method with an exception.
 */
public class OrConditionImpl extends AbstractCondition implements Condition {

	// /////////////////////////////////////////////////////////////////////////
	// STATIC:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// CONSTANTS:
	// /////////////////////////////////////////////////////////////////////////

	private static final String GNU_POSIX_OR = " ? ";
	private static final String LOGICAL_OR = " | ";

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS:
	// /////////////////////////////////////////////////////////////////////////

	public OrConditionImpl( Syntaxable... aArgumentizers ) {
		super( aArgumentizers );
	}

	// /////////////////////////////////////////////////////////////////////////
	// METHODS:
	// /////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("unchecked")
	@Override
	public List> parseArgs( String[] aArgs ) throws UnknownArgsException, AmbiguousArgsException, ParseArgsException {
		List> theResult = null;
		List> eOperands;
		for ( Syntaxable e : getChildren() ) {
			try {
				eOperands = (List>) e.parseArgs( aArgs );
				if ( eOperands != null ) {
					if ( theResult == null ) {
						theResult = new ArrayList>();
						;
					}
					theResult.addAll( (List>) eOperands );
				}
				aArgs = ConsoleUtility.toDiff( aArgs, eOperands );
			}
			catch ( UnknownArgsException | AmbiguousArgsException exc ) {}
		}
		if ( theResult != null ) {
			return theResult;
		}
		throw new UnknownArgsException( aArgs, "None OR operand (option) matched the provided command line arguments; though at least one must match." );
	}

	@Override
	public String parseSyntax( SyntaxNotation aSyntaxNotation ) {
		StringBuilder theBuilder = new StringBuilder();
		for ( Syntaxable eArgumentizer : getChildren() ) {
			if ( theBuilder.length() != 0 ) {
				if ( aSyntaxNotation == SyntaxNotation.REFCODES ) {
					theBuilder.append( LOGICAL_OR );
				}
				else {
					theBuilder.append( GNU_POSIX_OR );
				}
			}
			theBuilder.append( eArgumentizer.toSyntax( aSyntaxNotation ) );
		}
		return theBuilder.toString();
	}

	@Override
	public String toState() {
		StringBuilder theBuilder = new StringBuilder();
		for ( Syntaxable eArgumentizer : getChildren() ) {
			if ( theBuilder.length() == 0 ) {
				theBuilder.append( "( " );
			}
			else {
				theBuilder.append( LOGICAL_OR );
			}
			theBuilder.append( eArgumentizer.toState() );
		}
		if ( theBuilder.length() != 0 ) {
			theBuilder.append( " )" );
		}
		return theBuilder.toString();

	}

	// /////////////////////////////////////////////////////////////////////////
	// HOOKS:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// HELPER:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// INNER CLASSES:
	// /////////////////////////////////////////////////////////////////////////
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy