org.springframework.data.redis.connection.jedis.JedisResult Maven / Gradle / Ivy
/*
* Copyright 2017-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.jedis;
import redis.clients.jedis.Response;
import java.util.function.Supplier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.FutureResult;
import org.springframework.lang.Nullable;
/**
* Jedis specific {@link FutureResult} implementation.
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param The data type of the object that holds the future result (usually of type Future).
* @param The data type of the result type.
* @since 2.1
*/
class JedisResult extends FutureResult> {
private final boolean convertPipelineAndTxResults;
JedisResult(Response resultHolder) {
this(resultHolder, false, null);
}
JedisResult(Response resultHolder, boolean convertPipelineAndTxResults, @Nullable Converter converter) {
this(resultHolder, () -> null, convertPipelineAndTxResults, converter);
}
JedisResult(Response resultHolder, Supplier defaultReturnValue, boolean convertPipelineAndTxResults,
@Nullable Converter converter) {
super(resultHolder, converter, defaultReturnValue);
this.convertPipelineAndTxResults = convertPipelineAndTxResults;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.FutureResult#get()
* @return
*/
@Nullable
@Override
@SuppressWarnings("unchecked")
public T get() {
return (T) getResultHolder().get();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.FutureResult#conversionRequired()
*/
public boolean conversionRequired() {
return convertPipelineAndTxResults;
}
/**
* Jedis specific {@link FutureResult} implementation of a throw away status result.
*/
static class JedisStatusResult extends JedisResult {
@SuppressWarnings("unchecked")
JedisStatusResult(Response resultHolder, Converter converter) {
super(resultHolder, false, converter);
setStatus(true);
}
}
/**
* Builder for constructing {@link JedisResult}.
*
* @param
* @param
* @since 2.1
*/
static class JedisResultBuilder {
private final Response response;
private Converter converter;
private boolean convertPipelineAndTxResults = false;
private Supplier nullValueDefault = () -> null;
@SuppressWarnings("unchecked")
JedisResultBuilder(Response response) {
this.response = response;
this.converter = (source) -> (R) source;
}
/**
* Create a new {@link JedisResultBuilder} given {@link Response}.
*
* @param response must not be {@literal null}.
* @param native response type.
* @param resulting response type.
* @return the new {@link JedisResultBuilder}.
*/
static JedisResultBuilder forResponse(Response response) {
return new JedisResultBuilder<>(response);
}
/**
* Configure a {@link Converter} to convert between {@code T} and {@code R} types.
*
* @param converter must not be {@literal null}.
* @return {@code this} builder.
*/
JedisResultBuilder mappedWith(Converter converter) {
this.converter = converter;
return this;
}
/**
* Configure a {@link Supplier} to map {@literal null} responses to a different value.
*
* @param supplier must not be {@literal null}.
* @return {@code this} builder.
*/
JedisResultBuilder mapNullTo(Supplier supplier) {
this.nullValueDefault = supplier;
return this;
}
JedisResultBuilder convertPipelineAndTxResults(boolean flag) {
convertPipelineAndTxResults = flag;
return this;
}
/**
* @return a new {@link JedisResult} wrapper with configuration applied from this builder.
*/
JedisResult build() {
return new JedisResult<>(response, nullValueDefault, convertPipelineAndTxResults, converter);
}
/**
* @return a new {@link JedisStatusResult} wrapper for status results with configuration applied from this builder.
*/
JedisStatusResult buildStatusResult() {
return new JedisStatusResult<>(response, converter);
}
}
}