author | vlivanov |
Fri, 26 May 2017 18:39:27 +0300 | |
changeset 48557 | 2e867226b914 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 { |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
25859
diff
changeset
|
86 |
double lval = ((Number)evaluate(l)).doubleValue(); |
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
25859
diff
changeset
|
87 |
double rval = ((Number)evaluate(r)).doubleValue(); |
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
25859
diff
changeset
|
88 |
double result = op.eval(lval, rval); |
2 | 89 |
if (debug) { |
90 |
System.out.println("Performed Operation: " + lval + op + rval |
|
91 |
+ " = " + result); |
|
92 |
} |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
25859
diff
changeset
|
93 |
return Double.valueOf(result); |
2 | 94 |
} |
95 |
} |
|
96 |
} |