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  
          * @param map The map containing the collections.
 47  
          */
 48  1636
         public MappedCollectionsIterator(Map<?, ? extends Collection<T>> map) {
 49  1636
                 outerIterator = map.values().iterator();
 50  1636
                 innerIterator = null;
 51  1636
                 hasNextObject = true;
 52  1636
                 getReady();
 53  1636
         }
 54  
 
 55  
         /**
 56  
          * Throws an UnsupportedOperationException.
 57  
          * 
 58  
          * @throws UnsupportedOperationException Remove not supported by this
 59  
          *         iterator.
 60  
          */
 61  
         @Override
 62  
         public void remove() {
 63  1
                 throw new UnsupportedOperationException();
 64  
         }
 65  
 
 66  
         @Override
 67  
         public T next() {
 68  17491
                 if (hasNextObject) {
 69  17490
                         T result = nextObject;
 70  17490
                         getReady();
 71  17490
                         return result;
 72  
                 }
 73  1
                 throw new NoSuchElementException();
 74  
         }
 75  
 
 76  
         @Override
 77  
         public boolean hasNext() {
 78  17638
                 return hasNextObject;
 79  
         }
 80  
 
 81  
         /** Prepare nextObject and hasNextObject for subsequent calls. */
 82  
         private void getReady() {
 83  36551
                 if (innerIterator == null) {
 84  19061
                         nextInner();
 85  19061
                         if (innerIterator == null)
 86  162
                                 return; // no more inner iterators; quit
 87  
                 }
 88  36389
                 if (innerIterator.hasNext()) {
 89  18964
                         nextObject = innerIterator.next();
 90  18964
                 } else {
 91  17425
                         innerIterator = null; // forget current inner iterator, try the next
 92  
                                                                         // one
 93  17425
                         getReady();
 94  
                 }
 95  36389
         }
 96  
 
 97  
         /** Switch to the next inner collection. */
 98  
         private void nextInner() {
 99  19061
                 if (outerIterator.hasNext()) {
 100  18899
                         Collection<T> c = outerIterator.next();
 101  18899
                         innerIterator = c.iterator();
 102  18899
                 } else {
 103  162
                         hasNextObject = false;
 104  
                 }
 105  19061
         }
 106  
 }