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 sun.jvmstat.monitor.MonitorException;
029:
030: /**
031: * A class implementing the Closure interface that visits the nodes of
032: * the nodes of a ColumFormat object and computes the header string for
033: * the columns of data.
034: *
035: * @author Brian Doherty
036: * @version 1.8, 05/05/07
037: * @since 1.5
038: */
039: public class HeaderClosure implements Closure {
040: private static final char ALIGN_CHAR = '^';
041:
042: private StringBuilder header = new StringBuilder();
043:
044: /*
045: * visit an object to perform some operation. In this case, the
046: * object is a ColumnFormat we are building the header string.
047: */
048: public void visit(Object o, boolean hasNext)
049: throws MonitorException {
050:
051: if (!(o instanceof ColumnFormat)) {
052: return;
053: }
054:
055: ColumnFormat c = (ColumnFormat) o;
056:
057: String h = c.getHeader();
058:
059: // check for special alignment character
060: if (h.indexOf(ALIGN_CHAR) >= 0) {
061: int len = h.length();
062: if ((h.charAt(0) == ALIGN_CHAR)
063: && (h.charAt(len - 1) == ALIGN_CHAR)) {
064: // ^<header>^ case - center alignment
065: c.setWidth(Math.max(c.getWidth(), Math.max(c
066: .getFormat().length(), len - 2)));
067: h = h.substring(1, len - 1);
068: h = Alignment.CENTER.align(h, c.getWidth());
069: } else if (h.charAt(0) == ALIGN_CHAR) {
070: // ^<header> case - left alignment
071: c.setWidth(Math.max(c.getWidth(), Math.max(c
072: .getFormat().length(), len - 1)));
073: h = h.substring(1, len);
074: h = Alignment.LEFT.align(h, c.getWidth());
075: } else if (h.charAt(len - 1) == ALIGN_CHAR) {
076: // <header>^ case - right alignment
077: c.setWidth(Math.max(c.getWidth(), Math.max(c
078: .getFormat().length(), len - 1)));
079: h = h.substring(0, len - 1);
080: h = Alignment.RIGHT.align(h, c.getWidth());
081: } else {
082: // an internal alignment character - ignore
083: }
084: } else {
085: // User has provided their own padding for alignment purposes
086: }
087:
088: header.append(h);
089: if (hasNext) {
090: header.append(" ");
091: }
092: }
093:
094: /*
095: * get the header string.
096: */
097: public String getHeader() {
098: return header.toString();
099: }
100: }
|