Coverage Report - net.sf.jmatchparser.template.engine.operation.LoopForkOperation
 
Classes in this File Line Coverage Branch Coverage Complexity
LoopForkOperation
79%
19/24
87%
7/8
4,5
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * All rights reserved.
 4  
  * 
 5  
  * This program is free software: you can redistribute it and/or modify
 6  
  * it under the terms of the GNU General Public License as published by
 7  
  * the Free Software Foundation, either version 2 of the License, or
 8  
  * (at your option) any later version.
 9  
  * 
 10  
  * This program is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 package net.sf.jmatchparser.template.engine.operation;
 19  
 
 20  
 import net.sf.jmatchparser.template.engine.ParserState;
 21  
 import net.sf.jmatchparser.template.engine.parameter.Parameter;
 22  
 
 23  
 public class LoopForkOperation extends Operation {
 24  
 
 25  
         private final Label label;
 26  
         private final CounterOperation co;
 27  
         private final Parameter mincount;
 28  
         private final Parameter maxcount;
 29  
 
 30  
         public LoopForkOperation(String templatePosition, Label label,
 31  
                         CounterOperation co, Parameter mincount, Parameter maxcount) {
 32  22
                 super(templatePosition);
 33  22
                 this.label = label;
 34  22
                 this.co = co;
 35  22
                 this.mincount = mincount;
 36  22
                 this.maxcount = maxcount;
 37  22
         }
 38  
 
 39  
         @Override
 40  
         public OperationResult execute(ParserState state) {
 41  256
                 int min = 0, max = Integer.MAX_VALUE;
 42  
                 try {
 43  256
                         min = Integer.parseInt(mincount.getString(state));
 44  0
                 } catch (NumberFormatException ex) {
 45  256
                 }
 46  256
                 if (maxcount != null) {
 47  
                         try {
 48  29
                                 max = Integer.parseInt(maxcount.getString(state));
 49  0
                         } catch (NumberFormatException ex) {
 50  29
                         }
 51  
                 }
 52  256
                 if (min > max) {
 53  0
                         int tmp = max;
 54  0
                         max = min;
 55  0
                         min = tmp;
 56  
                 }
 57  256
                 int counter = co.getCounter();
 58  256
                 if (counter < max) {
 59  
                         // fork inside the loop
 60  254
                         state.getParser().enqueueState(new ParserState(state, label.getDestination()));
 61  
                 }
 62  256
                 if (counter < min) {
 63  30
                         return OperationResult.DIE;
 64  
                 }
 65  226
                 return OperationResult.CONTINUE;
 66  
         }
 67  
 }