List of usage examples for java.util Collection getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
@SuppressWarnings("rawtypes") public static void printInterfaceChecks(Collection c) { System.out.println(c.getClass().getName() + " Implememnts RandomAccess? " + (c instanceof RandomAccess)); System.out.println(c.getClass().getName() + " Implememnts Serializable? " + (c instanceof Serializable)); System.out.println(c.getClass().getName() + " Implememnts cloneable? " + (c instanceof Cloneable)); }
From source file:Main.java
/** * Used to determine immutability of a collection to pass the test #6 :) * @param c - a collection/*from ww w.jav a 2 s . c o m*/ * @return {@code true} if this collection is one of standard immutable collections */ public static boolean isImmutable(Collection<?> c) { String className = c.getClass().getName(); return unmodifiableCollectionClassNamePattern.matcher(className).matches(); }
From source file:Main.java
private static <A, B> Collection<A> buildFrom(Collection<B> as) { try {// ww w .j a v a 2 s . c o m return as.getClass().newInstance(); } catch (Exception e) { throw new IllegalArgumentException("Type " + as.getClass() + " needs a parameterless constructor"); } }
From source file:Main.java
private static <T> Collection<T> newCollection(Collection<T> coll) { try {//from w w w .ja v a2s . com Class cl = coll.getClass(); Constructor con = cl.getConstructor(new Class[0]); return (Collection<T>) con.newInstance(new Object[0]); } catch (Exception e) { if (coll instanceof List) return new LinkedList<T>(); if (coll instanceof Set) return new HashSet<T>(); throw new RuntimeException("Cannot handle this collection"); } }
From source file:Main.java
/** * Creates a new empty instance of the provided collection * /*from ww w. j a v a 2s .c om*/ * @param <T> * @param in * @return */ public static <T> Collection<T> createEmpty(Collection<?> in) { Class<?> originalClass = in.getClass(); try { Constructor<?> originalConstructor = originalClass.getConstructor(new Class[0]); return (Collection<T>) originalConstructor.newInstance(new Object[0]); } catch (IllegalArgumentException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } catch (SecurityException e) { } catch (NoSuchMethodException e) { } finally { return null; } }
From source file:Main.java
private static Collection getEmpty(Collection collection) { try {//from ww w . ja v a 2 s .c om return (Collection) collection.getClass().newInstance(); } catch (Exception e) { if (collection instanceof List) { return new ArrayList(); } else { return new HashSet(); } } }
From source file:Main.java
/** * Returns a filtered copy of the collection <code>coll</code>. * * @param coll the collection to filter. * @param p the predicate to filter by./*from ww w . java 2 s . c o m*/ * @return a new collection. */ /*public static <T> Collection<T> filter(Collection<T> coll, Predicate<T> p){ Collection<T> c2 = newCollection(coll); for(T obj: coll) if(p.apply(obj)) c2.add(obj); return c2; }*/ private static <T> Collection<T> cloneCollection(Collection<T> coll) { try { Class cl = coll.getClass(); Constructor con = cl.getConstructor(new Class[] { Collection.class }); return (Collection<T>) con.newInstance(new Object[] { coll }); } catch (Exception e) { if (coll instanceof List) return new LinkedList<T>(coll); if (coll instanceof Set) return new HashSet<T>(coll); throw new RuntimeException("Cannot handle this collection"); } }
From source file:Main.java
/** * Create and answer a collection that is the same type, or the closest * equivalent of col./*from w w w. j a v a2s.c o m*/ * * @param col * @return */ public static Collection createCollection(Collection col) { Collection answer; try { answer = (Collection) col.getClass().newInstance(); } catch (Exception e) { if (col instanceof List) { answer = new ArrayList(); } else if (col instanceof SortedSet) { answer = new TreeSet(); } else if (col instanceof Set) { answer = new HashSet(); } else { answer = new ArrayList(); // should this throw an exception? } } return answer; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] toArray(Collection<T> c) { if (c == null) { return null; } else {/*from w w w .j a v a2s . co m*/ T[] result = (T[]) Array.newInstance(c.getClass().getComponentType(), c.size()); int i = 0; for (T r : c) { result[i] = r; i++; } return result; } }
From source file:therian.operator.immutablecheck.DefaultImmutableChecker.java
private static void addTypeTo(final Set<Class<?>> target, final Collection<?> coll) { addImmutableTypeTo(target, coll.getClass()); addImmutableTypeTo(target, coll.iterator().getClass()); }