All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.collections.composite.CompositeEnumeration Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections.composite;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

class CompositeEnumeration implements Enumeration
{
   protected final Iterator> _enumerations;
   
   protected Enumeration _currentEnumeration;
   protected Enumeration _nextEnumeration;
   
   CompositeEnumeration(List> enumerations)
   {
      _enumerations = enumerations.iterator();
      
      findNextEnumeration();
   }
   
   public boolean hasMoreElements()
   {
      if(_currentEnumeration != null && _currentEnumeration.hasMoreElements())
      {
         return true;
      }
      
      findNextEnumeration();
      return _nextEnumeration != null && _nextEnumeration.hasMoreElements();
   }

   public E nextElement()
   {
      if(!hasMoreElements())
      {
         throw new NoSuchElementException();
      }
      
      return moveNext();
   }

   protected void findNextEnumeration()
   {
      if(_nextEnumeration != null)
      {
         return;
      }
      
      while(_enumerations.hasNext())
      {
          _nextEnumeration = _enumerations.next();
          
          if(_nextEnumeration.hasMoreElements())
          {
             break;
          }
      }
   }

   protected E moveNext()
   {
      if(_nextEnumeration != null)
      {
         _currentEnumeration = _nextEnumeration;
         _nextEnumeration = null;
      }
      
      return _currentEnumeration.nextElement();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy