Java ArrayList Create getArrayList(final Object o, final Class clazz)

Here you can find the source of getArrayList(final Object o, final Class clazz)

Description

Cast an object into a typed ArrayList .

License

Apache License

Parameter

Parameter Description
o The input object
clazz The output class
T The type of the output

Return

The casted object

Declaration

public static <T> List<T> getArrayList(final Object o, final Class<T> clazz) 

Method Source Code

//package com.java2s;
/*//  w  w  w  . jav  a  2 s.co m
 * #%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.List;

public class Main {
    /**
     * Cast an object into a typed {@link ArrayList}.
     * 
     * @param o
     *            The input object
     * @param clazz
     *            The output class
     * @return The casted object
     * @param <T>
     *            The type of the output
     */
    public static <T> List<T> getArrayList(final Object o, final Class<T> clazz) {
        final List<T> list = new ArrayList<>();

        list(list, o, clazz);

        return list;
    }

    /**
     * Cast the object and feel the list.
     * 
     * @param list
     *            The output list
     * @param o
     *            The input object
     * @param classElement
     *            The typed class
     */
    private static <T> void list(final List<T> list, final Object o, final Class<T> clazz) {
        if (o != null && clazz != null && List.class.isAssignableFrom(o.getClass())) {
            List<?> mObj = (List<?>) o;
            for (Object obj : mObj) {
                setListValue(list, obj, clazz);
            }
        }
    }

    /**
     * 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;
    }

    /**
     * Set the list value.
     * 
     * @param list
     *            the list (required)
     * @param obj
     *            the object (nullable)
     * @param clazz
     *            the class (required)
     * @param <T>
     *            The type of the element
     */
    private static <T> void setListValue(final List<T> list, final Object obj, final Class<T> clazz) {
        if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
            list.add(clazz.cast(obj));
        } else if (obj == null) {
            list.add(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. createArrayListFilledIncrement(int count)
  2. createArrayListOfArrayList(int size)
  3. getArrayList()
  4. getArrayList()
  5. getArrayList()
  6. getArrayList(List list)
  7. getArrayList(Map map, Object obj)
  8. getArrayList(String[] args)
  9. getArrayListForArray(Object[] rowData)