1 /* 2 * Copyright (c) 2004, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jstat; 27 28 import java.util.*; 29 import sun.jvmstat.monitor.MonitorException; 30 31 /** 32 * A class for describing the output format specified by a command 33 * line option that was parsed from an option description file. 34 * 35 * @author Brian Doherty 36 * @since 1.5 37 */ 38 public class OptionFormat { 39 protected String name; 40 protected List<OptionFormat> children; 41 OptionFormat(String name)42 public OptionFormat(String name) { 43 this.name = name; 44 this.children = new ArrayList<OptionFormat>(); 45 } 46 equals(Object o)47 public boolean equals(Object o) { 48 if (o == this) { 49 return true; 50 } 51 if (!(o instanceof OptionFormat)) { 52 return false; 53 } 54 OptionFormat of = (OptionFormat)o; 55 return (this.name.compareTo(of.name) == 0); 56 } 57 hashCode()58 public int hashCode() { 59 return name.hashCode(); 60 } 61 addSubFormat(OptionFormat f)62 public void addSubFormat(OptionFormat f) { 63 children.add(f); 64 } 65 getSubFormat(int index)66 public OptionFormat getSubFormat(int index) { 67 return children.get(index); 68 } 69 insertSubFormat(int index, OptionFormat f)70 public void insertSubFormat(int index, OptionFormat f) { 71 children.add(index, f); 72 } 73 getName()74 public String getName() { 75 return name; 76 } 77 apply(Closure c)78 public void apply(Closure c) throws MonitorException { 79 80 for (Iterator<OptionFormat> i = children.iterator(); i.hasNext(); /* empty */) { 81 OptionFormat o = i.next(); 82 c.visit(o, i.hasNext()); 83 } 84 85 for (Iterator <OptionFormat>i = children.iterator(); i.hasNext(); /* empty */) { 86 OptionFormat o = i.next(); 87 o.apply(c); 88 } 89 } 90 printFormat()91 public void printFormat() { 92 printFormat(0); 93 } 94 printFormat(int indentLevel)95 public void printFormat(int indentLevel) { 96 String indentAmount = " "; 97 StringBuilder indent = new StringBuilder(""); 98 99 for (int j = 0; j < indentLevel; j++) { 100 indent.append(indentAmount); 101 } 102 System.out.println(indent + name + " {"); 103 104 // iterate over all children and call their printFormat() methods 105 for (OptionFormat of : children) { 106 of.printFormat(indentLevel+1); 107 } 108 System.out.println(indent + "}"); 109 } 110 } 111