001/* 002 * Copyright 2010, 2011 Chris Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.bindings.core.utils.lang; 017 018import java.util.Enumeration; 019import java.util.Iterator; 020 021/** 022 * Helper used for working with the JDKs classloader API 023 * @param <E> Subject of the enumeration to be wrapped 024 */ 025public class IterableEnumeration<E> implements Iterable<E> { 026 027 private final Enumeration<E> enumeration; 028 029 /** 030 * Creates a new instance 031 * @param enumeration The enumeration to be wrapped 032 */ 033 public IterableEnumeration(Enumeration<E> enumeration) { 034 this.enumeration = enumeration; 035 } 036 037 /** 038 * Returns an iterator wrapping the underlying enumeration 039 */ 040 public Iterator<E> iterator() { 041 042 return new Iterator<E>() { 043 044 public boolean hasNext() { 045 return enumeration.hasMoreElements(); 046 } 047 048 public E next() { 049 return enumeration.nextElement(); 050 } 051 052 public void remove() { 053 throw new UnsupportedOperationException("IterableEnumeration does not support remove"); 054 } 055 }; 056 } 057 058 /** 059 * Typesafe factory method for construction convenience 060 * @param enumeration The enumeration to be wrapped with type-arg of E 061 * @param <E> Type-arg for the relevant enumeration 062 * @return An Iterable instance for subject type, E 063 */ 064 public static <E> Iterable<E> wrapEnumeration(Enumeration<E> enumeration) { 065 return new IterableEnumeration<E>(enumeration); 066 } 067}