Wraps the given enumeration in an Iterator . - Android java.util

Android examples for java.util:Iterator

Description

Wraps the given enumeration in an Iterator .

Demo Code

/*//from   ww  w . jav a  2s .c  om
 * Copyright 2012 Marco Soeima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
//package com.book2s;

import java.util.Enumeration;

import java.util.Iterator;

public class Main {
    /**
     * Wraps the given <code>enumeration</code> in an {@link Iterator}.
     *
     * @param   <E>
     * @param   enumeration  The {@link Enumeration} to wrap inside of an {@link Iterator}.
     *
     * @return  An {@link Iterator} for the given <code>enumeration</code>.
     *
     * @throws  UnsupportedOperationException  If the {@link Iterator#remove()} method is invoked.
     */
    public static <E> Iterator<E> asIterator(
            final Enumeration<E> enumeration) {
        return new Iterator<E>() {

            /**
             * @see  Iterator#hasNext()
             */
            @Override
            public boolean hasNext() {
                return enumeration.hasMoreElements();
            }

            /**
             * @see  Iterator#next()
             */
            @Override
            public E next() {
                return enumeration.nextElement();
            }

            /**
             * @see  Iterator#remove()
             */
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
}

Related Tutorials