Skip to content

Commit

Permalink
Refactor and fix compiler warnings in FutureResult and JedisResult.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxblum committed Sep 20, 2023
1 parent 3242c46 commit 384b1df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@
import org.springframework.lang.Nullable;

/**
* The result of an asynchronous operation
* Result of an asynchronous operation
*
* @param <T> The data type of the object that holds the future result (usually type of the
* {@link java.util.concurrent.Future} or response wrapper).
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param <T> The data type of the object that holds the future result (usually type of the
* {@link java.util.concurrent.Future} or response wrapper).
* @author John Blum
*/
public abstract class FutureResult<T> {

private T resultHolder;
private boolean status = false;

private final T resultHolder;

private final Supplier<?> defaultConversionResult;

private boolean status = false;

@SuppressWarnings("rawtypes") //
@SuppressWarnings("rawtypes")
protected Converter converter;

/**
Expand Down Expand Up @@ -71,23 +74,14 @@ public FutureResult(T resultHolder, @Nullable Converter converter) {
* @param defaultConversionResult must not be {@literal null}.
* @since 2.1
*/
@SuppressWarnings("rawtypes")
public FutureResult(T resultHolder, @Nullable Converter converter, Supplier<?> defaultConversionResult) {

this.resultHolder = resultHolder;
this.converter = converter != null ? converter : val -> val;
this.defaultConversionResult = defaultConversionResult;
}

/**
* Get the object holding the actual result.
*
* @return never {@literal null}.
* @since 1.1
*/
public T getResultHolder() {
return resultHolder;
}

/**
* Converts the given result if a converter is specified, else returns the result
*
Expand All @@ -98,11 +92,9 @@ public T getResultHolder() {
@Nullable
public Object convert(@Nullable Object result) {

if (result == null) {
return computeDefaultResult(null);
}
Object resolvedResult = result != null ? getConverter().convert(result) : null;

return computeDefaultResult(converter.convert(result));
return computeDefaultResult(resolvedResult);
}

@Nullable
Expand All @@ -112,7 +104,17 @@ private Object computeDefaultResult(@Nullable Object source) {

@SuppressWarnings("rawtypes")
public Converter getConverter() {
return converter;
return this.converter;
}

/**
* Get the object holding the actual result.
*
* @return never {@literal null}.
* @since 1.1
*/
public T getResultHolder() {
return this.resultHolder;
}

/**
Expand All @@ -121,7 +123,7 @@ public Converter getConverter() {
* @return true if this is a status result (i.e. OK)
*/
public boolean isStatus() {
return status;
return this.status;
}

/**
Expand All @@ -144,4 +146,5 @@ public void setStatus(boolean status) {
* @since 2.1
*/
public abstract boolean conversionRequired();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@
import org.springframework.lang.Nullable;

/**
* Jedis specific {@link FutureResult} implementation. <br />
* {@link FutureResult} implementation for Jedis.
*
* @param <T> The data type of the object that holds the future result (usually of type Future).
* @param <R> The data type of the result type.
* @author Costin Leau
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param <T> The data type of the object that holds the future result (usually of type Future).
* @param <R> The data type of the result type.
* @author John Blum
* @since 2.1
*/
class JedisResult<T, R> extends FutureResult<Response<?>> {

private final boolean convertPipelineAndTxResults;

JedisResult(Response<T> resultHolder) {
this(resultHolder, false, null);
}

JedisResult(Response<T> resultHolder, boolean convertPipelineAndTxResults, @Nullable Converter<T, ?> converter) {
this(resultHolder, () -> null, convertPipelineAndTxResults, converter);
}
Expand All @@ -50,6 +47,7 @@ class JedisResult<T, R> extends FutureResult<Response<?>> {
@Nullable Converter<T, ?> converter) {

super(resultHolder, converter, defaultReturnValue);

this.convertPipelineAndTxResults = convertPipelineAndTxResults;
}

Expand All @@ -61,18 +59,18 @@ public T get() {
}

public boolean conversionRequired() {
return convertPipelineAndTxResults;
return this.convertPipelineAndTxResults;
}

/**
* Jedis specific {@link FutureResult} implementation of a throw away status result.
*/
static class JedisStatusResult<T, R> extends JedisResult<T, R> {

@SuppressWarnings("unchecked")
JedisStatusResult(Response<T> resultHolder, Converter<T, R> converter) {

super(resultHolder, false, converter);

setStatus(true);
}
}
Expand All @@ -86,9 +84,12 @@ static class JedisStatusResult<T, R> extends JedisResult<T, R> {
*/
static class JedisResultBuilder<T, R> {

private final Response<T> response;
private Converter<T, R> converter;
private boolean convertPipelineAndTxResults = false;

private Converter<T, R> converter;

private final Response<T> response;

private Supplier<R> nullValueDefault = () -> null;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -119,6 +120,7 @@ static <T, R> JedisResultBuilder<T, R> forResponse(Response<T> response) {
JedisResultBuilder<T, R> mappedWith(Converter<T, R> converter) {

this.converter = converter;

return this;
}

Expand All @@ -131,12 +133,14 @@ JedisResultBuilder<T, R> mappedWith(Converter<T, R> converter) {
JedisResultBuilder<T, R> mapNullTo(Supplier<R> supplier) {

this.nullValueDefault = supplier;

return this;
}

JedisResultBuilder<T, R> convertPipelineAndTxResults(boolean flag) {

convertPipelineAndTxResults = flag;
this.convertPipelineAndTxResults = flag;

return this;
}

Expand Down

0 comments on commit 384b1df

Please sign in to comment.