1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.sf.zel.instr.var;
19
20
21 import java.util.LinkedList;
22 import java.util.List;
23 import net.sf.zel.instr.Instruction;
24 import net.sf.zel.nr.OperableNumber;
25 import net.sf.zel.vm.ExecutionContext;
26 import net.sf.zel.vm.ZExecutionException;
27 import net.sf.zel.vm.Variable;
28
29 public final class WVMAX extends Instruction
30 {
31
32
33
34
35 public static final Instruction instance = new WVMAX();
36 private static final long serialVersionUID = -4176604591737667214L;
37
38 private WVMAX(){};
39
40
41 public void execute(final ExecutionContext context) throws ZExecutionException
42 {
43 final MatchIterator iterator = new MatchIterator((String) context.popSyncStackVal());
44 context.stack.pop();
45 FindMax stuff = new FindMax();
46 iterator.iterate(stuff, context.memContext);
47 context.stack.push(stuff.getMax());
48 context.ip++;
49 }
50
51 private static final class FindMax implements MatchIterator.DoStuff
52 {
53
54
55
56
57 private transient List maxVar;
58
59
60
61 private transient double max = Double.NEGATIVE_INFINITY;
62
63 public Variable getMax()
64 {
65 if ( maxVar != null)
66 return new Variable(maxVar);
67 else
68 return null;
69 }
70
71 @SuppressWarnings("unchecked")
72 public void execute(Object obj, List context)
73 {
74 if (obj instanceof OperableNumber)
75 {
76 final double val = ((OperableNumber) obj).doubleValue();
77 if (val > max)
78 {
79 max = val;
80 maxVar = new LinkedList(context);
81 }
82 }
83 }
84 }
85
86
87 }