001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.util.*;
029: import sun.jvmstat.monitor.MonitorException;
030:
031: /**
032: * A class for describing the output format specified by a command
033: * line option that was parsed from an option description file.
034: *
035: * @author Brian Doherty
036: * @version 1.9, 05/09/07
037: * @since 1.5
038: */
039: public class OptionFormat {
040: protected String name;
041: protected List<OptionFormat> children;
042:
043: public OptionFormat(String name) {
044: this .name = name;
045: this .children = new ArrayList<OptionFormat>();
046: }
047:
048: public boolean equals(Object o) {
049: if (o == this ) {
050: return true;
051: }
052: if (!(o instanceof OptionFormat)) {
053: return false;
054: }
055: OptionFormat of = (OptionFormat) o;
056: return (this .name.compareTo(of.name) == 0);
057: }
058:
059: public int hashCode() {
060: return name.hashCode();
061: }
062:
063: public void addSubFormat(OptionFormat f) {
064: children.add(f);
065: }
066:
067: public OptionFormat getSubFormat(int index) {
068: return children.get(index);
069: }
070:
071: public void insertSubFormat(int index, OptionFormat f) {
072: children.add(index, f);
073: }
074:
075: public String getName() {
076: return name;
077: }
078:
079: public void apply(Closure c) throws MonitorException {
080:
081: for (Iterator i = children.iterator(); i.hasNext(); /* empty */) {
082: OptionFormat o = (OptionFormat) i.next();
083: c.visit(o, i.hasNext());
084: }
085:
086: for (Iterator i = children.iterator(); i.hasNext(); /* empty */) {
087: OptionFormat o = (OptionFormat) i.next();
088: o.apply(c);
089: }
090: }
091:
092: public void printFormat() {
093: printFormat(0);
094: }
095:
096: public void printFormat(int indentLevel) {
097: String indentAmount = " ";
098: StringBuilder indent = new StringBuilder("");
099:
100: for (int j = 0; j < indentLevel; j++) {
101: indent.append(indentAmount);
102: }
103: System.out.println(indent + name + " {");
104:
105: // iterate over all children and call their printFormat() methods
106: for (OptionFormat of : children) {
107: of.printFormat(indentLevel + 1);
108: }
109: System.out.println(indent + "}");
110: }
111: }
|