Java List Cast cast(final Class cls, final Object[] list)

Here you can find the source of cast(final Class cls, final Object[] list)

Description

type safe cast from any array to an Iterable

License

Apache License

Parameter

Parameter Description
T a parameter
cls a parameter
list a parameter

Declaration

public static <T> Iterable<T> cast(final Class<T> cls, final Object[] list) 

Method Source Code

//package com.java2s;
/**/*w ww  .jav a 2 s . com*/
 * Copyright 2010 Molindo GmbH
 *
 * 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.
 */

import java.util.Arrays;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    public static final Iterator<?> EMPTY_ITERATOR = new Iterator<Object>() {

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Object next() {
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new IllegalStateException();
        }

    };

    /**
     * type safe cast from any array to an {@link Iterable}
     * 
     * @param <T>
     * @param cls
     * @param list
     * @return
     */
    public static <T> Iterable<T> cast(final Class<T> cls, final Object[] list) {
        return cast(cls, Arrays.asList(list));
    }

    /**
     * a type safe cast for {@link Iterable}
     * 
     * @param <T>
     * @param cls
     * @param iterable
     * @return
     */
    public static <T> Iterable<T> cast(final Class<T> cls, final Iterable<?> iterable) {
        if (cls == null) {
            throw new NullPointerException("cls");
        }
        if (iterable == null) {
            throw new NullPointerException("iterable");
        }

        return new Iterable<T>() {

            @Override
            public Iterator<T> iterator() {
                return new Iterator<T>() {

                    private final Iterator<?> _iter = iterable.iterator();

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

                    @Override
                    public T next() {
                        return cls.cast(_iter.next());
                    }

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

    /**
     * @param <T>
     * @param iterable
     * @return {@link Iterable#iterator()} or {@link #EMPTY_ITERATOR} if null
     */
    @SuppressWarnings("unchecked")
    public static <T> Iterator<T> iterator(Iterable<T> iterable) {
        return iterable == null ? (Iterator<T>) EMPTY_ITERATOR : iterable.iterator();
    }

    /**
     * 
     * @param <T>
     * @param iter
     * @return the next element if available, <code>null</code> otherwise
     */
    public static <T> T next(final Iterator<T> iter) {
        return iter == null || !iter.hasNext() ? null : iter.next();
    }
}

Related

  1. cast(Class clazz, List list)
  2. cast(final Collection list)
  3. cast(final List list)
  4. cast(List list)
  5. cast(List list)