|
1 /* |
|
2 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package nsk.sysdict.share; |
|
25 |
|
26 import java.io.*; |
|
27 |
|
28 /** |
|
29 * This tools generates a chain of classes. |
|
30 * For more details, use: |
|
31 * <pre> |
|
32 * java ChainGen -help |
|
33 * </pre> |
|
34 */ |
|
35 public class ChainGen { |
|
36 private static final int FATS_HEIGHT = 5; |
|
37 private static final int FATS_WEIGHT = 10000; |
|
38 private static final int LEANS_HEIGHT = 50; |
|
39 private static final int LEANS_WEIGHT = 1; |
|
40 |
|
41 private static void explain(Object x) { |
|
42 System.err.println("# " + x); |
|
43 } |
|
44 |
|
45 public static void main(String args[]) { |
|
46 if (args.length < 1 || args[0].toLowerCase().startsWith("-h")) { |
|
47 explain("Generates a chain classes extending each other."); |
|
48 explain(""); |
|
49 explain("Use:"); |
|
50 explain(" java ChainGen $NAME $HEIGHT $WEIGHT"); |
|
51 explain("Or:"); |
|
52 explain(" java ChainGen \"fats\""); |
|
53 explain("Or:"); |
|
54 explain(" java ChainGen \"leans\""); |
|
55 explain(""); |
|
56 explain("This creates:"); |
|
57 explain(" ${NAME}Info.java class displaying HEIGHT and WEIGHT."); |
|
58 explain(" ${NAME}XXXXXX.java defining classes chain."); |
|
59 explain(""); |
|
60 explain("Here:"); |
|
61 explain(" HEIGHT and WEIGHT must be positive integers."); |
|
62 explain(" Defaults for \"fats\": HEIGHT is " + FATS_HEIGHT |
|
63 + ", WEIGHT is " + FATS_WEIGHT +"."); |
|
64 explain(" Defaults for \"leans\": HEIGHT is " + LEANS_HEIGHT |
|
65 + ", WEIGHT is " + LEANS_WEIGHT +"."); |
|
66 System.exit(1); |
|
67 }; |
|
68 String name; |
|
69 int height, weight; |
|
70 if (args[0].toLowerCase().equals("fats")) { |
|
71 name = "Fats"; |
|
72 height = FATS_HEIGHT; |
|
73 weight = FATS_WEIGHT; |
|
74 } else if (args[0].toLowerCase().equals("leans")) { |
|
75 name = "Leans"; |
|
76 height = LEANS_HEIGHT; |
|
77 weight = LEANS_WEIGHT; |
|
78 } else { |
|
79 name = args[0]; |
|
80 height = Integer.parseInt(args[1]); |
|
81 weight = Integer.parseInt(args[2]); |
|
82 }; |
|
83 try { |
|
84 doit(name, height, weight); |
|
85 } catch (IOException exception) { |
|
86 exception.printStackTrace(System.err); |
|
87 System.exit(1); |
|
88 } |
|
89 } |
|
90 |
|
91 private static void doit(String name, int height, int weight) |
|
92 throws FileNotFoundException |
|
93 { |
|
94 // Constants definition: |
|
95 PrintStream info = new PrintStream( |
|
96 new FileOutputStream(new File(name + "Info.java")) |
|
97 ); |
|
98 info.println("// This file is generated by: ChainGen.java"); |
|
99 info.println("package nsk.sysdict.share;"); |
|
100 info.println("public class " + name + "Info {"); |
|
101 info.println(" public static final int HEIGHT = " + height + ";"); |
|
102 info.println(" private static final int WEIGHT = " + weight + ";"); |
|
103 info.println(" public static final String rootName = \"" + name + "\";"); |
|
104 info.println(" public static final String[] nodeNames = new String[] {"); |
|
105 // Generate a series of fat classes: |
|
106 for (int index=0; index<height; index++) { |
|
107 String suffix = digits(index,6); |
|
108 String className = name + suffix; |
|
109 info.println(" \"" + suffix + "\"" |
|
110 + (index+1<height? "," : "")); |
|
111 PrintStream chain = new PrintStream( |
|
112 new FileOutputStream(new File(className + ".java")) |
|
113 ); |
|
114 chain.println("// This file is generated by: ChainGen.java"); |
|
115 chain.println("package nsk.sysdict.share;"); |
|
116 chain.println("class " + className |
|
117 + (index>0? " extends " + name + digits(index-1,6): "") |
|
118 + " {"); |
|
119 for (int w=0; w<weight; w++) { |
|
120 String fieldName = "fill_" + className + "_" + digits(w,6); |
|
121 chain.println(" static long " + fieldName + ";"); |
|
122 } |
|
123 chain.println("}"); |
|
124 chain.close(); |
|
125 }; |
|
126 // |
|
127 info.println(" };"); |
|
128 info.println("}"); |
|
129 info.close(); |
|
130 } |
|
131 |
|
132 /** |
|
133 * Convert <tt>x</tt> to <tt>n</tt>-digits string. |
|
134 */ |
|
135 private static String digits(int x, int n) { |
|
136 String s = "" + x; |
|
137 while (s.length() < n) |
|
138 s = "0" + s; |
|
139 return s; |
|
140 } |
|
141 } |