HBASE-18656 First issues found by error-prone

This commit is contained in:
Mike Drob 2017-08-23 16:43:50 -05:00
parent 016ead7936
commit 9c26a42ab1
4 changed files with 15 additions and 84 deletions

View File

@ -18,10 +18,8 @@
*/
package org.apache.hadoop.hbase.util;
import java.lang.reflect.Array;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
@ -34,7 +32,7 @@ import org.apache.hadoop.hbase.classification.InterfaceAudience;
* NOTE: Doesn't implement list as it is not necessary for current usage, feel free to add.
*/
@InterfaceAudience.Private
public class ConcatenatedLists<T> implements Collection<T> {
public class ConcatenatedLists<T> extends AbstractCollection<T> {
protected final ArrayList<List<T>> components = new ArrayList<List<T>>();
protected int size = 0;
@ -56,77 +54,6 @@ public class ConcatenatedLists<T> implements Collection<T> {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public boolean contains(Object o) {
for (List<T> component : this.components) {
if (component.contains(o)) return true;
}
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o)) return false;
}
return true;
}
@Override
public Object[] toArray() {
return toArray((Object[])Array.newInstance(Object.class, this.size));
}
@Override
@SuppressWarnings("unchecked")
public <U> U[] toArray(U[] a) {
U[] result = (a.length == this.size()) ? a
: (U[])Array.newInstance(a.getClass().getComponentType(), this.size);
int i = 0;
for (List<T> component : this.components) {
for (T t : component) {
result[i] = (U)t;
++i;
}
}
return result;
}
@Override
public boolean add(T e) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends T> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public java.util.Iterator<T> iterator() {
return new Iterator();

View File

@ -38,6 +38,8 @@ import org.junit.experimental.categories.Category;
@Category(SmallTests.class)
public class TestChoreService {
public static final Log log = LogFactory.getLog(TestChoreService.class);
/**
* A few ScheduledChore samples that are useful for testing with ChoreService
*/
@ -77,7 +79,7 @@ public class TestChoreService {
try {
Thread.sleep(getPeriod() * 2);
} catch (InterruptedException e) {
e.printStackTrace();
log.warn("", e);
}
return true;
}
@ -87,7 +89,7 @@ public class TestChoreService {
try {
Thread.sleep(getPeriod() * 2);
} catch (InterruptedException e) {
//e.printStackTrace();
log.warn("", e);
}
}
}
@ -128,7 +130,7 @@ public class TestChoreService {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
log.warn("", e);
}
return true;
}
@ -138,7 +140,7 @@ public class TestChoreService {
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
System.err.println(e.getStackTrace());
log.warn("", e);
}
}
}
@ -176,7 +178,7 @@ public class TestChoreService {
}
private void outputTickCount() {
System.out.println("Chore: " + getName() + ". Count of chore calls: " + countOfChoreCalls);
log.info("Chore: " + getName() + ". Count of chore calls: " + countOfChoreCalls);
}
public int getCountOfChoreCalls() {

View File

@ -63,7 +63,7 @@ public class TestConcatenatedLists {
} catch (UnsupportedOperationException ex) {
}
try {
c.retainAll(Arrays.asList(0L, 1L));
c.retainAll(Arrays.asList(0L, 2L));
fail("Should throw");
} catch (UnsupportedOperationException ex) {
}
@ -114,9 +114,11 @@ public class TestConcatenatedLists {
verify(c, 7);
}
@SuppressWarnings("ModifyingCollectionWithItself")
private void verify(ConcatenatedLists<Long> c, int last) {
assertEquals((last == -1), c.isEmpty());
assertEquals(last + 1, c.size());
// This check is O(n^2), test with care
assertTrue(c.containsAll(c));
Long[] array = c.toArray(new Long[c.size()]);
List<Long> all = new ArrayList<Long>();

View File

@ -43,7 +43,7 @@ public class TestDrainBarrier {
DrainBarrier barrier = new DrainBarrier();
try {
barrier.endOp();
fail("Should have asserted");
throw new Error("Should have asserted");
} catch (AssertionError e) {
}
@ -53,7 +53,7 @@ public class TestDrainBarrier {
barrier.endOp();
try {
barrier.endOp();
fail("Should have asserted");
throw new Error("Should have asserted");
} catch (AssertionError e) {
}
}
@ -105,7 +105,7 @@ public class TestDrainBarrier {
barrier.stopAndDrainOpsOnce();
try {
barrier.stopAndDrainOpsOnce();
fail("Should have asserted");
throw new Error("Should have asserted");
} catch (AssertionError e) {
}
}