Polishing

This commit is contained in:
Juergen Hoeller
2014-09-04 00:55:38 +02:00
parent 8543a5548e
commit 97bd0ccfec
10 changed files with 319 additions and 445 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,18 +30,16 @@ import java.util.concurrent.ExecutionException;
* @author Arjen Poutsma
* @since 4.0
*/
public abstract class ListenableFutureAdapter<T, S> extends FutureAdapter<T, S>
implements ListenableFuture<T> {
public abstract class ListenableFutureAdapter<T, S> extends FutureAdapter<T, S> implements ListenableFuture<T> {
/**
* Constructs a new {@code ListenableFutureAdapter} with the given adaptee.
* Construct a new {@code ListenableFutureAdapter} with the given adaptee.
* @param adaptee the future to adaptee to
*/
protected ListenableFutureAdapter(ListenableFuture<S> adaptee) {
super(adaptee);
}
@Override
public void addCallback(final ListenableFutureCallback<? super T> callback) {
ListenableFuture<S> listenableAdaptee = (ListenableFuture<S>) getAdaptee();
@@ -59,11 +57,11 @@ public abstract class ListenableFutureAdapter<T, S> extends FutureAdapter<T, S>
onFailure(t);
}
}
@Override
public void onFailure(Throwable t) {
callback.onFailure(t);
public void onFailure(Throwable ex) {
callback.onFailure(ex);
}
});
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,8 +33,8 @@ public interface ListenableFutureCallback<T> {
/**
* Called when the {@link ListenableFuture} fails to complete.
* @param t the exception that triggered the failure
* @param ex the exception that triggered the failure
*/
void onFailure(Throwable t);
void onFailure(Throwable ex);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,54 +48,52 @@ public class ListenableFutureCallbackRegistry<T> {
@SuppressWarnings("unchecked")
public void addCallback(ListenableFutureCallback<? super T> callback) {
Assert.notNull(callback, "'callback' must not be null");
synchronized (mutex) {
switch (state) {
synchronized (this.mutex) {
switch (this.state) {
case NEW:
callbacks.add(callback);
this.callbacks.add(callback);
break;
case SUCCESS:
callback.onSuccess((T)result);
callback.onSuccess((T) this.result);
break;
case FAILURE:
callback.onFailure((Throwable) result);
callback.onFailure((Throwable) this.result);
break;
}
}
}
/**
* Triggers a {@link ListenableFutureCallback#onSuccess(Object)} call on all added
* callbacks with the given result
* Triggers a {@link ListenableFutureCallback#onSuccess(Object)} call on all
* added callbacks with the given result.
* @param result the result to trigger the callbacks with
*/
public void success(T result) {
synchronized (mutex) {
state = State.SUCCESS;
synchronized (this.mutex) {
this.state = State.SUCCESS;
this.result = result;
while (!callbacks.isEmpty()) {
callbacks.poll().onSuccess(result);
while (!this.callbacks.isEmpty()) {
this.callbacks.poll().onSuccess(result);
}
}
}
/**
* Triggers a {@link ListenableFutureCallback#onFailure(Throwable)} call on all added
* callbacks with the given {@code Throwable}.
* @param t the exception to trigger the callbacks with
* Triggers a {@link ListenableFutureCallback#onFailure(Throwable)} call on all
* added callbacks with the given {@code Throwable}.
* @param ex the exception to trigger the callbacks with
*/
public void failure(Throwable t) {
synchronized (mutex) {
state = State.FAILURE;
this.result = t;
while (!callbacks.isEmpty()) {
callbacks.poll().onFailure(t);
public void failure(Throwable ex) {
synchronized (this.mutex) {
this.state = State.FAILURE;
this.result = ex;
while (!this.callbacks.isEmpty()) {
this.callbacks.poll().onFailure(ex);
}
}
}
private enum State {NEW, SUCCESS, FAILURE}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,10 +20,10 @@ import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
*/
@@ -44,10 +44,9 @@ public class ListenableFutureTaskTests {
public void onSuccess(String result) {
assertEquals(s, result);
}
@Override
public void onFailure(Throwable t) {
fail(t.getMessage());
public void onFailure(Throwable ex) {
fail(ex.getMessage());
}
});
task.run();
@@ -68,15 +67,12 @@ public class ListenableFutureTaskTests {
public void onSuccess(String result) {
fail("onSuccess not expected");
}
@Override
public void onFailure(Throwable t) {
assertEquals(s, t.getMessage());
public void onFailure(Throwable ex) {
assertEquals(s, ex.getMessage());
}
});
task.run();
}
}