Example usage for java.lang Class cast

List of usage examples for java.lang Class cast

Introduction

In this page you can find the example usage for java.lang Class cast.

Prototype

@SuppressWarnings("unchecked")
@HotSpotIntrinsicCandidate
public T cast(Object obj) 

Source Link

Document

Casts an object to the class or interface represented by this Class object.

Usage

From source file:Main.java

/**
 * Gets a list of all views of a given type, rooted at the given parent.
 * <p>//from ww w  .  j a v  a 2s. c o  m
 * This method will recurse down through all {@link ViewGroup} instances looking for
 * {@link View} instances of the supplied class type. Specifically it will use the
 * {@link Class#isAssignableFrom(Class)} method as the test for which views to add to the list,
 * so if you provide {@code View.class} as your type, you will get every view. The parent itself
 * will be included also, should it be of the right type.
 * <p>
 * This call manipulates the ui, and as such should only be called from the application's main
 * thread.
 */
private static <T extends View> List<T> getAllViews(final Class<T> clazz, final View parent) {
    List<T> results = new ArrayList<T>();
    if (parent.getClass().equals(clazz)) {
        results.add(clazz.cast(parent));
    }
    if (parent instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) parent;
        for (int i = 0; i < viewGroup.getChildCount(); ++i) {
            results.addAll(getAllViews(clazz, viewGroup.getChildAt(i)));
        }
    }
    return results;
}

From source file:Main.java

public static <T> T firstOfType(List<Object> list, Class<T> targetClass) {
    for (Object object : list) {
        boolean isTargetClass = object.getClass().equals(targetClass);
        if (isTargetClass) {
            return targetClass.cast(object);
        }//w w  w.j  a va  2s.c o  m
    }

    return null;
}

From source file:Main.java

/**
 * filter list of random types and only return only the matching type
 *
 * @param <T>//from ww w.j  ava  2 s.co  m
 * @param clazz
 * @param values
 * @return list of <T>
 */
public static <T> ArrayList<T> filterType(Class<T> clazz, Iterable<?> values) {
    ArrayList<T> list = new ArrayList<T>();
    for (Object o : values) {
        if (clazz.isInstance(o)) {
            list.add(clazz.cast(o));
        }
    }
    return list;
}

From source file:Main.java

@Nullable
public static <E> E firstElementOfType(@Nonnull final Class<E> type,
        @Nonnull final Iterable<? super E> elements) {

    for (final Object element : elements)
        if (type.isInstance(element))
            return type.cast(element);

    return null;/*ww w.j av  a  2  s  . c o  m*/
}

From source file:Main.java

public static <T extends Component> void addChildren(Class<T> clazz, Collection<? super T> dst,
        Container parent) {/*from  w w  w  .  ja  v  a  2 s  . c  o m*/
    for (Component c : parent.getComponents()) {
        if (clazz.isInstance(c))
            dst.add(clazz.cast(c));
    }
}

From source file:Main.java

public static <T> T deserialize(Class<T> res, InputStream is) throws JAXBException {
    Unmarshaller u = CTX.createUnmarshaller();
    u.setEventHandler(new DefaultValidationEventHandler());
    return res.cast(u.unmarshal(is));
}

From source file:com.bitranger.parknshop.common.recommend.util.Functional.java

public static <F, T> Function<F, T> cast(final Class<T> target) {
    return new Function<F, T>() {
        @Override//  www  .  j  a  v a  2  s. co m
        public T apply(F obj) {
            return target.cast(obj);
        }
    };
}

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, XMLStreamReader reader) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    return objectClass.cast(u.unmarshal(reader));
}

From source file:Main.java

public static <T> T getClassByName(String className, Class<T> classOfT) {
    try {/*w ww  .j  a  va  2s  . c  o m*/
        Class<?> clazz = Class.forName(className);
        Object instance = clazz.newInstance();
        if (classOfT.isInstance(instance)) {
            return classOfT.cast(instance);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Ensures that all elements of the given collection can be cast to a desired type.
 * //ww  w  . j a  va2 s.c  om
 * @param collection the collection to check
 * @param type the desired type
 * @return a collection of the desired type
 * @throws ClassCastException if an element cannot be cast to the desired type
 */
@SuppressWarnings("unchecked")
public static <E> Collection<E> checkCollection(Collection<?> collection, Class<E> type) {
    if (DEBUG) {
        Collection<E> copy = new ArrayList<E>(collection.size());
        for (Object element : collection) {
            copy.add(type.cast(element));
        }
        return copy;
    }
    return (Collection<E>) collection;
}