com.jn.agileway.redis.collection.RedisSortedSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agileway-redis-springdata2 Show documentation
Show all versions of agileway-redis-springdata2 Show documentation
Provides a large number of convenient redis tools:
distributed locks,
distributed count,
distributed cache,
distributed id generator,
jdk collection implements,
the enhanced RedisTemplate based on a specified key prefix and the agileway-codec module
package com.jn.agileway.redis.collection;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.collection.Pipeline;
import com.jn.langx.util.function.Function;
import com.jn.langx.util.function.Predicate;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.*;
public class RedisSortedSet implements SortedSet> {
private BoundZSetOperations ops;
private String key;
public RedisSortedSet(RedisOperations redisTemplate, String key) {
this.ops = redisTemplate.boundZSetOps(key);
this.key = key;
}
private final Comparator super ZSetOperations.TypedTuple> comparator = new Comparator>() {
@Override
public int compare(ZSetOperations.TypedTuple o1, ZSetOperations.TypedTuple o2) {
double delta = o1.getScore() - o2.getScore();
if (delta == 0d) {
return 0;
}
if (delta > 0) {
return 1;
}
return -1;
}
};
@Override
public Comparator super ZSetOperations.TypedTuple> comparator() {
return comparator;
}
@Override
public SortedSet> subSet(ZSetOperations.TypedTuple fromElement, ZSetOperations.TypedTuple toElement) {
TreeSet set = new TreeSet(comparator);
set.addAll(ops.range(ops.rank(fromElement.getValue()), ops.rank(toElement.getValue())));
return set;
}
@Override
public SortedSet> headSet(ZSetOperations.TypedTuple toElement) {
ZSetOperations.TypedTuple first = first();
if (first == null) {
return Collects.emptyTreeSet();
}
return subSet(first, toElement);
}
@Override
public SortedSet> tailSet(ZSetOperations.TypedTuple fromElement) {
ZSetOperations.TypedTuple last = last();
if (last == null) {
return Collects.emptyTreeSet();
}
return subSet(fromElement, last);
}
@Override
public ZSetOperations.TypedTuple first() {
Set> set = ops.rangeWithScores(0, 0);
if (Emptys.isNotEmpty(set)) {
return (ZSetOperations.TypedTuple) Collects.toArray(set)[0];
}
return null;
}
@Override
public ZSetOperations.TypedTuple last() {
Set> set = ops.rangeWithScores(-1, -1);
if (Emptys.isNotEmpty(set)) {
return (ZSetOperations.TypedTuple) Collects.toArray(set)[0];
}
return null;
}
@Override
public int size() {
return (int)longSize();
}
public long longSize() {
return ops.size();
}
@Override
public boolean isEmpty() {
return longSize() == 0L;
}
@Override
public boolean contains(Object o) {
return ops.rank(((ZSetOperations.TypedTuple) o).getValue()) != null;
}
@Override
public Iterator> iterator() {
return null;
}
@Override
public Object[] toArray() {
return getAll().toArray();
}
@Override
public T[] toArray(T[] a) {
Object[] arr = toArray();
if (a.length >= arr.length) {
System.arraycopy(arr, 0, a, 0, arr.length);
return a;
} else {
return (T[]) arr;
}
}
@Override
public boolean add(ZSetOperations.TypedTuple tuple) {
return ops.add(tuple.getValue(), tuple.getScore());
}
@Override
public boolean remove(Object o) {
ZSetOperations.TypedTuple tuple = (ZSetOperations.TypedTuple) o;
ops.remove(tuple.getValue());
return true;
}
@Override
public boolean containsAll(Collection c) {
return Collects.allMatch(c, new Predicate>() {
@Override
public boolean test(ZSetOperations.TypedTuple tuple) {
return contains(tuple);
}
});
}
@Override
public boolean addAll(Collection extends ZSetOperations.TypedTuple> c) {
ops.add(Collects.asSet(c));
return true;
}
@Override
public boolean retainAll(Collection c) {
final List values = Pipeline.of(c).map(new Function, E>() {
@Override
public E apply(ZSetOperations.TypedTuple tuple) {
return tuple.getValue();
}
}).asList();
if (Emptys.isNotEmpty(values)) {
ops.remove(Pipeline.of(getAll()).filter(new Predicate() {
@Override
public boolean test(ZSetOperations.TypedTuple tuple) {
return !values.contains(tuple.getValue());
}
}).toArray());
}
return true;
}
@Override
public boolean removeAll(Collection c) {
Object[] values = Pipeline.of(c).map(new Function, E>() {
@Override
public E apply(ZSetOperations.TypedTuple tuple) {
return tuple.getValue();
}
}).toArray();
ops.remove(values);
return true;
}
private Set getAll() {
return ops.rangeWithScores(0, -1);
}
@Override
public void clear() {
ops.removeRange(0, -1);
}
}