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:ch.algotrader.dao.AbstractDao.java

protected static <T> List<T> convertToList(final List<?> list, final Class<? extends T> clazz) {

    List<T> results = new ArrayList<>(list.size());
    for (int i = 0; i < list.size(); i++) {
        T entity = clazz.cast(list.get(i));
        results.add(entity);// w  w  w. ja  v a2  s.  c o m
    }
    return results;
}

From source file:newcontroller.handler.impl.DefaultResponse.java

@Override
public <T> T unwrap(Class<T> clazz) {
    return clazz.cast(this.response);
}

From source file:io.fabric8.maven.core.util.KubernetesResourceUtil.java

/**
 * Returns the resource of the given kind and name from the collection or null
 *///from   www. ja  v  a 2 s  . c  o  m
public static <T> T findResourceByName(Iterable<HasMetadata> entities, Class<T> clazz, String name) {
    if (entities != null) {
        for (HasMetadata entity : entities) {
            if (clazz.isInstance(entity) && Objects.equals(name, getName(entity))) {
                return clazz.cast(entity);
            }
        }
    }
    return null;
}

From source file:ch.algotrader.dao.AbstractDao.java

protected static <T> Set<T> convertToSet(final List<?> list, final Class<? extends T> clazz) {

    Set<T> results = new LinkedHashSet<>(list.size());
    for (int i = 0; i < list.size(); i++) {
        T entity = clazz.cast(list.get(i));
        results.add(entity);//from   w  ww.j av a  2s  .  c  om
    }
    return results;
}

From source file:com.lexicalscope.fluentreflection.FluentObjectImpl.java

@Override
public <V> V as(final Class<V> asType) {
    return asType.cast(value());
}

From source file:com.athena.peacock.common.netty.PeacockDatagram.java

/**
 * @return the message
 */
public T getMessage(Class<T> clazz) {
    return clazz.cast(message);
}

From source file:hudson.Util.java

/**
 * Creates a filtered sublist.//from  www  .j a  va 2 s.  c o m
 * @since 1.176
 */
public static <T> List<T> filter(Iterable<?> base, Class<T> type) {
    List<T> r = new ArrayList<T>();
    for (Object i : base) {
        if (type.isInstance(i))
            r.add(type.cast(i));
    }
    return r;
}

From source file:newcontroller.handler.impl.DefaultRequest.java

@Override
public <T> T get(String key, Class<T> clazz) {
    return clazz.cast(this.request.getAttribute(key));
}

From source file:newcontroller.handler.impl.DefaultRequest.java

@Override
public <T> T unwrap(Class<T> clazz) {
    return clazz.cast(this.request);
}

From source file:com.temenos.useragent.generic.mediatype.JsonPayloadHandler.java

private <T> T getJsonResponse(Class<T> klass) {
    return klass.cast(jsonResponse.orElseThrow(() -> new IllegalArgumentException("Invalid JSON response")));
}