001 /* Copyright 2000, 2001, Compaq Computer Corporation */
002
003 package javafe;
004
005 /**
006 * <code>Tool</code> is an abstract class for tools.
007 *
008 * <p> Tools are command-line applications invoked by calling their
009 * static <code>main(String[])</code> method. <p>
010 */
011
012 public abstract class Tool
013 {
014 /**************************************************
015 * Exit codes *
016 **************************************************/
017
018 static public final int okExitCode = 0;
019 static public final int badUsageExitCode = 1;
020 static public final int errorExitCode = 2;
021 static public final int outOfMemoryExitCode = 3;
022
023 /***************************************************
024 * *
025 * Generating a usage message: *
026 * *
027 **************************************************/
028
029 /**
030 * Return the non-null name of this tool. E.g., "ls" or "cp".
031 * Used in usage and error messages.
032 */
033 //@ ensures \result != null;
034 public abstract String name();
035
036
037 /**
038 * Print our usage message to <code>System.err</code>.
039 */
040 public void usage() {
041 options.usage(name());
042 }
043
044 public void badOptionUsage(Exception e) {
045 System.err.println(name() + ": " + e.getMessage());
046 if (!options.quiet) usage();
047 }
048
049 /***************************************************
050 * *
051 * Generic option processing: *
052 * *
053 **************************************************/
054
055 /**
056 * A statically held Options object. The object is static to
057 * facilitate using the options in other classes throughout the
058 * program. All processing and reporting of options is managed by
059 * this object.
060 */
061 static public Options options = null;
062
063 /***************************************************
064 * *
065 * Main processing code: *
066 * *
067 **************************************************/
068
069 /**
070 * Start up an instance of this tool using command-line arguments
071 * <code>args</code>.
072 *
073 * <p> <strong>Note</strong>: this code needs to be copied verbatim
074 * to each subclass of <code>Tool</code> except with the name of the
075 * actual subclass inserted after the new operator and the comment
076 * characters (//) removed.
077 *
078 * <p> (This needs to be done because static methods cannot be
079 * inherited.) <p>
080 */
081 //@ requires \nonnullelements(args);
082 public static void main(String[] args) {
083 // Tool t = new Tool();
084 // int result = t.run(args);
085 // if (result != 0) System.exit(result);
086 }
087
088
089 /**
090 * A tool's main entry point; <code>args</code> are the
091 * command-line arguments we have been invoked with.
092 *
093 * @return the exit code (0 = success, >0 is a failure)
094 */
095 //@ requires \nonnullelements(args);
096 public abstract int run(String[] args);
097
098 /**
099 * Compute the time used from a start time to now, then return it in
100 * a user readable form.
101 */
102 //@ ensures \result != null;
103 public static String timeUsed(long startTime) {
104 if (options.testMode) return "TIME";
105 long delta = java.lang.System.currentTimeMillis() - startTime;
106
107 return (delta/1000.0) + " s" + " " + spaceUsed();
108 }
109
110 public static long currentTime() {
111 return java.lang.System.currentTimeMillis();
112 }
113
114 public static String spaceUsed() {
115 long used = rt.totalMemory() - rt.freeMemory();
116 return used + " bytes";
117 }
118
119 private static Runtime rt = Runtime.getRuntime();
120 }