
com.lambdaworks.redis.dynamic.output.OutputRegistryCommandOutputFactoryResolver Maven / Gradle / Ivy
Show all versions of lettuce Show documentation
/*
* Copyright 2011-2016 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 com.lambdaworks.redis.dynamic.output;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.lambdaworks.redis.dynamic.support.ClassTypeInformation;
import com.lambdaworks.redis.internal.LettuceAssert;
import com.lambdaworks.redis.output.CommandOutput;
/**
* {@link CommandOutputFactoryResolver} using {@link OutputRegistry} to resolve a {@link CommandOutputFactory}.
*
* Types registered in {@link OutputRegistry} are inspected for the types they produce and matched with the declared repository
* method. If resolution yields multiple {@link CommandOutput}s, the first matched output is used.
*
* @author Mark Paluch
* @since 5.0
* @see OutputRegistry
*/
public class OutputRegistryCommandOutputFactoryResolver extends CommandOutputResolverSupport
implements CommandOutputFactoryResolver {
private static final ClassTypeInformation COMMAND_OUTPUT = ClassTypeInformation.from(CommandOutput.class);
private final OutputRegistry outputRegistry;
/**
* Create a new {@link OutputRegistryCommandOutputFactoryResolver} given {@link OutputRegistry}.
*
* @param outputRegistry must not be {@literal null}.
*/
public OutputRegistryCommandOutputFactoryResolver(OutputRegistry outputRegistry) {
LettuceAssert.notNull(outputRegistry, "OutputRegistry must not be null");
this.outputRegistry = outputRegistry;
}
@Override
public CommandOutputFactory resolveCommandOutput(OutputSelector outputSelector) {
Map registry = outputRegistry.getRegistry();
Set outputTypes = registry.keySet().stream().filter((outputType) -> !outputType.isStreaming())
.collect(Collectors.toSet());
List candidates = getCandidates(outputTypes, outputSelector);
if (candidates.isEmpty()) {
return null;
}
return registry.get(candidates.get(0));
}
@Override
public CommandOutputFactory resolveStreamingCommandOutput(OutputSelector outputSelector) {
Map registry = outputRegistry.getRegistry();
Set outputTypes = registry.keySet().stream().filter(OutputType::isStreaming).collect(Collectors.toSet());
List candidates = getCandidates(outputTypes, outputSelector);
if (candidates.isEmpty()) {
return null;
}
return registry.get(candidates.get(0));
}
private List getCandidates(Collection outputTypes, OutputSelector outputSelector) {
return outputTypes.stream().filter(outputType -> {
if (COMMAND_OUTPUT.getType().isAssignableFrom(outputSelector.getTypeInformation().getType())) {
if (outputSelector.getTypeInformation().getType().isAssignableFrom(outputType.getCommandOutputClass())) {
return true;
}
}
return isAssignableFrom(outputSelector, outputType);
}).collect(Collectors.toList());
}
}