下面列出了java.util.Collection#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String testInequality(Collection<?> s1, Collection<?> s2, InequalityTester t, String location)
{
if (s1.size() != s2.size())
{
return "@CI=" + location + ": Inequality in Set Size: " + s1 + " " + s2;
}
if (s1.equals(s2))
{
return null;
}
Collection<Object> l1 = new ArrayList<>(s1);
Collection<Object> l2 = new ArrayList<>(s2);
l1.removeAll(l2);
if (l1.isEmpty())
{
return null;
}
l2.removeAll(new ArrayList<Object>(s1));
return "@CI=" + location + ": " + s1.getClass() + " Inequality between: " + l1 + " " + l2;
}
/**
* Appends all of the elements in the specified collection to the end
* of this list, in the order that they are returned by the specified
* collection's iterator.
*
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws NullPointerException if the specified collection is null
* @see #add(Object)
*/
public boolean addAll(Collection<? extends E> c) {
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
if (cs.length == 0)
return false;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class)
setArray(cs);
else {
Object[] newElements = Arrays.copyOf(elements, len + cs.length);
System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements);
}
return true;
} finally {
lock.unlock();
}
}
/**
* Appends all of the elements in the specified collection to the end
* of this list, in the order that they are returned by the specified
* collection's iterator.
*
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws NullPointerException if the specified collection is null
* @see #add(Object)
*/
public boolean addAll(Collection<? extends E> c) {
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
if (cs.length == 0)
return false;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class)
setArray(cs);
else {
Object[] newElements = Arrays.copyOf(elements, len + cs.length);
System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements);
}
return true;
} finally {
lock.unlock();
}
}
/**
* Appends all of the elements in the specified collection to the end
* of this list, in the order that they are returned by the specified
* collection's iterator.
*
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws NullPointerException if the specified collection is null
* @see #add(Object)
*/
public boolean addAll(Collection<? extends E> c) {
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
if (cs.length == 0)
return false;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class)
setArray(cs);
else {
Object[] newElements = Arrays.copyOf(elements, len + cs.length);
System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements);
}
return true;
} finally {
lock.unlock();
}
}
/**
* Appends all of the elements in the specified collection to the end
* of this list, in the order that they are returned by the specified
* collection's iterator.
*
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws NullPointerException if the specified collection is null
* @see #add(Object)
*/
public boolean addAll(Collection<? extends E> c) {
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
if (cs.length == 0)
return false;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class)
setArray(cs);
else {
Object[] newElements = Arrays.copyOf(elements, len + cs.length);
System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements);
}
return true;
} finally {
lock.unlock();
}
}
/**
* Creates a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection of initially held elements
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements;
if (c.getClass() == CopyOnWriteArrayList.class)
elements = ((CopyOnWriteArrayList<?>)c).getArray();
else {
elements = c.toArray();
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class);
}
setArray(elements);
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
/**
* This will create a QueryHandler which first argument is a Collection of Workers.
* The other arguments have to be of the specified classes
*
*
* @param className The class name of the QueryHandler
* @param constructorArgs2 The constructor arguments
* @param constructorClasses2 The classes of the constructor arguments
* @param workers the List of all workers which queries should be generated
* @return the QueryHandler
*/
public QueryHandler createWorkerBasedQueryHandler(String className,
Object[] constructorArgs2, Class<?>[] constructorClasses2, Collection<Worker> workers) {
Object[] constructorArgs = new Object[1+constructorArgs2.length];
Class<?>[] constructorClasses = new Class<?>[1+constructorClasses2.length];
constructorArgs[0] = workers;
constructorClasses[0] = workers.getClass();
for(int i=1;i<constructorArgs.length;i++) {
constructorArgs[i] = constructorArgs2[i-1];
constructorClasses[i] = constructorClasses2[i-1];
}
return create(className, constructorArgs, constructorClasses);
}
/**
* Creates a set containing all of the elements of the specified
* collection.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArraySet(Collection<? extends E> c) {
if (c.getClass() == CopyOnWriteArraySet.class) {
@SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc =
(CopyOnWriteArraySet<E>)c;
al = new CopyOnWriteArrayList<E>(cc.al);
}
else {
al = new CopyOnWriteArrayList<E>();
al.addAllAbsent(c);
}
}
/**
* Creates a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection of initially held elements
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements;
if (c.getClass() == CopyOnWriteArrayList.class)
elements = ((CopyOnWriteArrayList<?>)c).getArray();
else {
elements = c.toArray();
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class);
}
setArray(elements);
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
/**
* Creates a set containing all of the elements of the specified
* collection.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArraySet(Collection<? extends E> c) {
if (c.getClass() == CopyOnWriteArraySet.class) {
@SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc =
(CopyOnWriteArraySet<E>)c;
al = new CopyOnWriteArrayList<E>(cc.al);
}
else {
al = new CopyOnWriteArrayList<E>();
al.addAllAbsent(c);
}
}
public static String serializeCollection( Collection<?> object, boolean serializeType, boolean serializeElementType ){
if( object == null ){
return null;
}
Class<?> type = object.getClass();
return gson.toJson( convertCollection( object, type, serializeType, serializeElementType ) );
}
/**
* Creates a set containing all of the elements of the specified
* collection.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArraySet(Collection<? extends E> c) {
if (c.getClass() == CopyOnWriteArraySet.class) {
@SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc =
(CopyOnWriteArraySet<E>)c;
al = new CopyOnWriteArrayList<E>(cc.al);
}
else {
al = new CopyOnWriteArrayList<E>();
al.addAllAbsent(c);
}
}
/**
* Creates a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection of initially held elements
* @throws NullPointerException if the specified collection is null
*/
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements;
if (c.getClass() == CopyOnWriteArrayList.class)
elements = ((CopyOnWriteArrayList<?>)c).getArray();
else {
elements = c.toArray();
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class);
}
setArray(elements);
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}
@Test(dataProvider = "collections")
public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {
Class cls = c.getClass();
for (Method m: defaultMethods) {
Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());
// default had been override
assertFalse(m2.isDefault(), cls.getCanonicalName());
}
}