Java ListIterator Usage castIterator(final Iterator iterator)

Here you can find the source of castIterator(final Iterator iterator)

Description

Creates a iterator of the desired type based on the given iterator

Warning This operation is not type safe.

License

Open Source License

Parameter

Parameter Description
S the generic type
D the generic type
iterator the iterator

Return

an iterator of type Iterator<D> based on the given iterator

Declaration

public static <S, D> Iterator<D> castIterator(final Iterator<S> iterator) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

import java.util.ListIterator;

public class Main {
    /**/*from  ww w.ja v  a 2  s.c  o m*/
     * Creates a iterator of the desired type based on the given iterator
     * <p/>
     * <b>Warning</b> This operation is not type safe.
     * 
     * @param <S>
     *            the generic type
     * @param <D>
     *            the generic type
     * @param iterator
     *            the iterator
     * @return an iterator of type <code>Iterator<</>D></code> based on the given iterator
     */
    public static <S, D> Iterator<D> castIterator(final Iterator<S> iterator) {
        return new Iterator<D>() {
            private final Iterator<S> it = iterator;

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public D next() {
                return (D) it.next();
            }

            @Override
            public void remove() {
                it.remove();
            }
        };
    }

    /**
     * Creates a iterator of the desired type based on the given iterator
     * <p/>
     * <b>Warning</b> This operation is not type safe.
     * 
     * @param <S>
     *            the generic type
     * @param <D>
     *            the generic type
     * @param iterator
     *            the iterator
     * @return an iterator of type <code>Iterator<</>D></code> based on the given iterator
     */
    public static <S, D> ListIterator<D> castIterator(final ListIterator<S> iterator) {
        return new ListIterator<D>() {
            private final ListIterator<S> it = iterator;

            @SuppressWarnings("unchecked")
            @Override
            public void add(D e) {
                it.add((S) e);
            }

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public boolean hasPrevious() {
                return it.hasPrevious();
            }

            @SuppressWarnings("unchecked")
            @Override
            public D next() {
                return (D) it.next();
            }

            @Override
            public int nextIndex() {
                return it.nextIndex();
            }

            @SuppressWarnings("unchecked")
            @Override
            public D previous() {
                return (D) it.previous();
            }

            @Override
            public int previousIndex() {
                return it.previousIndex();
            }

            @Override
            public void remove() {
                it.remove();
            }

            @SuppressWarnings("unchecked")
            @Override
            public void set(D e) {
                it.set((S) e);

            }
        };
    }
}

Related

  1. clearNulls(Collection col)
  2. containsUsingListIteratorNext(List list, Integer value)
  3. createCommaSeparatedListOfFullTypeNames(ListIterator strIt)
  4. differingDigits(final int lhs, final int rhs, final int base)