john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

CommandLineParameterRequirements

// 2012-03-20 johnpfeiffer
import java.io.PrintStream;

class CommandLineParameterRequirements
{
    public static final String classVersion = "0.2";
    int requiredCount = -1;
    String errorMessage = null;

    CommandLineParameterRequirements( int n , String msg )
    {
        if( n < 0 )
        {       throw new IllegalArgumentException( "Number of parameters cannot be below 0" );
        }else
        {
            if( msg.equals( null ) )
            {       throw new NullPointerException( "Error Message can not be null" );
            }else
            {       requiredCount = n;
                    errorMessage = msg;
            }
        }
    }

    String getVersion()
    {
        return classVersion;
    }

    int getRequiredCount()
    {       return requiredCount;
    }

    String getErrorMessage()
    {       return errorMessage;
    }

    boolean isValid( String[] args , PrintStream out )
    {
        boolean result = false;
        if( args != null )
        {
            if( args.length == requiredCount && !args[0].isEmpty() )
            {       result = true;
            }else
            {       out.println( args.length + " does not equal the " + requiredCount + " required arguments." );
                    out.println( errorMessage );
            }
        }
        return result;
    }
} //end class

  • « CommandLineParameterRequirementsTest
  • eclipse create a java library jar and add to new project »

Published

Mar 20, 2012

Category

java-classes

~101 words

Tags

  • classes 92
  • comanndline 1
  • java 252
  • parameters 15