All Downloads are FREE. Search and download functionalities are using the official Maven repository.

redis.clients.jedis.BuilderFactory Maven / Gradle / Ivy

The newest version!
package redis.clients.jedis;

import redis.clients.util.SafeEncoder;

import java.util.*;

public class BuilderFactory {
    public static final Builder DOUBLE = new Builder() {
        public Double build(Object data) {
            String asString = STRING.build(data);
            return asString == null ? null : Double.valueOf(asString);
        }

        public String toString() {
            return "double";
        }
    };
    public static final Builder BOOLEAN = new Builder() {
        public Boolean build(Object data) {
            return ((Long) data) == 1;
        }

        public String toString() {
            return "boolean";
        }
    };
    public static final Builder BYTE_ARRAY = new Builder() {
        public byte[] build(Object data) {
            return ((byte[]) data); // deleted == 1
        }

        public String toString() {
            return "byte[]";
        }
    };

    public static final Builder LONG = new Builder() {
        public Long build(Object data) {
            return (Long) data;
        }

        public String toString() {
            return "long";
        }

    };
    public static final Builder STRING = new Builder() {
        public String build(Object data) {
            return data == null ? null : SafeEncoder.encode((byte[]) data);
        }

        public String toString() {
            return "string";
        }

    };
    public static final Builder> STRING_LIST = new Builder>() {
        @SuppressWarnings("unchecked")
        public List build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final ArrayList result = new ArrayList(l.size());
            for (final byte[] barray : l) {
                if (barray == null) {
                    result.add(null);
                } else {
                    result.add(SafeEncoder.encode(barray));
                }
            }
            return result;
        }

        public String toString() {
            return "List";
        }

    };
    public static final Builder> STRING_MAP = new Builder>() {
        @SuppressWarnings("unchecked")
        public Map build(Object data) {
            final List flatHash = (List) data;
            final Map hash = new HashMap();
            final Iterator iterator = flatHash.iterator();
            while (iterator.hasNext()) {
                hash.put(SafeEncoder.encode(iterator.next()), SafeEncoder
                        .encode(iterator.next()));
            }

            return hash;
        }

        public String toString() {
            return "Map";
        }

    };
    public static final Builder> STRING_SET = new Builder>() {
        @SuppressWarnings("unchecked")
        public Set build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final Set result = new HashSet(l.size());
            for (final byte[] barray : l) {
                if (barray == null) {
                    result.add(null);
                } else {
                    result.add(SafeEncoder.encode(barray));
                }
            }
            return result;
        }

        public String toString() {
            return "Set";
        }

    };

    public static final Builder> BYTE_ARRAY_LIST = new Builder>() {
        @SuppressWarnings("unchecked")
        public List build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;

            return l;
        }

        public String toString() {
            return "List";
        }
    };

    public static final Builder> BYTE_ARRAY_ZSET = new Builder>() {
        @SuppressWarnings("unchecked")
        public Set build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final Set result = new LinkedHashSet(l);
            for (final byte[] barray : l) {
                if (barray == null) {
                    result.add(null);
                } else {
                    result.add(barray);
                }
            }
            return result;
        }

        public String toString() {
            return "ZSet";
        }
    };
    public static final Builder> BYTE_ARRAY_MAP = new Builder>() {
        @SuppressWarnings("unchecked")
        public Map build(Object data) {
            final List flatHash = (List) data;
            final Map hash = new HashMap();
            final Iterator iterator = flatHash.iterator();
            while (iterator.hasNext()) {
                hash.put(iterator.next(), iterator.next());
            }

            return hash;
        }

        public String toString() {
            return "Map";
        }

    };

    public static final Builder> STRING_ZSET = new Builder>() {
        @SuppressWarnings("unchecked")
        public Set build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final Set result = new LinkedHashSet(l.size());
            for (final byte[] barray : l) {
                if (barray == null) {
                    result.add(null);
                } else {
                    result.add(SafeEncoder.encode(barray));
                }
            }
            return result;
        }

        public String toString() {
            return "ZSet";
        }

    };

    public static final Builder> TUPLE_ZSET = new Builder>() {
        @SuppressWarnings("unchecked")
        public Set build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final Set result = new LinkedHashSet(l.size());
            Iterator iterator = l.iterator();
            while (iterator.hasNext()) {
                result.add(new Tuple(SafeEncoder.encode(iterator.next()),
                        Double.valueOf(SafeEncoder.encode(iterator.next()))));
            }
            return result;
        }

        public String toString() {
            return "ZSet";
        }

    };

    public static final Builder> TUPLE_ZSET_BINARY = new Builder>() {
        @SuppressWarnings("unchecked")
        public Set build(Object data) {
            if (null == data) {
                return null;
            }
            List l = (List) data;
            final Set result = new LinkedHashSet(l.size());
            Iterator iterator = l.iterator();
            while (iterator.hasNext()) {
                result.add(new Tuple(iterator.next(), Double
                        .valueOf(SafeEncoder.encode(iterator.next()))));
            }

            return result;

        }

        public String toString() {
            return "ZSet";
        }
    };

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy