public class Shell
extends java.lang.Object
null values and to enter arrays (String/int)
as a list of comma separated values.
In writing your code, you may be tempted to use System.out.println()
to determine what is happening. As an alternative, the method
debug() is provided. The nice thing about debug()
in comparison to System.out.println() is that debug()
only prints when debug is turn on. You can turn
debug on and off by giving the debug command to this program.
Thus, you can write debug code, but not need to change your program in any
way before turning it in. If you use System.out.println() you
MUST either comment out the lines or remove them before you submit
your code for grading. This is a simple alternative to the many logging
packages that have more advanced capabilities. See
this page for examples.
| Modifier and Type | Class and Description |
|---|---|
private static class |
Shell.StopProcessing
A class to help end processing that is only used internally by Shell.
|
| Modifier and Type | Field and Description |
|---|---|
protected static int |
debug
Any non-zero value turns debugging on
|
private static java.util.Random |
random
Random number generator
|
| Constructor and Description |
|---|
Shell() |
| Modifier and Type | Method and Description |
|---|---|
static void |
debug(java.lang.String format,
java.lang.Object... args)
Print a message if the debug value is non-zero.
|
protected java.lang.String |
filter(java.lang.String input)
Filter input for some special cases.
|
static java.lang.String |
format(java.lang.String format,
java.lang.Object... args)
An "extension" of
String.format() to automatically convert
arrays to Strings using the Arrays.toString() methods. |
int[] |
getIntArray(java.util.Scanner scanner)
Get an array of
int separated by commas from the scanner. |
static java.util.Random |
getRandom()
Get the shells random number generator.
|
java.lang.String[] |
getStrArray(java.util.Scanner scanner)
Get an array of
String separated by commas from the scanner. |
java.lang.String |
getStringOrNull(java.util.Scanner scanner)
Retrieve a String from the scanner, filtering it for convenience.
|
static void |
main(java.lang.String[] args)
Entry point to shell.
|
void |
ooMain(java.lang.String[] args)
Provide a main() like method that can be inherited and/or overridden.
|
void |
processCmdsFromFile(java.lang.String fileName)
Process commands from a file.
|
void |
processCommands()
Process commands from
System.in. |
void |
processCommands(java.util.Scanner scanner)
Command interpreter to test code.
|
void |
processOneCommand(java.lang.String cmd,
java.lang.String params)
Process one command.
|
java.lang.String |
rest(java.util.Scanner scanner)
Retrieve a String from the scanner using
nextLine(), to get
everything else in the line and filter it for special values. |
void |
showHelp()
Display a brief help summary for commands implemented by this class
|
int[] |
strArrayToIntArray(java.lang.String[] strResult)
Convert an array of
String to an array of int. |
protected static int debug
private static java.util.Random random
public static java.lang.String format(java.lang.String format,
java.lang.Object... args)
String.format() to automatically convert
arrays to Strings using the Arrays.toString() methods.
Arrays of arrays are also handled. The argument list is exactly the same
as that of String.format(), so any of the features of that
code may be used. If there is only a single argument after the
format and if that argument is an array, cast it to an Object using
((Object))format - - the format string for the output (use %s for arrays)args - - argments for the format string (variable number)public static void debug(java.lang.String format,
java.lang.Object... args)
String.format(), so any of
the features of that code may be used. See the documentation of
Shell.format() for details on printing arrays.format - - the format string for the outputargs - - argments for the format string (variable number)public static java.util.Random getRandom()
random
command with a seed.public void showHelp()
protected java.lang.String filter(java.lang.String input)
input - - the original string"empty!", return "".
If it is the String "null!", return null.
Otherwise, return the trimmed String. Override this for more special
conditions.public java.lang.String getStringOrNull(java.util.Scanner scanner)
scanner - - retrieve String from this Scanner.null).public java.lang.String rest(java.util.Scanner scanner)
nextLine(), to get
everything else in the line and filter it for special values. This differs
from Scaner.nextLine() in that the reult is trimmed of
leading and trailing blanks, and the special string
null!null and
empty!"".scanner - - retrieve String from this Scanner.null).public java.lang.String[] getStrArray(java.util.Scanner scanner)
String separated by commas from the scanner.
A future enhancement will allow quoting so that individual elements may
contain escaped commas.scanner - - source of the arraynull, containing the
filtered values.public int[] getIntArray(java.util.Scanner scanner)
int separated by commas from the scanner.scanner - - source of the arraynull, containing the valuespublic int[] strArrayToIntArray(java.lang.String[] strResult)
String to an array of int.strResult - - array of Strings to be converted to intspublic void processOneCommand(java.lang.String cmd,
java.lang.String params)
throws java.io.FileNotFoundException,
java.util.NoSuchElementException
cmd - - the command to processparams - - a string containing the parameter(s) (if any)java.io.FileNotFoundExceptionjava.util.NoSuchElementExceptionpublic void processCommands(java.util.Scanner scanner)
'#'. Comments are removed and the resulting
string trimmed. Empty lines are ignored.scanner - - the source of the commandspublic void processCmdsFromFile(java.lang.String fileName)
fileName - - name of file containing commandspublic void processCommands()
System.in.public void ooMain(java.lang.String[] args)
args - - arguments to process. If args is
null or of length 0, commands are taken from
System.in. If args.length > 0, the
args are assumed to be one or more file to be processed
using the input command..public static void main(java.lang.String[] args)
args - - an array of Strings