Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2013-2024 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
*
* https://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.convert;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.ClusterSlotHashUtil;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisClusterNode.Flag;
import org.springframework.data.redis.connection.RedisClusterNode.LinkState;
import org.springframework.data.redis.connection.RedisClusterNode.RedisClusterNodeBuilder;
import org.springframework.data.redis.connection.RedisClusterNode.SlotRange;
import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisNode.NodeType;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.NumberUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Common type converters.
*
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
* @author daihuabin
* @author John Blum
* @author Sorokin Evgeniy
*/
public abstract class Converters {
private static final Log LOGGER = LogFactory.getLog(Converters.class);
private static final byte[] ONE = new byte[] { '1' };
private static final byte[] ZERO = new byte[] { '0' };
private static final String CLUSTER_NODES_LINE_SEPARATOR = "\n";
/**
* Returns a {@link Converter} that always returns its input argument.
*
* @param the type of the input and output objects to the function
* @return a function that always returns its input argument
* @since 2.5
*/
public static Converter identityConverter() {
return t -> t;
}
public static Boolean stringToBoolean(String source) {
return ObjectUtils.nullSafeEquals("OK", source);
}
public static Converter stringToBooleanConverter() {
return Converters::stringToBoolean;
}
public static Converter stringToProps() {
return Converters::toProperties;
}
public static Converter longToBoolean() {
return Converters::toBoolean;
}
public static Converter stringToDataType() {
return Converters::toDataType;
}
public static Properties toProperties(String source) {
Properties info = new Properties();
try (StringReader stringReader = new StringReader(source)) {
info.load(stringReader);
} catch (Exception ex) {
throw new RedisSystemException("Cannot read Redis info", ex);
}
return info;
}
public static Properties toProperties(Map source) {
Properties target = new Properties();
target.putAll(source);
return target;
}
public static Boolean toBoolean(@Nullable Long source) {
return source != null && source == 1L;
}
public static DataType toDataType(String source) {
return DataType.fromCode(source);
}
public static byte[] toBit(Boolean source) {
return (source ? ONE : ZERO);
}
/**
* Converts the result of a single line of {@code CLUSTER NODES} into a {@link RedisClusterNode}.
*
* @param clusterNodesLine
* @return
* @since 1.7
*/
protected static RedisClusterNode toClusterNode(String clusterNodesLine) {
return ClusterNodesConverter.INSTANCE.convert(clusterNodesLine);
}
/**
* Converts lines from the result of {@code CLUSTER NODES} into {@link RedisClusterNode}s.
*
* @param lines
* @return
* @since 1.7
*/
public static Set toSetOfRedisClusterNodes(Collection lines) {
if (CollectionUtils.isEmpty(lines)) {
return Collections.emptySet();
}
Set nodes = new LinkedHashSet<>(lines.size());
for (String line : lines) {
nodes.add(toClusterNode(line));
}
return nodes;
}
/**
* Converts the result of {@code CLUSTER NODES} into {@link RedisClusterNode}s.
*
* @param clusterNodes
* @return
* @since 1.7
*/
public static Set toSetOfRedisClusterNodes(String clusterNodes) {
return StringUtils.hasText(clusterNodes)
? toSetOfRedisClusterNodes(Arrays.asList(clusterNodes.split(CLUSTER_NODES_LINE_SEPARATOR)))
: Collections.emptySet();
}
public static List