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

org.refcodes.console.impls.XorConditionImpl 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.

The 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.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;
import org.refcodes.textual.impls.VerboseTextBuilderImpl;

/**
 * An {@link XorConditionImpl} represents a list of {@link Syntaxable} instances
 * of which only one is allowed to 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
 * only "-a", only "-b" or only "-c" must be set. In case more then one is set
 * or none, then the {@link XorConditionImpl} will terminate the
 * {@link #parseArgs(String[])} method with an exception.
 */
public class XorConditionImpl extends AbstractCondition implements Condition {

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

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

	private static final String GUN_POSIX_XOR = " | ";
	private static final String LOGICAL_XOR = " ^ ";

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

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

	public XorConditionImpl( 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() ) {
			eOperands = null;
			try {
				eOperands = (List>) e.parseArgs( aArgs );
				if ( eOperands != null && eOperands.isEmpty() ) eOperands = null;
			}
			catch ( UnknownArgsException | AmbiguousArgsException exc ) {}
			if ( theResult != null && eOperands != null ) {
 throw new AmbiguousArgsException( aArgs, "More than one XOR operand (option) matched the provided command line arguments; though exactly one must match: " + new VerboseTextBuilderImpl().withElements( ConsoleUtility.toArgs( theResult, eOperands ) ).toString() );
			}
			if ( eOperands != null && !eOperands.isEmpty() && theResult == null ) {
				theResult = eOperands;
			}

		}
		if ( theResult != null ) {
			return theResult;
		}
		throw new UnknownArgsException( aArgs, "Not one XOR operand (option) matched the provided command line arguments; though exactly one must match." );
	}

	@Override
	public String toSyntax( SyntaxNotation aSyntaxNotation ) {
		StringBuilder theBuilder = new StringBuilder( parseSyntax( aSyntaxNotation ) );
		if ( theBuilder.length() > 0 ) {
			if ( aSyntaxNotation == SyntaxNotation.REFCODES ) {
				theBuilder.insert( 0, "( " );
			}
			else {
				theBuilder.insert( 0, "{ " );
			}
			if ( aSyntaxNotation == SyntaxNotation.REFCODES ) {
				theBuilder.append( " )" );
			}
			else {
				theBuilder.append( " }" );
			}
		}
		return theBuilder.toString();
	}

	@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_XOR );
				}
				else {
					theBuilder.append( GUN_POSIX_XOR );
				}
			}
			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_XOR );
			}
			theBuilder.append( eArgumentizer.toState() );
		}
		if ( theBuilder.length() != 0 ) {
			theBuilder.append( " )" );
		}
		return theBuilder.toString();

	}

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy