All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.redis.connection.lettuce.LettuceResult Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/*
 * 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.lettuce;

import io.lettuce.core.protocol.RedisCommand;

import java.util.concurrent.Future;
import java.util.function.Supplier;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.FutureResult;
import org.springframework.lang.Nullable;

/**
 * Lettuce specific {@link FutureResult} implementation. 
* * @author Costin Leau * @author Jennifer Hickey * @author Christoph Strobl * @author Mark Paluch * @since 2.1 */ @SuppressWarnings("rawtypes") class LettuceResult extends FutureResult> { private final boolean convertPipelineAndTxResults; @SuppressWarnings("unchecked") LettuceResult(Future resultHolder) { this(resultHolder, false, (Converter) val -> val); } LettuceResult(Future resultHolder, boolean convertPipelineAndTxResults, @Nullable Converter converter) { this(resultHolder, () -> null, convertPipelineAndTxResults, converter); } @SuppressWarnings("unchecked") LettuceResult(Future resultHolder, Supplier defaultReturnValue, boolean convertPipelineAndTxResults, @Nullable Converter converter) { super((RedisCommand) resultHolder, converter, defaultReturnValue); this.convertPipelineAndTxResults = convertPipelineAndTxResults; } /* * (non-Javadoc) * @see org.springframework.data.redis.connection.FutureResult#get() */ @Nullable @Override @SuppressWarnings("unchecked") public T get() { return (T) getResultHolder().getOutput().get(); } /* * (non-Javadoc) * @see org.springframework.data.redis.connection.FutureResult#conversionRequired() */ @Override public boolean conversionRequired() { return convertPipelineAndTxResults; } /** * Lettuce specific {@link FutureResult} implementation of a throw away status result. */ static class LettuceStatusResult extends LettuceResult { @SuppressWarnings("unchecked") LettuceStatusResult(Future resultHolder) { super(resultHolder); setStatus(true); } } /** * Builder for constructing {@link LettuceResult}. * * @param * @param * @since 2.1 */ static class LettuceResultBuilder { private final Future response; private Converter converter; private boolean convertPipelineAndTxResults = false; private Supplier nullValueDefault = () -> null; @SuppressWarnings("unchecked") LettuceResultBuilder(Future response) { this.response = response; this.converter = (source) -> (R) source; } /** * Create a new {@link LettuceResultBuilder} given {@link Future}. * * @param response must not be {@literal null}. * @param native response type. * @param resulting response type. * @return the new {@link LettuceResultBuilder}. */ static LettuceResultBuilder forResponse(Future response) { return new LettuceResultBuilder<>(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. */ LettuceResultBuilder 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. */ LettuceResultBuilder defaultNullTo(Supplier supplier) { this.nullValueDefault = supplier; return this; } LettuceResultBuilder convertPipelineAndTxResults(boolean flag) { convertPipelineAndTxResults = flag; return this; } /** * @return a new {@link LettuceResult} wrapper with configuration applied from this builder. */ LettuceResult build() { return new LettuceResult<>(response, nullValueDefault, convertPipelineAndTxResults, converter); } /** * @return a new {@link LettuceResult} wrapper for status results with configuration applied from this builder. */ LettuceResult buildStatusResult() { return new LettuceStatusResult<>(response); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy