Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/ExpressionExecuter.java
41159 views
/*1* Copyright (c) 2004, 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.util.*;28import sun.jvmstat.monitor.*;2930/**31* A class implementing the ExpressionEvaluator to evaluate an expression32* in the context of the available monitoring data.33*34* @author Brian Doherty35* @since 1.536*/37public class ExpressionExecuter implements ExpressionEvaluator {38private static final boolean debug =39Boolean.getBoolean("ExpressionEvaluator.debug");40private MonitoredVm vm;41private HashMap<String, Object> map = new HashMap<String, Object>();4243ExpressionExecuter(MonitoredVm vm) {44this.vm = vm;45}4647/*48* evaluate the given expression.49*/50public Object evaluate(Expression e) {51if (e == null) {52return null;53}5455if (debug) {56System.out.println("Evaluating expression: " + e);57}5859if (e instanceof Literal) {60return ((Literal)e).getValue();61}6263if (e instanceof Identifier) {64Identifier id = (Identifier)e;65if (map.containsKey(id.getName())) {66return map.get(id.getName());67} else {68// cache the data values for coherency of the values over69// the life of this expression executer.70Monitor m = (Monitor)id.getValue();71Object v = m.getValue();72map.put(id.getName(), v);73return v;74}75}7677Expression l = e.getLeft();78Expression r = e.getRight();7980Operator op = e.getOperator();8182if (op == null) {83return evaluate(l);84} else {85double lval = ((Number)evaluate(l)).doubleValue();86double rval = ((Number)evaluate(r)).doubleValue();87double result = op.eval(lval, rval);88if (debug) {89System.out.println("Performed Operation: " + lval + op + rval90+ " = " + result);91}92return Double.valueOf(result);93}94}95}969798