Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/BTreeGen.java
41161 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.sysdict.share;2425import java.io.*;2627/**28* This tools generates a binary tree of classes.29* For more details, use:30* <pre>31* java BTreeGen -help32* </pre>33*/34public class BTreeGen {35private static final int HEIGHT = 12; // default tree height36private static final int WEIGHT = 200; // as many longs in each node3738private static void explain(Object x) {39System.err.println("# " + x);40}4142public static void main(String args[]) {43if (args.length < 1 || args[0].toLowerCase().startsWith("-h")) {44explain("This tools generates a binary tree of classes:");45explain("");46explain(" BTree0");47explain(" / \\");48explain(" BTree0L BTree0R");49explain(" / \\ / \\");50explain(" ... ... ... ...");51explain("");52explain("Use:");53explain(" java BTreeGen { $HEIGHT [ $WEIGHT ] | \"default\" }");54explain("");55explain("Generates:");56explain(" BTree.java class having HEIGHT constant.");57explain(" BTree0.java defining a classes tree.");58explain("");59explain("Here:");60explain(" HEIGHT and WEIGHT must be positive integers.");61explain(" Default HEIGHT is " + HEIGHT + " ("62+ ((1<<HEIGHT)-1) + " classes in a tree).");63explain(" Default WEIGHT is " + WEIGHT + ".");64System.exit(1);65};66int height = HEIGHT;67int weight = WEIGHT;68if (!args[0].toLowerCase().equals("default")) {69height = Integer.parseInt(args[0]);70if (args.length > 1)71weight = Integer.parseInt(args[1]);72};73try {74doit(height, weight);75} catch (IOException exception) {76exception.printStackTrace(System.err);77System.exit(1);78}79}8081private static void doit(int height, int weight)82throws FileNotFoundException83{84// Constant definition:85PrintStream info;86info = new PrintStream(new FileOutputStream(new File("BTreeInfo.java")));87info.println("// This file is generated by: BTreeGen.java");88info.println("package nsk.sysdict.share;");89info.println("import nsk.share.*;");90info.println("public class BTreeInfo {");91info.println(" public static final int HEIGHT = " + height + ";");92info.println(" private static final int WEIGHT = " + weight + ";");93info.println(" public static final String rootName = \"BTree0\";");94info.println(" public static final Denotation nodesDenotation");95info.println(" = new TreeNodesDenotation(\"LR\");");96info.println("}");97info.close();98// Generate ta classes tree:99PrintStream btree;100btree = new PrintStream(new FileOutputStream(new File("BTree.java")));101btree.println("// This file is generated by: BTreeGen.java");102btree.println("package nsk.sysdict.share;");103new BTreeGen(btree).generate(height,weight);104btree.close();105}106107private PrintStream stream;108private BTreeGen(PrintStream stream) {109this.stream = stream;110}111private void println(String s) {112stream.println(s);113}114115private void generate(int height, int weight) {116if (height < 1)117throw new IllegalArgumentException("bad height: " + height);118if (weight < 1)119throw new IllegalArgumentException("bad weight: " + weight);120String basename = "BTree0";121genClass(null,basename,weight);122gen(basename,height,weight);123}124125private void gen(String name, int height, int weight) {126if (height == 1)127return;128String nameL = name + "L";129String nameR = name + "R";130genClass(name, nameL, weight);131genClass(name, nameR, weight);132gen(nameL, height-1, weight);133gen(nameR, height-1, weight);134}135136private void genClass(String baseName, String className, int weight) {137println("class " + className138+ (baseName!=null? " extends " + baseName: "") + " {");139for (int w=1; w<=weight; w++)140println(" static long fill_" + className + "_" + w + ";");141println("}");142}143}144145146