Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/HeaderClosure.java
41159 views
/*1* Copyright (c) 2004, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627import sun.jvmstat.monitor.MonitorException;2829/**30* A class implementing the Closure interface that visits the nodes of31* the nodes of a ColumFormat object and computes the header string for32* the columns of data.33*34* @author Brian Doherty35* @since 1.536*/37public class HeaderClosure implements Closure {38private static final char ALIGN_CHAR = '^';3940private StringBuilder header = new StringBuilder();4142/*43* visit an object to perform some operation. In this case, the44* object is a ColumnFormat we are building the header string.45*/46public void visit(Object o, boolean hasNext) throws MonitorException {4748if (! (o instanceof ColumnFormat)) {49return;50}5152ColumnFormat c = (ColumnFormat)o;5354String h = c.getHeader();5556// check for special alignment character57if (h.indexOf(ALIGN_CHAR) >= 0) {58int len = h.length();59if ((h.charAt(0) == ALIGN_CHAR)60&& (h.charAt(len-1) == ALIGN_CHAR)) {61// ^<header>^ case - center alignment62c.setWidth(Math.max(c.getWidth(),63Math.max(c.getFormat().length(), len-2)));64h = h.substring(1, len-1);65h = Alignment.CENTER.align(h, c.getWidth());66} else if (h.charAt(0) == ALIGN_CHAR) {67// ^<header> case - left alignment68c.setWidth(Math.max(c.getWidth(),69Math.max(c.getFormat().length(), len-1)));70h = h.substring(1, len);71h = Alignment.LEFT.align(h, c.getWidth());72} else if (h.charAt(len-1) == ALIGN_CHAR) {73// <header>^ case - right alignment74c.setWidth(Math.max(c.getWidth(),75Math.max(c.getFormat().length(), len-1)));76h = h.substring(0, len-1);77h = Alignment.RIGHT.align(h, c.getWidth());78} else {79// an internal alignment character - ignore80}81} else {82// User has provided their own padding for alignment purposes83}8485header.append(h);86if (hasNext) {87header.append(" ");88}89}9091/*92* get the header string.93*/94public String getHeader() {95return header.toString();96}97}9899100