1 /*
   2  * Copyright (c) 2004, 2018, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jstat;
  27 
  28 import java.util.*;
  29 import 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  */
  38 public 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     @Override
  52     public Object evaluate(Expression e, boolean isForcibly) {
  53         if (e == null) {
  54             return null;
  55         }
  56 
  57         if (debug) {
  58             System.out.println("Evaluating expression: " + e);
  59         }
  60 
  61         if (e instanceof Literal) {
  62             return ((Literal)e).getValue();
  63         }
  64 
  65         if (e instanceof Identifier) {
  66             Identifier id = (Identifier)e;
  67             if (map.containsKey(id.getName())) {
  68                 return map.get(id.getName());
  69             } else {
  70                 // cache the data values for coherency of the values over
  71                 // the life of this expression executer.
  72                 Monitor m = (Monitor)id.getValue();
  73                 Object v = m.getValue();
  74                 map.put(id.getName(), v);
  75                 return v;
  76             }
  77         }
  78 
  79         Expression l = e.getLeft();
  80         Expression r = e.getRight();
  81 
  82         Operator op = e.getOperator();
  83 
  84         if (op == null) {
  85             return evaluate(l, isForcibly);
  86         } else {
  87             double lval = ((Number)evaluate(l, isForcibly)).doubleValue();
  88             double rval = ((Number)evaluate(r, isForcibly)).doubleValue();
  89             double result = op.eval(lval, rval);
  90             if (debug) {
  91                 System.out.println("Performed Operation: " + lval + op + rval
  92                                    + " = " + result);
  93             }
  94             return Double.valueOf(result);
  95         }
  96     }
  97 }