Enumerations

A convenience class allowing Enumerations to be used in for-each statements. Five times faster than Collection.list(), using virtually no heap space.

/*
  * Copyright 2009-2010 Markus KARG
  *
  * This file is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This file is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Lesser General Public License for more details.
  *
  * For a copy of the GNU Lesser General Public License
  * see <http://www.gnu.org/licenses/>.
  */
 
 package eu.headcrashing.util;
 
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Iterator;
 
 /**
  * This class allows Enumerations to be iterated.
  * 
  * @author Markus KARG (markus@headcrashing.eu)
  */
 public final class Enumerations {
 
         /**
          * Allows using of {@link Enumeration} with the for-each statement. The
          * implementation is not using any heap space and such is able to serve
          * virtually endless Enumerations, while {@link Collections#list} is limited
          * by available RAM. As a result, this implementation is much faster than
          * Collections.list.
          * 
          * @param enumeration
          *            The original enumeration.
          * @return An {@link Iterable} directly calling the original Enumeration.
          */
         public static final <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
                 return new Iterable<T>() {
                         public final Iterator<T> iterator() {
                                 return new Iterator<T>() {
                                         public final boolean hasNext() {
                                                 return enumeration.hasMoreElements();
                                         }
 
                                         public final T next() {
                                                 return enumeration.nextElement();
                                         }
 
                                         /**
                                          * This method is not implemeted as it is impossible to
                                          * remove something from an Enumeration.
                                          * 
                                          * @throws UnsupportedOperationException
                                          *             always.
                                          */
                                         public final void remove() {
                                                 throw new UnsupportedOperationException();
                                         }
                                 };
                         }
                 };
         }
 
 }
Advertisements

About Markus Karg

Java Guru with +30 years of experience in professional software development. I travelled the whole world of IT, starting from Sinclair's great ZX Spectrum 48K, Commodore's 4040, over S/370, PCs since legendary XT, CP/M, VM/ESA, DOS, Windows (remember 3.1?), OS/2 WARP, Linux to Android and iOS... and still coding is my passion, and Java is my favourite drug!
This entry was posted in Java, Programming and tagged , . Bookmark the permalink.