Showing the changes to java/pkg/sun/tools/jstat/HeaderClosure.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,98 @@ | |||
| 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 sun.jvmstat.monitor.MonitorException; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * A class implementing the Closure interface that visits the nodes of | ||
| 32 | * the nodes of a ColumFormat object and computes the header string for | ||
| 33 | * the columns of data. | ||
| 34 | * | ||
| 35 | * @author Brian Doherty | ||
| 36 | * @since 1.5 | ||
| 37 | */ | ||
| 38 | public class HeaderClosure implements Closure { | ||
| 39 | private static final char ALIGN_CHAR = '^'; | ||
| 40 | |||
| 41 | private StringBuilder header = new StringBuilder(); | ||
| 42 | |||
| 43 | /* | ||
| 44 | * visit an object to perform some operation. In this case, the | ||
| 45 | * object is a ColumnFormat we are building the header string. | ||
| 46 | */ | ||
| 47 | public void visit(Object o, boolean hasNext) throws MonitorException { | ||
| 48 | |||
| 49 | if (! (o instanceof ColumnFormat)) { | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | ColumnFormat c = (ColumnFormat)o; | ||
| 54 | |||
| 55 | String h = c.getHeader(); | ||
| 56 | |||
| 57 | // check for special alignment character | ||
| 58 | if (h.indexOf(ALIGN_CHAR) >= 0) { | ||
| 59 | int len = h.length(); | ||
| 60 | if ((h.charAt(0) == ALIGN_CHAR) | ||
| 61 | && (h.charAt(len-1) == ALIGN_CHAR)) { | ||
| 62 | // ^<header>^ case - center alignment | ||
| 63 | c.setWidth(Math.max(c.getWidth(), | ||
| 64 | Math.max(c.getFormat().length(), len-2))); | ||
| 65 | h = h.substring(1, len-1); | ||
| 66 | h = Alignment.CENTER.align(h, c.getWidth()); | ||
| 67 | } else if (h.charAt(0) == ALIGN_CHAR) { | ||
| 68 | // ^<header> case - left alignment | ||
| 69 | c.setWidth(Math.max(c.getWidth(), | ||
| 70 | Math.max(c.getFormat().length(), len-1))); | ||
| 71 | h = h.substring(1, len); | ||
| 72 | h = Alignment.LEFT.align(h, c.getWidth()); | ||
| 73 | } else if (h.charAt(len-1) == ALIGN_CHAR) { | ||
| 74 | // <header>^ case - right alignment | ||
| 75 | c.setWidth(Math.max(c.getWidth(), | ||
| 76 | Math.max(c.getFormat().length(), len-1))); | ||
| 77 | h = h.substring(0, len-1); | ||
| 78 | h = Alignment.RIGHT.align(h, c.getWidth()); | ||
| 79 | } else { | ||
| 80 | // an internal alignment character - ignore | ||
| 81 | } | ||
| 82 | } else { | ||
| 83 | // User has provided their own padding for alignment purposes | ||
| 84 | } | ||
| 85 | |||
| 86 | header.append(h); | ||
| 87 | if (hasNext) { | ||
| 88 | header.append(" "); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | /* | ||
| 93 | * get the header string. | ||
| 94 | */ | ||
| 95 | public String getHeader() { | ||
| 96 | return header.toString(); | ||
| 97 | } | ||
| 98 | } | ||