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

public static <T extends Object> List<T> asList(ListModel listModel, Class<T> type) {

    List<T> res = new ArrayList<T>();
    for (int i = 0; i < listModel.getSize(); i++) {
        res.add(type.cast(listModel.getElementAt(i)));
    }/*from w w  w  .j  a v  a 2  s. c  om*/
    return res;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> checkMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) {
    if (DEBUG) {/*from www. j av a  2s .  co  m*/
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            keyType.cast(entry.getKey());
            valueType.cast(entry.getValue());
        }
    }
    return (Map<K, V>) map;
}

From source file:Main.java

public static <T> T findChildByName(Container container, Class<T> returnType, String name) {

    if (name.equals(container.getName())) {
        return returnType.cast(container);
    }//from   w w w.j a  v  a  2 s .com

    for (Component component : container.getComponents()) {
        if (name.equals(component.getName())) {
            return returnType.cast(component);
        } else if (component instanceof Container) {
            T recursiveResult = findChildByName((Container) component, returnType, name);

            if (recursiveResult != null) {
                return returnType.cast(recursiveResult);
            }
        }
    }

    throw new IllegalArgumentException(
            "Did not find child component by name [" + name + "] in the specified container!");
}

From source file:Main.java

public static <T extends Parcelable> Map<String, T> fromBundle(Bundle input, Class<T> c) {
    Map<String, T> output = new HashMap<String, T>();
    for (String key : input.keySet()) {
        output.put(key, c.cast(input.getParcelable(key)));
    }/*from w  w w .  j a v a2  s  .c o  m*/
    return output;
}

From source file:io.github.restdocsext.jersey.JerseyRestDocumentationFilter.java

private static <T> T getConfigProperty(ClientRequestContext requestContext, String property, Class<T> cls) {
    return cls.cast(requestContext.getConfiguration().getProperty(property));
}

From source file:Main.java

static public <T extends Container> T findAncestorOfType(Container acomp, Class<T> type) {
    Container parent = acomp;/*from  w  w w . j  ava  2  s .c  o  m*/
    while (parent != null) {
        if (type.isInstance(parent))
            return type.cast(parent);
        parent = parent.getParent();
    }
    return null;
}

From source file:Main.java

public static <T> List<T> subListByType(Collection<?> input, Class<T> type) {
    List<T> result = new ArrayList<T>();
    for (Object s : input) {
        if (type.isInstance(s)) {
            T t = type.cast(s);
            result.add(t);//from  w w w. ja v a2s.  c o  m
        }
    }
    return result;
}

From source file:Main.java

@NonNull
public static <T> List<T> childrenOfType(@NonNull View root, @NonNull Class<T> type) {
    final List<T> children = new ArrayList<>();
    if (type.isInstance(root)) {
        children.add(type.cast(root));
    }/*ww  w.j  a  va 2 s.  c  o m*/
    if (root instanceof ViewGroup) {
        final ViewGroup rootGroup = (ViewGroup) root;
        for (int i = 0; i < rootGroup.getChildCount(); i++) {
            final View child = rootGroup.getChildAt(i);
            children.addAll(childrenOfType(child, type));
        }
    }
    return children;
}

From source file:Main.java

/**
 * Filters the src collection and puts the objects matching the
 * clazz into the dest collection.//  w ww .j ava2s  .c om
 */
public static <T> void filter(Class<T> clazz, Collection<?> src, Collection<T> dest) {
    for (Object o : src) {
        if (clazz.isInstance(o)) {
            dest.add(clazz.cast(o));
        }
    }
}

From source file:Main.java

private static <T extends Collection<E>, E> T genericCollection(Collection<?> collection, Class<E> elementType,
        T genericCollection) {//ww w.  j a  v a 2  s  . c o m
    for (Object element : collection) {
        genericCollection.add(elementType.cast(element));
    }

    return genericCollection;
}