--- /dev/null
+/**\r
+ * Counter class\r
+ *\r
+ * @author Daniel Jackson\r
+ * @version 0, 07/03/01\r
+ */\r
+\r
+package tagger;\r
+import java.io.*;\r
+\r
+public class Counter {\r
+ private int count;\r
+ private int initial;\r
+ private int type;\r
+\r
+ final static int NO_SUCH_TYPE = -1;\r
+ final static int ARABIC = 0;\r
+ final static int ROMAN_UPPER = 1;\r
+ final static int ROMAN_LOWER = 2;\r
+ final static int ALPHA_UPPER = 3;\r
+ final static int ALPHA_LOWER = 4;\r
+\r
+ // eventually recognize counter_type and set initial count and output format\r
+ // takes style and stream for error reporting\r
+ /*\r
+ * requires: count_prop and style are non null\r
+ *\r
+ */\r
+ public Counter (String count_prop, String style, PrintStream error_stream) {\r
+ Assert.assert (count_prop != null);\r
+ Assert.assert (style != null);\r
+ type = get_type (count_prop);\r
+ switch (type) {\r
+ case NO_SUCH_TYPE:\r
+ type = ARABIC;\r
+ initial = 0;\r
+ break;\r
+ case ALPHA_LOWER:\r
+ case ALPHA_UPPER:\r
+ if (count_prop.length () != 1) {\r
+ error_stream.println ("Bad counter type for style " + style + ": " + count_prop);\r
+ initial = 0;\r
+ break;\r
+ }\r
+ initial = count_prop.toLowerCase().charAt (0) - 'a';\r
+ break;\r
+ case ARABIC:\r
+ try {\r
+ initial = Integer.parseInt (count_prop) - 1;\r
+ } catch (NumberFormatException e) {\r
+ error_stream.println ("Bad counter type for style " + style + ": " + count_prop + "; " + e.getMessage());\r
+ }\r
+ break;\r
+ case ROMAN_LOWER:\r
+ case ROMAN_UPPER:\r
+ // not yet implemented\r
+ initial = 0;\r
+ type = ARABIC;\r
+ break;\r
+ default:\r
+ Assert.unreachable ();\r
+ }\r
+ count = initial;\r
+ }\r
+\r
+ /**\r
+ * ensures: increments counter\r
+ * returns true iff successful, false otherwise (eg, because alphabetic counter went past 'z')\r
+ */\r
+ public boolean increment () {\r
+ if ((type == ALPHA_UPPER || type == ALPHA_LOWER) && count == 26)\r
+ return false;\r
+ count++;\r
+ return true;\r
+ }\r
+\r
+ public void reset () {\r
+ count = initial;\r
+ }\r
+\r
+ public String unparse () {\r
+ switch (type) {\r
+ case ALPHA_LOWER: {\r
+ char c = (char) ('a' + count - 1);\r
+ return new Character (c).toString();\r
+ }\r
+ case ALPHA_UPPER: {\r
+ char c = (char) ('A' + count - 1);\r
+ return new Character (c).toString();\r
+ }\r
+ case ARABIC:\r
+ return String.valueOf (count);\r
+ case ROMAN_LOWER:\r
+ case ROMAN_UPPER:\r
+ // not yet implemented\r
+ Assert.unreachable ();\r
+ break;\r
+ default:\r
+ Assert.unreachable ();\r
+ }\r
+ return "DUMMY";\r
+ }\r
+\r
+ /**\r
+ *\r
+ * ensures: returns counter type of counter given in the string counter_type\r
+ * as an int, being equal to one of the values of the constants declared in the Counter class.\r
+ * returns Counter.NO_SUCH_TYPE if the string is not well formed.\r
+ */\r
+ public static int get_type (String counter_type) {\r
+ if (counter_type.length() == 0) return NO_SUCH_TYPE;\r
+ char c = counter_type.charAt (0);\r
+ if (c >= 'a' && c <= 'z')\r
+ return ALPHA_LOWER;\r
+ if (c >= 'A' && c <= 'Z')\r
+ return ALPHA_UPPER;\r
+ if (c == 'i' || c == 'v' || c == 'x' ||c == 'l' || c == 'c' || c == 'm')\r
+ return ROMAN_LOWER;\r
+ if (c == 'I' || c == 'V' || c == 'X' ||c == 'L' || c == 'C' || c == 'M')\r
+ return ROMAN_LOWER;\r
+ if (c >= '0' && c <= '9')\r
+ return ARABIC;\r
+ return NO_SUCH_TYPE;\r
+ }\r
+}
\ No newline at end of file