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:hudson.Util.java

/**
 * Create a sub-list by only picking up instances of the specified type.
 *//*ww w.j a  va 2s.  c o m*/
public static <T> List<T> createSubList(Collection<?> source, Class<T> type) {
    List<T> r = new ArrayList<T>();
    for (Object item : source) {
        if (type.isInstance(item)) {
            r.add(type.cast(item));
        }
    }
    return r;
}

From source file:org.esigate.parser.ParserContextImpl.java

@Override
public <T> T findAncestor(Class<T> type) {
    T result = null;/*from  ww  w. j a v  a2  s. c  om*/
    for (int i = stack.size() - 1; i > -1; i--) {
        Element currentElement = stack.elementAt(i).element;
        if (type.isInstance(currentElement)) {
            result = type.cast(currentElement);
            break;
        }
    }
    // try with root
    if (result == null && type.isInstance(root.root)) {
        result = type.cast(root.root);
    }

    return result;
}

From source file:com.meltmedia.cadmium.core.config.impl.YamlConfigurationParser.java

/**
 * Helper method used to retrieve the value from a given map if it matches the type specified.
 * /*from   www  . j  a  v  a  2s.c o  m*/
 * @param configs The map to pull from.
 * @param key The key to pull.
 * @param type The expected type of the value.
 * @return
 */
private <T> T getEnvConfig(Map<String, ?> configs, String key, Class<T> type) {
    if (configs.containsKey(key)) {
        Object config = configs.get(key);
        if (type.isAssignableFrom(config.getClass())) {
            return type.cast(config);
        }
    }
    return null;
}

From source file:com.cloudbees.jenkins.plugins.mtslavescloud.templates.SlaveTemplateList.java

/**
 * Convenience method to create a new empty model.
 *//*from w  w w .  j  a va 2 s .com*/
public synchronized <T extends SlaveTemplate> T createTemplate(Class<T> templateType, String name)
        throws IOException {
    return templateType.cast(createTemplate(
            (SlaveTemplateDescriptor) Jenkins.getInstance().getDescriptorOrDie(templateType), name));
}

From source file:org.apache.taverna.scufl2.translator.t2flow.defaultactivities.AbstractActivityParser.java

/**
 * Deals with the not-so-helpful/*from  w w w .j a  va 2s. co m*/
 * getHandledReferenceSchemesOrTranslatedElementTypeOrName method
 * 
 * @param portBean
 * @param elementName
 * @return
 */
private <T> T getPortElement(ActivityPortDefinitionBean portBean, String elementName, Class<T> type) {
    for (JAXBElement<?> elem : portBean.getHandledReferenceSchemesOrTranslatedElementTypeOrName())
        if (elem.getName().getLocalPart().equals(elementName))
            return type.cast(elem.getValue());
    return null;
}

From source file:com.github.rinde.rinsim.core.model.comm.CommModel.java

@Override
@Nonnull//from   w  ww.j  a v  a2s. c  om
public <U> U get(Class<U> clazz) {
    checkArgument(clazz == CommModel.class);
    return clazz.cast(this);
}

From source file:edu.jhuapl.graphs.jfreechart.TimeSeriesRenderer.java

private <T> T getItemProperty(int row, int column, Class<T> klazz, T defaultValue, String key) {

    Map<String, Object> metadata = getMetadata(row, column);
    if (metadata == null) {
        return defaultValue;
    }//from   ww  w  .j a v a  2 s  .  c  o  m

    Object o = metadata.get(key);
    if (o != null && klazz.isInstance(o)) {
        return klazz.cast(o);
    } else {
        return defaultValue;
    }
}

From source file:org.focusns.common.event.support.EventContext.java

@SuppressWarnings("unchecked")
public <T> T getArgument(String argName, Class<?> requiredType) {
    Object paramValue = arguments.get(argName);
    ////ww w  .  j a v a  2  s.  com
    if (paramValue == null) {
        return null;
    }
    if (ClassUtils.isAssignableValue(requiredType, paramValue)) {
        return (T) paramValue;
    }
    //
    return (T) requiredType.cast(paramValue);
}

From source file:com.dell.asm.asmcore.asmmanager.client.vsphere.ManagedObjectDTO.java

private <T> List<T> getChildrenHelper(Class<T> klazz, boolean getAllDescendants) {
    List<T> ret = new ArrayList<>();
    for (ManagedObjectDTO child : children) {
        if (klazz.isAssignableFrom(child.getClass())) {
            T cast = klazz.cast(child);
            ret.add(cast);//from   w w w. j a  v a2  s . c om
        }
        if (getAllDescendants) {
            ret.addAll(child.getChildrenHelper(klazz, getAllDescendants));
        }
    }
    return ret;
}

From source file:com.haulmont.cuba.web.gui.components.WebAbstractComponent.java

@Override
public <X> X unwrap(Class<X> internalComponentClass) {
    return internalComponentClass.cast(getComponent());
}