1 /*
   2  * Copyright (c) 2004, 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     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 = Double.valueOf(((Number)evaluate(l)).doubleValue());
  87             Double rval = Double.valueOf(((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 Double.valueOf(result);
  94         }
  95     }
  96 }