org.springframework.data.redis.connection.lettuce.RangeConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-redis Show documentation
Show all versions of spring-data-redis Show documentation
Spring Data module for Redis
/*
* Copyright 2018-2023 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.lettuce;
import io.lettuce.core.Range;
import io.lettuce.core.Range.Boundary;
import io.lettuce.core.codec.StringCodec;
import java.util.function.Function;
import org.springframework.data.domain.Range.Bound;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* @author Christoph Strobl
* @author Mark Paluch
*/
class RangeConverter {
static Range toRange(org.springframework.data.domain.Range range) {
return toRange(range, StringCodec.UTF8::encodeValue);
}
/**
* @param range the source {@link org.springframework.data.domain.Range} to convert.
* @param lowerDefault the lower default to use if {@link org.springframework.data.domain.Range#getLowerBound()} is
* not {@link Bound#isBounded() bounded}.
* @param upperDefault the upper default to use if {@link org.springframework.data.domain.Range#getUpperBound()} is
* not {@link Bound#isBounded() bounded}.
* @param
* @return new instance of {@link Range}.
* @since 2.3
*/
static Range toRangeWithDefault(org.springframework.data.domain.Range range, @Nullable T lowerDefault,
@Nullable T upperDefault) {
return toRangeWithDefault(range, lowerDefault, upperDefault, StringCodec.UTF8::encodeValue);
}
static Range toRange(org.springframework.data.domain.Range range,
Function stringEncoder) {
return toRangeWithDefault(range, null, null, stringEncoder);
}
/**
* @param range the source {@link org.springframework.data.domain.Range} to convert.
* @param lowerDefault the lower default to use if {@link org.springframework.data.domain.Range#getLowerBound()} is
* not {@link Bound#isBounded() bounded}.
* @param upperDefault the upper default to use if {@link org.springframework.data.domain.Range#getUpperBound()} is
* not {@link Bound#isBounded() bounded}.
* @param stringEncoder the encoder to use.
* @param
* @return new instance of {@link Range}.
* @since 2.3
*/
static Range toRangeWithDefault(org.springframework.data.domain.Range range, @Nullable T lowerDefault,
@Nullable T upperDefault, Function stringEncoder) {
return Range.from(lowerBoundArgOf(range, lowerDefault, stringEncoder),
upperBoundArgOf(range, upperDefault, stringEncoder));
}
@SuppressWarnings("unchecked")
private static Boundary lowerBoundArgOf(org.springframework.data.domain.Range range,
@Nullable T lowerDefault, Function stringEncoder) {
return (Boundary) convertBound(range.getLowerBound(), false, lowerDefault, stringEncoder);
}
@SuppressWarnings("unchecked")
private static Boundary upperBoundArgOf(org.springframework.data.domain.Range range,
@Nullable T upperDefault, Function stringEncoder) {
return (Boundary) convertBound(range.getUpperBound(), false, upperDefault, stringEncoder);
}
static Boundary convertBound(org.springframework.data.domain.Range.Bound source, boolean convertNumberToBytes,
Object defaultValue, Function stringEncoder) {
if (!source.isBounded()) {
return Boundary.unbounded();
}
Boolean inclusive = source.isInclusive();
Object value = source.getValue().orElse(defaultValue);
if (value instanceof Number) {
if (convertNumberToBytes) {
value = value.toString();
} else {
return inclusive ? Boundary.including((Number) value) : Boundary.excluding((Number) value);
}
}
if (value instanceof String) {
if (!StringUtils.hasText((String) value) || ObjectUtils.nullSafeEquals(value, "+")
|| ObjectUtils.nullSafeEquals(value, "-")) {
return Boundary.unbounded();
}
Object encoded = stringEncoder.apply((String) value);
return inclusive ? Boundary.including(encoded) : Boundary.excluding(encoded);
}
if (value == null) {
return Boundary.unbounded();
}
return inclusive ? Boundary.including(value) : Boundary.excluding(value);
}
}