Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/RowClosure.java
41159 views
/*1* Copyright (c) 2004, 2013, 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 java.text.*;28import sun.jvmstat.monitor.*;2930/**31* A class implementing the Closure interface for iterating over the32* specified columns of data and generating the columnized string of33* data representing a row of output for the form.34*35* @author Brian Doherty36* @since 1.537*/38public class RowClosure implements Closure {39private MonitoredVm vm;40private StringBuilder row = new StringBuilder();4142public RowClosure(MonitoredVm vm) {43this.vm = vm;44}4546public void visit(Object o, boolean hasNext) throws MonitorException {47if (! (o instanceof ColumnFormat)) {48return;49}5051ColumnFormat c = (ColumnFormat)o;52String s = null;5354Expression e = c.getExpression();55ExpressionEvaluator ee = new ExpressionExecuter(vm);56Object value = ee.evaluate(e);5758if (value instanceof String) {59s = (String)value;60} else if (value instanceof Number) {61double d = ((Number)value).doubleValue();62double scaledValue = c.getScale().scale(d);63DecimalFormat df = new DecimalFormat(c.getFormat());64DecimalFormatSymbols syms = df.getDecimalFormatSymbols();65syms.setNaN("-");66df.setDecimalFormatSymbols(syms);67s = df.format(scaledValue);68}6970c.setPreviousValue(value);71s = c.getAlignment().align(s, c.getWidth());72row.append(s);73if (hasNext) {74row.append(" ");75}76}7778public String getRow() {79return row.toString();80}81}828384