Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyweston committed Sep 16, 2018
1 parent 33d89a3 commit 054264d
Show file tree
Hide file tree
Showing 30 changed files with 79 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static <E extends RuntimeException> void throwException(Exception throwab
}

static class ExceptionFactory<E extends Exception> implements Factory<E> {
private final Class wrapped;
private final Class<E> wrapped;
private final Throwable throwable;

static <E extends Exception> ExceptionFactory<E> newException(WithException<E> wrapped, Throwable throwable) {
return new ExceptionFactory<E>(wrapped.getType(), throwable);
return new ExceptionFactory<>(wrapped.getType(), throwable);
}

private ExceptionFactory(Class<E> wrapped, Throwable throwable) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/google/code/tempusfugit/WithException.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public final class WithException<E extends Exception> {
private final Class<E> type;

public static <E extends Exception> WithException<E> with(Class<E> type) {
return new WithException<E>(type);
return new WithException<>(type);
}

public static <E extends Exception> WithException<E> as(Class<E> type) {
return new WithException<E>(type);
return new WithException<>(type);
}

private WithException(Class<E> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,21 @@
public class CallableAdapter {

public static Runnable runnable(final Callable callable) {
return new Runnable() {
public void run() {
try {
callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
return () -> {
try {
callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}

public static Condition condition(final Callable<Boolean> callable ) {
return new Condition() {
@Override
public boolean isSatisfied() {
try {
return callable.call();
} catch (Exception e ) {
return false;
}
return () -> {
try {
return callable.call();
} catch (Exception e ) {
return false;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Deadlocks extends OutputStream {
private final ByteArrayOutputStream stream = new ByteArrayOutputStream();

@Override
public void write(int b) throws IOException {
public void write(int b) {
stream.write(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DefaultTimeoutableCompletionService(CompletionService completionService,
}

public <T> List<T> submit(List<? extends java.util.concurrent.Callable<T>> tasks) throws ExecutionException, TimeoutException {
List<Future<T>> submitted = new ArrayList<Future<T>>();
List<Future<T>> submitted = new ArrayList<>();
try {
for (Callable task : tasks) {
submitted.add(completionService.submit(task));
Expand All @@ -63,7 +63,7 @@ public <T> List<T> submit(List<? extends java.util.concurrent.Callable<T>> tasks
}

private <T> List<T> waitFor(int tasks, Duration timeout) throws ExecutionException, TimeoutException {
List<T> completed = new ArrayList<T>();
List<T> completed = new ArrayList<>();
Interrupter interrupter = interrupt(currentThread()).using(time).after(timeout);
try {
for (int i = 0; i < tasks; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private ExecuteUsingLock(Callable<T, E> callable) {
}

public static <T, E extends Exception> ExecuteUsingLock<T, E> execute(Callable<T, E> callable) {
return new ExecuteUsingLock<T, E>(callable);
return new ExecuteUsingLock<>(callable);
}

public T using(Lock lock) throws E {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class InterruptCapturingThread extends Thread {

private final CopyOnWriteArrayList<StackTraceElement[]> interrupterStackTraces = new CopyOnWriteArrayList<StackTraceElement[]>();
private final CopyOnWriteArrayList<StackTraceElement[]> interrupterStackTraces = new CopyOnWriteArrayList<>();

public InterruptCapturingThread() {
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public void interrupt() {
}

public List<StackTraceElement[]> getInterrupters() {
return new ArrayList<StackTraceElement[]>(interrupterStackTraces);
return new ArrayList<>(interrupterStackTraces);
}

public void printStackTraceOfInterruptingThreads(PrintStream out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ Interrupter using(Clock time) {

public Interrupter after(final Duration duration) {
final Timeout timeout = timeout(duration, createAndStartStopWatch());
interrupterThread = new Thread(new Runnable() {
public void run() {
try {
waitUntil(timeout);
if (!interrupterThread.isInterrupted()) {
Interrupter.this.threadToInterrupt.interrupt();
}
} catch (InterruptedException e) {
currentThread().interrupt();
interrupterThread = new Thread(() -> {
try {
waitUntil(timeout);
if (!interrupterThread.isInterrupted()) {
Interrupter.this.threadToInterrupt.interrupt();
}
} catch (InterruptedException e) {
currentThread().interrupt();
}
}, "Interrupter-Thread-" + COUNTER.incrementAndGet());
interrupterThread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void evaluate() throws Throwable {
statement.evaluate();
}

private ExecutorCompletionService createCompletionService() {
return new ExecutorCompletionService(new Executor() {
private ExecutorCompletionService<Void> createCompletionService() {
return new ExecutorCompletionService<Void>(new Executor() {
private int count;
public void execute(Runnable runnable) {
new Thread(runnable, method.getName() + "-Thread-" + count++).start();
Expand Down Expand Up @@ -92,7 +92,7 @@ public StatementEvaluatingTask(Statement statement, CountDownLatch start) {
this.start = start;
}

public Void call() throws Exception {
public Void call() {
try {
start.await();
statement.evaluate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public TimeoutExceptionWithResults(String message) {

public <T> TimeoutExceptionWithResults(String message, List<T> results) {
this(message);
this.results = new ArrayList<T>(results);
this.results = new ArrayList<>(results);
}

public <T> TimeoutExceptionWithResults(List<T> results) {
super();
this.results = new ArrayList<T>(results);
this.results = new ArrayList<>(results);
}

public <T> List<T> getResults() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static <T> Condition assertion(T actual, Matcher<T> matcher) {
}

public static <T> SelfDescribingCondition assertion(Callable<T, RuntimeException> actual, Matcher<T> matcher) {
return new SelfDescribingMatcherCondition<T>(actual, matcher);
return new SelfDescribingMatcherCondition<>(actual, matcher);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SelfDescribingMatcherCondition<T> implements SelfDescribingConditio

private final Callable<T, RuntimeException> actual;
private final Matcher<T> matcher;
private final List<Description> description = new ArrayList<Description>();
private final List<Description> description = new ArrayList<>();

public static <T> SelfDescribingMatcherCondition probe(ProbeFor<T> probe, Matcher<T> matcher) {
return new SelfDescribingMatcherCondition(probe, matcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ThreadLocalLong(long initialValue) {

@Override
protected Long initialValue() {
return Long.valueOf(initialValue);
return initialValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void returnsOnSuccessfulDelegateCreate() {
never(factory2).create();
never(factory3).create();
}});
assertThat(new CompositeFactory<String>(factory1, factory2, factory3).create(), is("done"));
assertThat(new CompositeFactory<>(factory1, factory2, factory3).create(), is("done"));
}

@Test
Expand All @@ -55,7 +55,7 @@ public void skipFactoriesThatThrowExceptions() {
oneOf(factory2).create(); will(returnValue("done")); inSequence(sequence);
never(factory3).create();
}});
assertThat(new CompositeFactory<String>(factory1, factory2, factory3).create(), is("done"));
assertThat(new CompositeFactory<>(factory1, factory2, factory3).create(), is("done"));
}

@Test (expected = FactoryException.class)
Expand All @@ -66,6 +66,6 @@ public void throwsExceptionIfAllFactoriesThrowExceptions() {
oneOf(factory2).create(); will(throwException(new FactoryException())); inSequence(sequence);
oneOf(factory3).create(); will(throwException(new FactoryException())); inSequence(sequence);
}});
new CompositeFactory<String>(factory1, factory2, factory3).create();
new CompositeFactory<>(factory1, factory2, factory3).create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class StubOutputStream extends OutputStream {
private final ByteArrayOutputStream stream = new ByteArrayOutputStream();

@Override
public void write(int b) throws IOException {
public void write(int b) {
stream.write(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class AbstractConcurrentTestRunnerTest {

protected final static int CONCURRENT_COUNT = 3;

protected static final Set<String> THREADS = synchronizedSet(new HashSet<String>());
protected static final Set<String> THREADS = synchronizedSet(new HashSet<>());

@Test
public void shouldRunInParallel1() throws TimeoutException, InterruptedException {
Expand Down Expand Up @@ -76,7 +76,7 @@ private void waitToForceCachedThreadPoolToCreateNewThread() throws InterruptedEx
}

@Test(expected = AssertionFailedError.class)
public void concurrentFailuresFailInTheMainTestThread() throws InterruptedException {
public void concurrentFailuresFailInTheMainTestThread() {
fail();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void timeoutReturnsPartialResults() throws Exception {
new DefaultTimeoutableCompletionService(completionService, TIMEOUT, time).submit(asList(task1, task2));
fail("should have timed out");
} catch (final TimeoutExceptionWithResults e) {
assertThat((String) e.getResults().get(0), is(TASK1_RESULT));
assertThat(e.getResults().get(0), is(TASK1_RESULT));
}
}

Expand Down Expand Up @@ -172,11 +172,11 @@ public boolean isDone() {
throw new UnsupportedOperationException();
}

public String get() throws InterruptedException, ExecutionException {
public String get() {
return string;
}

public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
public String get(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,12 @@ public void locksAndUnlocksWhenExceptionThrown() throws Exception {
}

private Callable<Void, RuntimeException> something() {
return new Callable<Void, RuntimeException>() {
public Void call() throws RuntimeException {
return null;
}
};
return () -> null;
}

private Callable<Void, Exception> somethingThatThrowsException() {
return new Callable<Void, Exception>() {
public Void call() throws Exception {
throw new RuntimeException("go go go");
}
return () -> {
throw new RuntimeException("go go go");
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ private void verify(List<StackTraceElement[]> stackTraceElements) {
}

private static InterruptCapturingThread sleepingThread() {
return new InterruptCapturingThread(new Runnable() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// this is supposed to happen
}
return new InterruptCapturingThread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// this is supposed to happen
}
}, "thread-to-interrupt");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void interruptGetsCalledFromAnotherThread() throws TimeoutException {
}

@Test
public void interruptDoesntGetsCalledAfterFixedTime() throws TimeoutException {
public void interruptDoesntGetsCalledAfterFixedTime() {
interrupt(thread).using(time).after(millis(1));
assertNotInterruptedWithin(TIMEOUT);
}
Expand All @@ -88,7 +88,7 @@ public void interruptGetsCalledAfterFixedTime() throws TimeoutException {
}

@Test
public void interruptCanBeCancelled() throws InterruptedException {
public void interruptCanBeCancelled() {
Interrupter interrupter = interrupt(thread).using(time).after(millis(1));
interrupter.cancel();
time.setTime(millis(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* limitations under the License.
*/

/**
*
*/
package com.google.code.tempusfugit.concurrency;

import com.google.code.tempusfugit.concurrency.annotations.Concurrent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class RunConcurrentlyTest {

private static final AtomicInteger COUNTER = new AtomicInteger();

private static final Set<String> THREADS = synchronizedSet(new HashSet<String>());
private static final Set<String> THREADS = synchronizedSet(new HashSet<>());

@Test
@Concurrent (count = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ private void waitForStartup(Thread thread) throws TimeoutException, InterruptedE
}

private InterruptedIndicatingThread threadSleepsForever() {
return new InterruptedIndicatingThread(new Runnable() {
public void run() {
ThreadUtils.sleep(seconds(10));
}
}, "sleeping-thread");
return new InterruptedIndicatingThread(() -> ThreadUtils.sleep(seconds(10)), "sleeping-thread");
}

private void waitForShutdown(final Thread thread) throws TimeoutException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TimeoutExceptionWithResultsTest {

private static final String MESSAGE = "message";

private final List<String> strings = new ArrayList<String>(asList("hello", "goodbye", "bonjour"));
private final List<String> strings = new ArrayList<>(asList("hello", "goodbye", "bonjour"));

@Test
public void resultsAreInitialised() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ private void waitUntil(StatePredicate predicate, Timeout testTimeout) throws Int
}

public Invokable synchroniseAccessTo(final Invokable mockObject) {
return new Invokable() {
public Object invoke(Invocation invocation) throws Throwable {
return synchroniseInvocation(mockObject, invocation);
}
};
return invocation -> synchroniseInvocation(mockObject, invocation);
}

private Object synchroniseInvocation(Invokable mockObject, Invocation invocation) throws Throwable {
Expand Down
Loading

0 comments on commit 054264d

Please sign in to comment.