Example usage for java.lang Class isInstance

List of usage examples for java.lang Class isInstance

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInstance(Object obj);

Source Link

Document

Determines if the specified Object is assignment-compatible with the object represented by this Class .

Usage

From source file:Main.java

/**
 * Returns first component placed in the specified container which is
 * instance of specified class type or null if none found.
 *
 * @param container//from w  ww  .j a v a2 s.  c  om
 *            container to look for component in
 * @param componentClass
 *            component class
 * @param recursive
 *            whether to check all subcontainers or not
 * @param <T>
 *            component class type
 * @return first component placed in the specified container which is
 *         instance of specified class type or null if none found
 */
public static <T extends Component> T getFirst(final Container container, final Class<T> componentClass,
        final boolean recursive) {
    for (int i = 0; i < container.getComponentCount(); i++) {
        final Component component = container.getComponent(i);
        if (componentClass.isInstance(component)) {
            return (T) component;
        }
        if (recursive) {
            if (component instanceof Container) {
                final T first = getFirst((Container) component, componentClass, recursive);
                if (first != null) {
                    return first;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static Component getFirstChildComponent(Container container, Class ofType) {
    java.awt.Component[] comps = container.getComponents();
    Component comp = null;//from  w ww .  j ava2  s.  co m
    for (int n = 0; n < comps.length; n++) {
        System.out.println("WHat component am I " + n + ":" + comps[n] + " IN " + container);
        if (ofType.isInstance(comps[n]))
            return comps[n];
        if (comps[n] instanceof JComponent || comps[n] instanceof Container)
            comp = getFirstChildComponent((Container) comps[n], ofType);
        if (comp != null) {
            // Does this component have the focus?
            boolean hasFocus = comp.hasFocus();
            if (hasFocus)
                return comp;
        }
    }
    return null;
}

From source file:org.apache.nifi.minifi.c2.security.authorization.GrantedAuthorityAuthorizer.java

private static <T, E extends Throwable> T as(Class<T> clazz, Object object,
        Function<Object, E> exceptionSupplier) throws E {
    if (object == null) {
        return null;
    }//  w  ww  . ja v a  2  s  .  c  o  m
    if (!clazz.isInstance(object)) {
        throw exceptionSupplier.apply(object);
    }
    return clazz.cast(object);
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert/*from   w  w w  .  ja  v a 2 s  .co  m*/
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:com.jklas.sample.petclinic.util.EntityUtils.java

/**
 * Look up the entity of the given class with the given id
 * in the given collection.//from www .j  av a  2s .  c o  m
 * @param entities the collection to search
 * @param entityClass the entity class to look up
 * @param entityId the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static Entity getById(Collection entities, Class entityClass, int entityId)
        throws ObjectRetrievalFailureException {

    for (Iterator it = entities.iterator(); it.hasNext();) {
        Entity entity = (Entity) it.next();
        if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId));
}

From source file:net.ripe.rpki.commons.crypto.util.Asn1Util.java

/**
 * Checks if <code>value</code> is an instance of the
 * <code>expectedClass</code>.
 *
 * @throws IllegalArgumentException the instance is null or not an instance of the expected
 *                                  class.
 *//*  w w  w .  j av a 2  s .  c om*/
public static <T extends ASN1Encodable> T expect(ASN1Encodable value, Class<? extends T> expectedClass) {
    Validate.notNull(value, expectedClass.getSimpleName() + " expected, got null");
    Validate.isTrue(expectedClass.isInstance(value), expectedClass.getSimpleName() + " expected, got "
            + value.getClass().getSimpleName() + " with value: " + value);
    return expectedClass.cast(value);
}

From source file:com.github.vanroy.springdata.jest.CriteriaFilterProcessor.java

private static boolean isType(Object[] array, Class clazz) {
    for (Object o : array) {
        if (!clazz.isInstance(o)) {
            return false;
        }//w ww . j  av a2 s  .c  om
    }
    return true;
}

From source file:Main.java

/**
 * Find a single value of the given type in the given Collection.
 * @param collection the Collection to search
 * @param type the type to look for//  ww  w. ja v  a 2  s. c om
 * @return a value of the given type found if there is a clear match,
 * or <code>null</code> if none or more than one such value found
 */
public static Object findValueOfType(Collection collection, Class type) {
    if (isEmpty(collection)) {
        return null;
    }
    Object value = null;
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object obj = it.next();
        if (type == null || type.isInstance(obj)) {
            if (value != null) {
                // More than one value found... no clear single value.
                return null;
            }
            value = obj;
        }
    }
    return value;
}

From source file:fr.inria.atlanmod.neoemf.core.PersistentEObjectAdapter.java

/**
 * Returns the given {@code object} adapted in a specific {@code type}.
 *
 * @param adaptableObject the object to adapt
 * @param adapterType     the class in which the object must be adapted
 *
 * @return an adapted object in the given {@code type}, or {@code null} if the {@code object} cannot be assigned as
 * a {@code type}//  w  w w  . ja  va  2  s  .c  o m
 *
 * @throws NullPointerException if the {@code type} is {@code null}
 */
public static <T extends PersistentEObject> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (isNull(adaptableObject)) {
        return null;
    }
    checkNotNull(adapterType);

    Object adapter = null;
    if (adapterType.isInstance(adaptableObject)) {
        adapter = adaptableObject;
    } else if (adaptableObject instanceof InternalEObject) {
        adapter = ADAPTED_OBJECTS_CACHE.getIfPresent(adaptableObject);
        if (isNull(adapter) || !adapterType.isAssignableFrom(adapter.getClass())) {
            adapter = createAdapter(adaptableObject, adapterType);
            ADAPTED_OBJECTS_CACHE.put((InternalEObject) adaptableObject, (PersistentEObject) adapter);
        }
    }

    if (isNull(adapter)) {
        NeoLogger.warn("Unable to create a {0} adapter for this object of type {1}",
                adapterType.getSimpleName(), adaptableObject.getClass().getSimpleName());
    }

    return adapterType.cast(adapter);
}

From source file:org.lightadmin.core.util.NumberUtils.java

@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {
    Assert.notNull(number, "Number must not be null");
    Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (targetClass.equals(byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }/* w  w w  . j av  a2s.  c o m*/
        return (T) new Byte(number.byteValue());
    } else if (targetClass.equals(short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (targetClass.equals(int.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (targetClass.equals(long.class)) {
        return (T) new Long(number.longValue());
    } else if (targetClass.equals(float.class)) {
        return (T) Float.valueOf(number.toString());
    } else if (targetClass.equals(double.class)) {
        return (T) Double.valueOf(number.toString());
    } else {
        return org.springframework.util.NumberUtils.convertNumberToTargetClass(number, targetClass);
    }
}