001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.util.*;
029: import sun.jvmstat.monitor.*;
030:
031: /**
032: * A class implementing the ExpressionEvaluator to evaluate an expression
033: * in the context of the available monitoring data.
034: *
035: * @author Brian Doherty
036: * @version 1.9, 05/09/07
037: * @since 1.5
038: */
039: public class ExpressionExecuter implements ExpressionEvaluator {
040: private static final boolean debug = Boolean
041: .getBoolean("ExpressionEvaluator.debug");
042: private MonitoredVm vm;
043: private HashMap<String, Object> map = new HashMap<String, Object>();
044:
045: ExpressionExecuter(MonitoredVm vm) {
046: this .vm = vm;
047: }
048:
049: /*
050: * evaluate the given expression.
051: */
052: public Object evaluate(Expression e) {
053: if (e == null) {
054: return null;
055: }
056:
057: if (debug) {
058: System.out.println("Evaluating expression: " + e);
059: }
060:
061: if (e instanceof Literal) {
062: return ((Literal) e).getValue();
063: }
064:
065: if (e instanceof Identifier) {
066: Identifier id = (Identifier) e;
067: if (map.containsKey(id.getName())) {
068: return map.get(id.getName());
069: } else {
070: // cache the data values for coherency of the values over
071: // the life of this expression executer.
072: Monitor m = (Monitor) id.getValue();
073: Object v = m.getValue();
074: map.put(id.getName(), v);
075: return v;
076: }
077: }
078:
079: Expression l = e.getLeft();
080: Expression r = e.getRight();
081:
082: Operator op = e.getOperator();
083:
084: if (op == null) {
085: return evaluate(l);
086: } else {
087: Double lval = new Double(((Number) evaluate(l))
088: .doubleValue());
089: Double rval = new Double(((Number) evaluate(r))
090: .doubleValue());
091: double result = op.eval(lval.doubleValue(), rval
092: .doubleValue());
093: if (debug) {
094: System.out.println("Performed Operation: " + lval + op
095: + rval + " = " + result);
096: }
097: return new Double(result);
098: }
099: }
100: }
|