View Javadoc

1   /*
2    * Copyright (c) 2001, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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       * instance
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           * the max variable
56           */
57          private transient List maxVar;
58          /**
59           * The max value
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  }