blob: c633e5431527f44c6502726b6e5f211e411a2334 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.tools.jstat;
27
28import java.util.*;
29import 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 */
38public class OptionFormat {
39 protected String name;
40 protected List<OptionFormat> children;
41
42 public OptionFormat(String name) {
43 this.name = name;
44 this.children = new ArrayList<OptionFormat>();
45 }
46
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
58 public int hashCode() {
59 return name.hashCode();
60 }
61
62 public void addSubFormat(OptionFormat f) {
63 children.add(f);
64 }
65
66 public OptionFormat getSubFormat(int index) {
67 return children.get(index);
68 }
69
70 public void insertSubFormat(int index, OptionFormat f) {
71 children.add(index, f);
72 }
73
74 public String getName() {
75 return name;
76 }
77
78 public void apply(Closure c) throws MonitorException {
79
80 for (Iterator i = children.iterator(); i.hasNext(); /* empty */) {
81 OptionFormat o = (OptionFormat)i.next();
82 c.visit(o, i.hasNext());
83 }
84
85 for (Iterator i = children.iterator(); i.hasNext(); /* empty */) {
86 OptionFormat o = (OptionFormat)i.next();
87 o.apply(c);
88 }
89 }
90
91 public void printFormat() {
92 printFormat(0);
93 }
94
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}