All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
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.
redis.clients.jedis.BuilderFactory Maven / Gradle / Ivy
package redis.clients.jedis;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.resps.LCSMatchResult.MatchedPosition;
import redis.clients.jedis.resps.LCSMatchResult.Position;
import redis.clients.jedis.util.DoublePrecision;
import redis.clients.jedis.util.JedisByteHashMap;
import redis.clients.jedis.util.KeyValue;
import redis.clients.jedis.util.SafeEncoder;
public final class BuilderFactory {
public static final Builder RAW_OBJECT = new Builder() {
@Override
public Object build(Object data) {
return data;
}
@Override
public String toString() {
return "Object";
}
};
public static final Builder> RAW_OBJECT_LIST = new Builder>() {
@Override
public List build(Object data) {
return (List) data;
}
@Override
public String toString() {
return "List";
}
};
public static final Builder ENCODED_OBJECT = new Builder() {
@Override
public Object build(Object data) {
return SafeEncoder.encodeObject(data);
}
@Override
public String toString() {
return "Object";
}
};
public static final Builder> ENCODED_OBJECT_LIST = new Builder>() {
@Override
public List build(Object data) {
return (List) SafeEncoder.encodeObject(data);
}
@Override
public String toString() {
return "List";
}
};
public static final Builder LONG = new Builder() {
@Override
public Long build(Object data) {
return (Long) data;
}
@Override
public String toString() {
return "Long";
}
};
public static final Builder> LONG_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) {
return null;
}
return (List) data;
}
@Override
public String toString() {
return "List";
}
};
public static final Builder DOUBLE = new Builder() {
@Override
public Double build(Object data) {
if (data == null) return null;
else if (data instanceof Double) return (Double) data;
else return DoublePrecision.parseFloatingPointNumber(STRING.build(data));
}
@Override
public String toString() {
return "Double";
}
};
public static final Builder> DOUBLE_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(DOUBLE::build).collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder BOOLEAN = new Builder() {
@Override
public Boolean build(Object data) {
if (data == null) return null;
else if (data instanceof Boolean) return (Boolean) data;
return ((Long) data) == 1L;
}
@Override
public String toString() {
return "Boolean";
}
};
public static final Builder> BOOLEAN_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(BOOLEAN::build).collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder> BOOLEAN_WITH_ERROR_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) return null;
return ((List) data).stream()
//.map((val) -> (val instanceof JedisDataException) ? val : BOOLEAN.build(val))
.map((val) -> (val instanceof JedisDataException) ? null : BOOLEAN.build(val))
.collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder BINARY = new Builder() {
@Override
public byte[] build(Object data) {
return (byte[]) data;
}
@Override
public String toString() {
return "byte[]";
}
};
public static final Builder> BINARY_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
return (List) data;
}
@Override
public String toString() {
return "List";
}
};
public static final Builder> BINARY_SET = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Set build(Object data) {
if (null == data) {
return null;
}
List l = BINARY_LIST.build(data);
return SetFromList.of(l);
}
@Override
public String toString() {
return "Set";
}
};
public static final Builder>> BINARY_PAIR_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public List> build(Object data) {
final List flatHash = (List) data;
final List> pairList = new ArrayList<>();
final Iterator iterator = flatHash.iterator();
while (iterator.hasNext()) {
pairList.add(new AbstractMap.SimpleEntry<>(iterator.next(), iterator.next()));
}
return pairList;
}
@Override
public String toString() {
return "List>";
}
};
public static final Builder>> BINARY_PAIR_LIST_FROM_PAIRS
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public List> build(Object data) {
final List list = (List) data;
final List> pairList = new ArrayList<>();
for (Object object : list) {
final List flat = (List) object;
pairList.add(new AbstractMap.SimpleEntry<>(flat.get(0), flat.get(1)));
}
return pairList;
}
@Override
public String toString() {
return "List>";
}
};
public static final Builder STRING = new Builder() {
@Override
public String build(Object data) {
return data == null ? null : SafeEncoder.encode((byte[]) data);
}
@Override
public String toString() {
return "String";
}
};
public static final Builder> STRING_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(STRING::build).collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder> STRING_SET = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Set build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(STRING::build).collect(Collectors.toSet());
}
@Override
public String toString() {
return "Set";
}
};
public static final Builder> BINARY_MAP = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Map build(Object data) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.get(0) instanceof KeyValue) {
final Map map = new JedisByteHashMap();
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
KeyValue kv = (KeyValue) iterator.next();
map.put(BINARY.build(kv.getKey()), BINARY.build(kv.getValue()));
}
return map;
} else {
final Map map = new JedisByteHashMap();
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
map.put(BINARY.build(iterator.next()), BINARY.build(iterator.next()));
}
return map;
}
}
@Override
public String toString() {
return "Map";
}
};
public static final Builder> STRING_MAP = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Map build(Object data) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.get(0) instanceof KeyValue) {
final Map map = new HashMap<>(list.size(), 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
KeyValue kv = (KeyValue) iterator.next();
map.put(STRING.build(kv.getKey()), STRING.build(kv.getValue()));
}
return map;
} else {
final Map map = new HashMap<>(list.size() / 2, 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
map.put(STRING.build(iterator.next()), STRING.build(iterator.next()));
}
return map;
}
}
@Override
public String toString() {
return "Map";
}
};
public static final Builder> ENCODED_OBJECT_MAP = new Builder>() {
@Override
public Map build(Object data) {
if (data == null) return null;
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.get(0) instanceof KeyValue) {
final Map map = new HashMap<>(list.size(), 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
KeyValue kv = (KeyValue) iterator.next();
map.put(STRING.build(kv.getKey()), ENCODED_OBJECT.build(kv.getValue()));
}
return map;
} else {
final Map map = new HashMap<>(list.size() / 2, 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
map.put(STRING.build(iterator.next()), ENCODED_OBJECT.build(iterator.next()));
}
return map;
}
}
};
public static final Builder AGGRESSIVE_ENCODED_OBJECT = new Builder() {
@Override
public Object build(Object data) {
if (data == null) return null;
if (data instanceof List) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.get(0) instanceof KeyValue) {
return ((List) data).stream()
.filter(kv -> kv != null && kv.getKey() != null && kv.getValue() != null)
.collect(Collectors.toMap(kv -> STRING.build(kv.getKey()),
kv -> this.build(kv.getValue())));
} else {
return list.stream().map(this::build).collect(Collectors.toList());
}
} else if (data instanceof byte[]) {
return STRING.build(data);
}
return data;
}
};
public static final Builder> AGGRESSIVE_ENCODED_OBJECT_MAP = new Builder>() {
@Override
public Map build(Object data) {
return (Map) AGGRESSIVE_ENCODED_OBJECT.build(data);
}
};
public static final Builder>> STRING_PAIR_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public List> build(Object data) {
final List flatHash = (List) data;
final List> pairList = new ArrayList<>(flatHash.size() / 2);
final Iterator iterator = flatHash.iterator();
while (iterator.hasNext()) {
pairList.add(KeyValue.of(STRING.build(iterator.next()), STRING.build(iterator.next())));
}
return pairList;
}
@Override
public String toString() {
return "List>";
}
};
public static final Builder>> STRING_PAIR_LIST_FROM_PAIRS
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public List> build(Object data) {
return ((List) data).stream().map(o -> (List) o)
.map(l -> KeyValue.of(STRING.build(l.get(0)), STRING.build(l.get(1))))
.collect(Collectors.toList());
}
@Override
public String toString() {
return "List>";
}
};
public static final Builder> STRING_LONG_MAP = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Map build(Object data) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.get(0) instanceof KeyValue) {
final Map map = new LinkedHashMap<>(list.size(), 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
KeyValue kv = (KeyValue) iterator.next();
map.put(STRING.build(kv.getKey()), LONG.build(kv.getValue()));
}
return map;
} else {
final Map map = new LinkedHashMap<>(list.size() / 2, 1f);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
map.put(STRING.build(iterator.next()), LONG.build(iterator.next()));
}
return map;
}
}
@Override
public String toString() {
return "Map";
}
};
public static final Builder> KEYED_ELEMENT = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue build(Object data) {
if (data == null) return null;
List l = (List) data;
return KeyValue.of(STRING.build(l.get(0)), STRING.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue";
}
};
public static final Builder> BINARY_KEYED_ELEMENT = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue build(Object data) {
if (data == null) return null;
List l = (List) data;
return KeyValue.of(BINARY.build(l.get(0)), BINARY.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue";
}
};
public static final Builder> ZRANK_WITHSCORE_PAIR = new Builder>() {
@Override
public KeyValue build(Object data) {
if (data == null) {
return null;
}
List l = (List) data;
return new KeyValue<>(LONG.build(l.get(0)), DOUBLE.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue";
}
};
public static final Builder>> KEYED_STRING_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue> build(Object data) {
if (data == null) return null;
List l = (List) data;
return new KeyValue<>(STRING.build(l.get(0)), STRING_LIST.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue>";
}
};
public static final Builder> LONG_LONG_PAIR = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue build(Object data) {
if (data == null) return null;
List dataList = (List) data;
return new KeyValue<>(LONG.build(dataList.get(0)), LONG.build(dataList.get(1)));
}
};
public static final Builder>>> KEYED_STRING_LIST_LIST
= new Builder>>>() {
@Override
public List>> build(Object data) {
List list = (List) data;
return list.stream().map(KEYED_STRING_LIST::build).collect(Collectors.toList());
}
};
public static final Builder>> KEYED_BINARY_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue> build(Object data) {
if (data == null) return null;
List l = (List) data;
return new KeyValue<>(BINARY.build(l.get(0)), BINARY_LIST.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue>";
}
};
public static final Builder TUPLE = new Builder() {
@Override
@SuppressWarnings("unchecked")
public Tuple build(Object data) {
List l = (List) data; // never null
if (l.isEmpty()) {
return null;
}
return new Tuple(l.get(0), DOUBLE.build(l.get(1)));
}
@Override
public String toString() {
return "Tuple";
}
};
public static final Builder> KEYED_TUPLE = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue build(Object data) {
List l = (List) data; // never null
if (l.isEmpty()) {
return null;
}
return KeyValue.of(STRING.build(l.get(0)), new Tuple(BINARY.build(l.get(1)), DOUBLE.build(l.get(2))));
}
@Override
public String toString() {
return "KeyValue";
}
};
public static final Builder> BINARY_KEYED_TUPLE = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue build(Object data) {
List l = (List) data; // never null
if (l.isEmpty()) {
return null;
}
return KeyValue.of(BINARY.build(l.get(0)), new Tuple(BINARY.build(l.get(1)), DOUBLE.build(l.get(2))));
}
@Override
public String toString() {
return "KeyValue";
}
};
public static final Builder> TUPLE_LIST = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) {
return null;
}
List l = (List) data;
final List result = new ArrayList<>(l.size() / 2);
Iterator iterator = l.iterator();
while (iterator.hasNext()) {
result.add(new Tuple(iterator.next(), DOUBLE.build(iterator.next())));
}
return result;
}
@Override
public String toString() {
return "List";
}
};
public static final Builder> TUPLE_LIST_RESP3 = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(TUPLE::build).collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder> TUPLE_ZSET = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Set build(Object data) {
if (null == data) {
return null;
}
List l = (List) data;
final Set result = new LinkedHashSet<>(l.size() / 2, 1);
Iterator iterator = l.iterator();
while (iterator.hasNext()) {
result.add(new Tuple(iterator.next(), DOUBLE.build(iterator.next())));
}
return result;
}
@Override
public String toString() {
return "ZSet";
}
};
public static final Builder> TUPLE_ZSET_RESP3 = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public Set build(Object data) {
if (null == data) return null;
return ((List) data).stream().map(TUPLE::build).collect(Collectors.toCollection(LinkedHashSet::new));
}
@Override
public String toString() {
return "ZSet";
}
};
private static final Builder> TUPLE_LIST_FROM_PAIRS = new Builder>() {
@Override
@SuppressWarnings("unchecked")
public List build(Object data) {
if (data == null) return null;
return ((List>) data).stream().map(TUPLE::build).collect(Collectors.toList());
}
@Override
public String toString() {
return "List";
}
};
public static final Builder>> KEYED_TUPLE_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue> build(Object data) {
if (data == null) return null;
List l = (List) data;
return new KeyValue<>(STRING.build(l.get(0)), TUPLE_LIST_FROM_PAIRS.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue>";
}
};
public static final Builder>> BINARY_KEYED_TUPLE_LIST
= new Builder>>() {
@Override
@SuppressWarnings("unchecked")
public KeyValue> build(Object data) {
if (data == null) return null;
List l = (List) data;
return new KeyValue<>(BINARY.build(l.get(0)), TUPLE_LIST_FROM_PAIRS.build(l.get(1)));
}
@Override
public String toString() {
return "KeyValue>";
}
};
public static final Builder> SCAN_RESPONSE = new Builder>() {
@Override
public ScanResult build(Object data) {
List result = (List) data;
String newcursor = new String((byte[]) result.get(0));
List rawResults = (List) result.get(1);
List results = new ArrayList<>(rawResults.size());
for (byte[] bs : rawResults) {
results.add(SafeEncoder.encode(bs));
}
return new ScanResult<>(newcursor, results);
}
};
public static final Builder>> HSCAN_RESPONSE
= new Builder>>() {
@Override
public ScanResult> build(Object data) {
List result = (List) data;
String newcursor = new String((byte[]) result.get(0));
List rawResults = (List) result.get(1);
List> results = new ArrayList<>(rawResults.size() / 2);
Iterator iterator = rawResults.iterator();
while (iterator.hasNext()) {
results.add(new AbstractMap.SimpleEntry<>(SafeEncoder.encode(iterator.next()),
SafeEncoder.encode(iterator.next())));
}
return new ScanResult<>(newcursor, results);
}
};
public static final Builder> SSCAN_RESPONSE = new Builder