List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
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 a2 s.co m }
From source file:Main.java
private static <T> T search(Object[] objs, Class c) { for (Object o : objs) { if (c.isInstance(o)) return (T) o; }/* w w w . ja v a2 s .co m*/ return null; }
From source file:Main.java
/** * filter list of random types and only return only the matching type * * @param <T>// ww w . ja va 2 s .c o 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
public static <T> T getClassByName(String className, Class<T> classOfT) { try {/*from w ww . j a v a 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
public static <T extends Component> void addChildren(Class<T> clazz, Collection<? super T> dst, Container parent) {/*from w ww . j a v a 2s.c om*/ for (Component c : parent.getComponents()) { if (clazz.isInstance(c)) dst.add(clazz.cast(c)); } }
From source file:Main.java
/** Quick checks for <em>possibly</em> determining equality of two objects. /*from w ww .j a v a 2 s.c o m*/ <P>This method exists to make <tt>equals</tt> implementations read more legibly, and to avoid multiple <tt>return</tt> statements. <P><em>It cannot be used by itself to fully implement <tt>equals</tt>. </em> It uses <tt>==</tt> and <tt>instanceof</tt> to determine if equality can be found cheaply, without the need to examine field values in detail. It is <em>always</em> paired with some other method (usually {@link #equalsFor(Object[], Object[])}), as in the following example : <PRE> public boolean equals(Object aThat){ Boolean result = ModelUtil.quickEquals(this, aThat); <b>if ( result == null ){</b> //quick checks not sufficient to determine equality, //so a full field-by-field check is needed : This this = (This) aThat; //will not fail result = ModelUtil.equalsFor(this.getSignificantFields(), that.getSignificantFields()); } return result; } </PRE> <P>This method is unusual since it returns a <tt>Boolean</tt> that takes <em>3</em> values : <tt>true</tt>, <tt>false</tt>, and <tt>null</tt>. Here, <tt>true</tt> and <tt>false</tt> mean that a simple quick check was able to determine equality. <span class='highlight'>The <tt>null</tt> case means that the quick checks were not able to determine if the objects are equal or not, and that further field-by-field examination is necessary. The caller must always perform a check-for-null on the return value.</span> */ static public Boolean quickEquals(Object aThis, Object aThat) { Boolean result = null; if (aThis == aThat) { result = Boolean.TRUE; } else { Class<?> thisClass = aThis.getClass(); if (!thisClass.isInstance(aThat)) { result = Boolean.FALSE; } } return result; }
From source file:Main.java
/** * Finds the first object in the heterogeneous list that is an instance of * the given class, removes it from the list, and returns it. * If there is not object in the list of the given type the list is left * unmodified and null is returned.//from www . j av a 2 s. c o m * * @throws NullPointerException if list or clazz is null * @throws UnsupportedOperationException if the list's Iterator does not support the remove() * method */ @SuppressWarnings("unchecked") public static <T> T findFirstAndRemove(List<?> list, Class<T> clazz) { for (Iterator<?> iter = list.iterator(); iter.hasNext();) { Object o = iter.next(); if (clazz.isInstance(o)) { iter.remove(); return (T) o; // safe } } return null; }
From source file:Main.java
public static <T> Collection<T> filter(final Collection<?> objects, final Class<T> filterType) { final Collection<T> filtered = new ArrayList<T>(); for (final Object object : objects) { if (filterType.isInstance(object)) { filtered.add(filterType.cast(object)); }/*from ww w .j a v a 2s .c o m*/ } return filtered; }
From source file:org.vaadin.spring.security.util.SecurityExceptionUtils.java
private static boolean hasExceptionOfTypeInChain(Class<? extends Throwable> type, Throwable throwable) { if (throwable == null) { return false; } else if (type.isInstance(throwable)) { return true; } else {/* ww w. j ava 2 s . co m*/ return hasExceptionOfTypeInChain(type, throwable.getCause()); } }
From source file:Main.java
/** * Generic-ified version of SwingUtilities.getAncestorOfClass() *///from w w w . j a v a2 s. c om public static <T> T getAncestorOfClass(Class<T> c, Component comp) { if (comp == null || c == null) return null; Container parent = comp.getParent(); while (parent != null && !(c.isInstance(parent))) parent = parent.getParent(); return (T) parent; }