blob: 17bbabfda47cf14933ee8fa22d8a54fe164649ae [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * 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
26package sun.tools.jstat;
27
28import java.util.*;
29import sun.jvmstat.monitor.*;
30
31/**
32 * A class implementing the ExpressionEvaluator to evaluate an expression
33 * in the context of the available monitoring data.
34 *
35 * @author Brian Doherty
36 * @since 1.5
37 */
38public class ExpressionExecuter implements ExpressionEvaluator {
39 private static final boolean debug =
40 Boolean.getBoolean("ExpressionEvaluator.debug");
41 private MonitoredVm vm;
42 private HashMap<String, Object> map = new HashMap<String, Object>();
43
44 ExpressionExecuter(MonitoredVm vm) {
45 this.vm = vm;
46 }
47
48 /*
49 * evaluate the given expression.
50 */
51 public Object evaluate(Expression e) {
52 if (e == null) {
53 return null;
54 }
55
56 if (debug) {
57 System.out.println("Evaluating expression: " + e);
58 }
59
60 if (e instanceof Literal) {
61 return ((Literal)e).getValue();
62 }
63
64 if (e instanceof Identifier) {
65 Identifier id = (Identifier)e;
66 if (map.containsKey(id.getName())) {
67 return map.get(id.getName());
68 } else {
69 // cache the data values for coherency of the values over
70 // the life of this expression executer.
71 Monitor m = (Monitor)id.getValue();
72 Object v = m.getValue();
73 map.put(id.getName(), v);
74 return v;
75 }
76 }
77
78 Expression l = e.getLeft();
79 Expression r = e.getRight();
80
81 Operator op = e.getOperator();
82
83 if (op == null) {
84 return evaluate(l);
85 } else {
86 Double lval = new Double(((Number)evaluate(l)).doubleValue());
87 Double rval = new Double(((Number)evaluate(r)).doubleValue());
88 double result = op.eval(lval.doubleValue(), rval.doubleValue());
89 if (debug) {
90 System.out.println("Performed Operation: " + lval + op + rval
91 + " = " + result);
92 }
93 return new Double(result);
94 }
95 }
96}