Coverage Report - com.sdmetrics.math.MappedCollectionsIterator - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MappedCollectionsIterator
100%
29/29
100%
10/10
2,5
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.math;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.Iterator;
 26  
 import java.util.Map;
 27  
 import java.util.NoSuchElementException;
 28  
 
 29  
 /**
 30  
  * Iterates over the elements contained in multiple collections, which are
 31  
  * organized as values in a map.
 32  
  * 
 33  
  * @param <T> Type of the elements contained in the collections.
 34  
  */
 35  
 public class MappedCollectionsIterator<T> implements Iterator<T> {
 36  
         /** Iterator for the outer map. */
 37  
         private Iterator<? extends Collection<T>> outerIterator;
 38  
         /** Iterator for an inner collection. */
 39  
         private Iterator<T> innerIterator;
 40  
         /** Result for next() method. */
 41  
         private T nextObject;
 42  
         /** Result for hasNext() method. */
 43  
         private boolean hasNextObject;
 44  
 
 45  
         /**
 46  
          * Constructor.
 47  
          * @param map The map containing the collections.
 48  
          */
 49  1656
         public MappedCollectionsIterator(Map<?, ? extends Collection<T>> map) {
 50  1656
                 outerIterator = map.values().iterator();
 51  1656
                 innerIterator = null;
 52  1656
                 hasNextObject = true;
 53  1656
                 getReady();
 54  1656
         }
 55  
 
 56  
         /**
 57  
          * Throws an UnsupportedOperationException.
 58  
          * 
 59  
          * @throws UnsupportedOperationException Remove not supported by this
 60  
          *         iterator.
 61  
          */
 62  
         @Override
 63  
         public void remove() {
 64  1
                 throw new UnsupportedOperationException();
 65  
         }
 66  
 
 67  
         @Override
 68  
         public T next() {
 69  17645
                 if (hasNextObject) {
 70  17644
                         T result = nextObject;
 71  17644
                         getReady();
 72  17644
                         return result;
 73  
                 }
 74  1
                 throw new NoSuchElementException();
 75  
         }
 76  
 
 77  
         @Override
 78  
         public boolean hasNext() {
 79  17795
                 return hasNextObject;
 80  
         }
 81  
 
 82  
         /** Prepare nextObject and hasNextObject for subsequent calls. */
 83  
         private void getReady() {
 84  36879
                 if (innerIterator == null) {
 85  19235
                         nextInner();
 86  19235
                         if (innerIterator == null) {
 87  171
                                 return; // no more inner iterators; quit
 88  
                         }
 89  
                 }
 90  36708
                 if (innerIterator.hasNext()) {
 91  19129
                         nextObject = innerIterator.next();
 92  19129
                 } else {
 93  17579
                         innerIterator = null; // forget current inner iterator, try next one
 94  17579
                         getReady();
 95  
                 }
 96  36708
         }
 97  
 
 98  
         /** Switch to the next inner collection. */
 99  
         private void nextInner() {
 100  19235
                 if (outerIterator.hasNext()) {
 101  19064
                         Collection<T> c = outerIterator.next();
 102  19064
                         innerIterator = c.iterator();
 103  19064
                 } else {
 104  171
                         hasNextObject = false;
 105  
                 }
 106  19235
         }
 107  
 }