01: /*
02: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.tools.jstat;
27:
28: import java.text.*;
29: import sun.jvmstat.monitor.*;
30:
31: /**
32: * A class implementing the Closure interface for iterating over the
33: * specified columns of data and generating the columnized string of
34: * data representing a row of output for the form.
35: *
36: * @author Brian Doherty
37: * @version 1.8, 05/05/07
38: * @since 1.5
39: */
40: public class RowClosure implements Closure {
41: private MonitoredVm vm;
42: private StringBuilder row = new StringBuilder();
43:
44: public RowClosure(MonitoredVm vm) {
45: this .vm = vm;
46: }
47:
48: public void visit(Object o, boolean hasNext)
49: throws MonitorException {
50: if (!(o instanceof ColumnFormat)) {
51: return;
52: }
53:
54: ColumnFormat c = (ColumnFormat) o;
55: String s = null;
56:
57: Expression e = c.getExpression();
58: ExpressionEvaluator ee = new ExpressionExecuter(vm);
59: Object value = ee.evaluate(e);
60:
61: if (value instanceof String) {
62: s = (String) value;
63: } else if (value instanceof Number) {
64: double d = ((Number) value).doubleValue();
65: double scaledValue = c.getScale().scale(d);
66: DecimalFormat df = new DecimalFormat(c.getFormat());
67: s = df.format(scaledValue);
68: }
69:
70: c.setPreviousValue(value);
71: s = c.getAlignment().align(s, c.getWidth());
72: row.append(s);
73: if (hasNext) {
74: row.append(" ");
75: }
76: }
77:
78: public String getRow() {
79: return row.toString();
80: }
81: }
|