Java Iterator from getIterator(final Object o, final Class classElement)

Here you can find the source of getIterator(final Object o, final Class classElement)

Description

Cast an object into a typed iterator.

License

Apache License

Parameter

Parameter Description
o The input object
classElement The class of the element in the iterator
T The type of the element

Return

The casted object

Declaration

public static <T> Iterator<T> getIterator(final Object o, final Class<T> classElement) 

Method Source Code

//package com.java2s;
/*/*from  w  w w.  j ava2 s  .c om*/
 * #%L
 * utils-commons
 * %%
 * Copyright (C) 2016 - 2018 Gilles Landel
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Cast an object into a typed iterator.
     * 
     * @param o
     *            The input object
     * @param classElement
     *            The class of the element in the iterator
     * @return The casted object
     * @param <T>
     *            The type of the element
     */
    public static <T> Iterator<T> getIterator(final Object o, final Class<T> classElement) {
        final List<T> list = new ArrayList<>();

        if (o != null && classElement != null && Iterator.class.isAssignableFrom(o.getClass())) {
            Iterator<?> mObj = (Iterator<?>) o;
            while (mObj.hasNext()) {
                Object obj = mObj.next();
                if (obj == null) {
                    list.add(null);
                } else if (classElement.isAssignableFrom(obj.getClass())) {
                    final T element = classElement.cast(obj);
                    list.add(element);
                }
            }
        }

        return list.iterator();
    }

    /**
     * Get the class of the object ({@code null} safe).
     * 
     * @param object
     *            The object (required)
     * @param <T>
     *            The object type
     * @return The class of the object or null
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> getClass(final T object) {
        if (object != null) {
            return (Class<T>) object.getClass();
        }
        return null;
    }

    /**
     * Auto cast an object.
     * 
     * @param object
     *            The object (required)
     * @param <T>
     *            The object type
     * @return The casted object
     * @throws ClassCastException
     *             on cast failure
     */
    @SuppressWarnings("unchecked")
    public static <T> T cast(final Object object) {
        return (T) object;
    }

    /**
     * Cast an object into the specified class (null safe).
     * 
     * @param o
     *            The input object (required)
     * @param clazz
     *            The output class (required)
     * @return The casted object or {@code null}
     * @param <T>
     *            The type of the output
     */
    public static <T> T cast(final Object o, final Class<T> clazz) {
        if (o != null && clazz != null && clazz.isAssignableFrom(o.getClass())) {
            return clazz.cast(o);
        }
        return null;
    }
}

Related

  1. getEnumeratedObjectCount(Iterator objects)
  2. getFirstChildrendIterator(List children)
  3. getIndexInIterator(Iterator iterator, int index)
  4. getIterable(final Iterator i)
  5. getIterator(final int[] p, final Object[] data)
  6. getIterator(List list)
  7. getIterator(T[] array)
  8. getIteratorAsString(Iterator iter, final String separator)
  9. getIteratorOptionalValue(final Iterator stringIterator)