Coverage Report - net.sourceforge.combean.util.GCombinedNestedIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
GCombinedNestedIterator
100%
9/9
60%
18/30
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean 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 General Public License
 15  
     along with Foobar; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 package net.sourceforge.combean.util
 19  
 
 20  
 /**
 21  
  * Similar to a nested iterator. The only difference is that the combined nested
 22  
  * iterator returns pairs of the elements of the inner and the outer iteration as
 23  
  * result of next(). If the inner or the outer iteration return Lists, these
 24  
  * Lists can be flattened into a single list as result of next().
 25  
  */
 26  
 class GCombinedNestedIterator extends GNestedIterator {
 27  
     
 28  
     private boolean flattenResult = true; 
 29  
     
 30  
     /**
 31  
      * Constructor
 32  
      * 
 33  
      * @params flattenResult flag whether the results of the inner and outer
 34  
      * iteration shall be flattened into one List if they are Lists themselves
 35  
      * @params outerIterable Iterable for the outer iteration
 36  
      * @params getInnerItForOuter Closure which returns the inner iterator
 37  
      */
 38  
     public GCombinedNestedIterator(boolean flattenResult,
 39  
             Iterable outerIterable, Closure getInnerItForOuter) {
 40  20
         super(outerIterable, getInnerItForOuter);
 41  20
         this.flattenResult = flattenResult;
 42  
     }
 43  
     
 44  
     /**
 45  
      * Constructor
 46  
      * 
 47  
      * @params flattenResult flag whether the results of the inner and outer
 48  
      * iteration shall be flattened into one List if they are Lists themselves
 49  
      * @params itOuter Iterator for the outer iteration
 50  
      * @params getInnerItForOuter Closure which returns the inner iterator
 51  
      */
 52  
     public GCombinedNestedIterator(boolean flattenResult,
 53  
             Iterator itOuter, Closure getInnerItForOuter) {
 54  45
         super(itOuter, getInnerItForOuter);
 55  45
         this.flattenResult = flattenResult;
 56  
     }
 57  
     
 58  
     Object next() {
 59  230
         Object innerObj = super.next();
 60  230
         if (this.flattenResult) {
 61  60
             assert getOuter() instanceof List;
 62  60
             return getOuter() + innerObj;
 63  
         }
 64  170
         return [getOuter(), innerObj];
 65  
     }
 66  
 }