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

com.aliyun.tair.tairgis.factory.GisBuilderFactory Maven / Gradle / Ivy

package com.aliyun.tair.tairgis.factory;

import com.aliyun.tair.tairgis.params.GisSearchResponse;
import redis.clients.jedis.Builder;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.util.SafeEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GisBuilderFactory {

    public static final Builder> GISSEARCH_RESULT_MAP_STRING = new Builder>() {
        @Override
        public Map build(Object data) {
            List rawResults;
            List result = (List) data;
            if (null == result || 0 == result.size()) {
                rawResults = null;
            } else {
                rawResults = (List) result.get(1);
            }

            final List flatHash = (List) rawResults;
            final Map hash = new HashMap(flatHash.size()/2, 1);
            final Iterator iterator = flatHash.iterator();
            while (iterator.hasNext()) {
                hash.put(SafeEncoder.encode(iterator.next()), SafeEncoder.encode(iterator.next()));
            }
            return hash;
        }

        @Override
        public String toString() {
            return "gisResult";
        }
    };

    public static final Builder> GISSEARCH_RESULT_MAP_BYTE = new Builder>() {
        @Override
        public Map build(Object data) {
            List rawResults;
            List result = (List) data;
            if (null == result || 0 == result.size()) {
                rawResults = null;
            } else {
                rawResults = (List) result.get(1);
            }

            final List flatHash = (List) rawResults;
            final Map hash = new HashMap(flatHash.size()/2, 1);
            final Iterator iterator = flatHash.iterator();
            while (iterator.hasNext()) {
                hash.put(iterator.next(), iterator.next());
            }
            return hash;
        }

        @Override
        public String toString() {
            return "gisResult";
        }
    };

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

            List l = (List)((List)data).get(1);
            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;
        }

        @Override
        public String toString() {
            return "gisResult>";
        }
    };

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

        @Override
        public String toString() {
            return "gisResult>";
        }
    };

    public static final Builder> GISSEARCH_WITH_PARAMS_RESULT = new Builder>() {
        @Override
        public List build(Object data) {
            List objectList = (List)data;
            if (objectList == null || objectList.isEmpty()) {
                return  null;
            }

            long number = (Long) objectList.get(0);
            List rawResults = (List) objectList.get(1);
            List responses = new ArrayList<>();
            if ((number == 0) || (rawResults.isEmpty())) {
                return responses;
            }

            int size = rawResults.size() / (int)number;

            for (int i = 0; i < number; i++) {
                GisSearchResponse resp = new GisSearchResponse();
                resp.setField((byte[])rawResults.get(i * size));
                for (int j = i * size + 1; j < (i + 1) * size; j++) {
                    Object obj = rawResults.get(j);
                    if (canConvertToDouble(obj)) {
                        resp.setDistance(BuilderFactory.DOUBLE.build(obj));
                    } else {
                        resp.setValue((byte[])obj);
                    }
                }
                responses.add(resp);
            }
            return responses;
        }
    };

    private static boolean canConvertToDouble(Object data) {
        String string = BuilderFactory.STRING.build(data);
        if (string == null) return false;

        try {
             Double.valueOf(string);
             return true;
        } catch (NumberFormatException e) {
            if (string.equals("inf") || string.equals("+inf") || string.equals("-inf")) return true;
            return false;
        }
    }
}