Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to RetryListener class #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ repositories {
}

dependencies {
compile 'com.google.guava:guava:[10.+,)'
compile 'com.google.guava:guava:[10.0,19.0]'
compile 'com.google.code.findbugs:jsr305:2.0.2'

// junit testing
Expand Down Expand Up @@ -200,4 +200,4 @@ idea {
languageLevel = project_jdk
vcs = 'Git'
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/github/rholder/retry/RetryListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
* code through a {@link Retryer} instance.
*/
@Beta
public interface RetryListener {
public interface RetryListener<V> {

/**
* This method with fire no matter what the result is and before the
* rejection predicate and stop strategies are applied.
*
* @param attempt the current {@link Attempt}
* @param <V> the type returned by the retryer callable
*/
<V> void onRetry(Attempt<V> attempt);
void onRetry(Attempt<V> attempt);
}
8 changes: 4 additions & 4 deletions src/main/java/com/github/rholder/retry/Retryer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class Retryer<V> {
private final BlockStrategy blockStrategy;
private final AttemptTimeLimiter<V> attemptTimeLimiter;
private final Predicate<Attempt<V>> rejectionPredicate;
private final Collection<RetryListener> listeners;
private final Collection<RetryListener<V>> listeners;

/**
* Constructor
Expand Down Expand Up @@ -100,7 +100,7 @@ public Retryer(@Nonnull AttemptTimeLimiter<V> attemptTimeLimiter,
@Nonnull WaitStrategy waitStrategy,
@Nonnull BlockStrategy blockStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate) {
this(attemptTimeLimiter, stopStrategy, waitStrategy, blockStrategy, rejectionPredicate, new ArrayList<RetryListener>());
this(attemptTimeLimiter, stopStrategy, waitStrategy, blockStrategy, rejectionPredicate, new ArrayList<RetryListener<V>>());
}

/**
Expand All @@ -121,7 +121,7 @@ public Retryer(@Nonnull AttemptTimeLimiter<V> attemptTimeLimiter,
@Nonnull WaitStrategy waitStrategy,
@Nonnull BlockStrategy blockStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate,
@Nonnull Collection<RetryListener> listeners) {
@Nonnull Collection<RetryListener<V>> listeners) {
Preconditions.checkNotNull(attemptTimeLimiter, "timeLimiter may not be null");
Preconditions.checkNotNull(stopStrategy, "stopStrategy may not be null");
Preconditions.checkNotNull(waitStrategy, "waitStrategy may not be null");
Expand Down Expand Up @@ -163,7 +163,7 @@ public V call(Callable<V> callable) throws ExecutionException, RetryException {
attempt = new ExceptionAttempt<V>(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
}

for (RetryListener listener : listeners) {
for (RetryListener<V> listener : listeners) {
listener.onRetry(attempt);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/rholder/retry/RetryerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RetryerBuilder<V> {
private WaitStrategy waitStrategy;
private BlockStrategy blockStrategy;
private Predicate<Attempt<V>> rejectionPredicate = Predicates.alwaysFalse();
private List<RetryListener> listeners = new ArrayList<RetryListener>();
private List<RetryListener<V>> listeners = new ArrayList<RetryListener<V>>();

private RetryerBuilder() {
}
Expand Down
27 changes: 11 additions & 16 deletions src/test/java/com/github/rholder/retry/RetryerBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.github.rholder.retry;

import static org.junit.Assert.*;

import com.github.rholder.retry.Retryer.RetryerCallable;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,12 +30,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;

public class RetryerBuilderTest {

Expand Down Expand Up @@ -463,9 +458,9 @@ public void testWhetherBuilderFailsForNullWaitStrategyWithCompositeStrategies()
public void testRetryListener_SuccessfulAttempt() throws Exception {
final Map<Long, Attempt> attempts = new HashMap<Long, Attempt>();

RetryListener listener = new RetryListener() {
RetryListener<Long> listener = new RetryListener<Long>() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
public void onRetry(Attempt<Long> attempt) {
attempts.put(attempt.getAttemptNumber(), attempt);
}
};
Expand All @@ -492,9 +487,9 @@ public <V> void onRetry(Attempt<V> attempt) {
public void testRetryListener_WithException() throws Exception {
final Map<Long, Attempt> attempts = new HashMap<Long, Attempt>();

RetryListener listener = new RetryListener() {
RetryListener listener = new RetryListener<Long>() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
public void onRetry(Attempt<Long> attempt) {
attempts.put(attempt.getAttemptNumber(), attempt);
}
};
Expand Down Expand Up @@ -531,15 +526,15 @@ public Boolean call() throws Exception {
final AtomicBoolean listenerTwo = new AtomicBoolean(false);

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.withRetryListener(new RetryListener() {
.withRetryListener(new RetryListener<Boolean>() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
public void onRetry(Attempt<Boolean> attempt) {
listenerOne.set(true);
}
})
.withRetryListener(new RetryListener() {
.withRetryListener(new RetryListener<Boolean>() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
public void onRetry(Attempt<Boolean> attempt) {
listenerTwo.set(true);
}
})
Expand Down