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

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

Go to download

A java client for github.com/antirez/redis, a fork of github.com/xetorthio/jedis

The newest version!
package redis.clients.jedis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.util.JedisByteHashMap;
import redis.clients.util.SafeEncoder;

public final class BuilderFactory {
  public static final Builder DOUBLE = new Builder() {
    @Override
    public Double build(Object data) {
      String string = STRING.build(data);
      if (string == null) return null;
      try {
        return Double.valueOf(string);
      } catch (NumberFormatException e) {
        if (string.equals("inf") || string.equals("+inf")) return Double.POSITIVE_INFINITY;
        if (string.equals("-inf")) return Double.NEGATIVE_INFINITY;
        throw e;
      }
    }

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

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

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

  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 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;
      }
      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;
    }

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

  };
  public static final Builder> STRING_MAP = new Builder>() {
    @Override
    @SuppressWarnings("unchecked")
    public Map build(Object data) {
      final List flatHash = (List) data;
      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 "Map";
    }

  };

  public static final Builder> PUBSUB_NUMSUB_MAP = new Builder>() {
    @Override
    @SuppressWarnings("unchecked")
    public Map build(Object data) {
      final List flatHash = (List) data;
      final Map hash = new HashMap(flatHash.size()/2, 1);
      final Iterator iterator = flatHash.iterator();
      while (iterator.hasNext()) {
        hash.put(SafeEncoder.encode((byte[]) iterator.next()),
          String.valueOf((Long) iterator.next()));
      }

      return hash;
    }

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

  };

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

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

  };

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

      return l;
    }

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

  public static final Builder> BYTE_ARRAY_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);
      for (final byte[] barray : l) {
        if (barray == null) {
          result.add(null);
        } else {
          result.add(barray);
        }
      }
      return result;
    }

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

      return hash;
    }

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

  };

  public static final Builder> STRING_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(), 1);
      for (final byte[] barray : l) {
        if (barray == null) {
          result.add(null);
        } else {
          result.add(SafeEncoder.encode(barray));
        }
      }
      return result;
    }

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

  };

  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 EVAL_RESULT = new Builder() {

    @Override
    public Object build(Object data) {
      return evalResult(data);
    }

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

    private Object evalResult(Object result) {
      if (result instanceof byte[]) return SafeEncoder.encode((byte[]) result);

      if (result instanceof List) {
        List list = (List) result;
        List listResult = new ArrayList(list.size());
        for (Object bin : list) {
          listResult.add(evalResult(bin));
        }

        return listResult;
      }

      return result;
    }

  };

  public static final Builder EVAL_BINARY_RESULT = new Builder() {

    @Override
    public Object build(Object data) {
      return evalResult(data);
    }

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

    private Object evalResult(Object result) {
      if (result instanceof List) {
        List list = (List) result;
        List listResult = new ArrayList(list.size());
        for (Object bin : list) {
          listResult.add(evalResult(bin));
        }

        return listResult;
      }

      return result;
    }

  };

  public static final Builder> GEO_COORDINATE_LIST = new Builder>() {
    @Override
    public List build(Object data) {
      if (null == data) {
        return null;
      }
      return interpretGeoposResult((List) data);
    }

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

    private List interpretGeoposResult(List responses) {
      List responseCoordinate = new ArrayList(responses.size());
      for (Object response : responses) {
        if (response == null) {
          responseCoordinate.add(null);
        } else {
          List respList = (List) response;
          GeoCoordinate coord = new GeoCoordinate(DOUBLE.build(respList.get(0)),
              DOUBLE.build(respList.get(1)));
          responseCoordinate.add(coord);
        }
      }
      return responseCoordinate;
    }
  };

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

      List objectList = (List) data;

      List responses = new ArrayList(objectList.size());
      if (objectList.isEmpty()) {
        return responses;
      }

      if (objectList.get(0) instanceof List) {
        // list of members with additional informations
        GeoRadiusResponse resp;
        for (Object obj : objectList) {
          List informations = (List) obj;

          resp = new GeoRadiusResponse((byte[]) informations.get(0));

          int size = informations.size();
          for (int idx = 1; idx < size; idx++) {
            Object info = informations.get(idx);
            if (info instanceof List) {
              // coordinate
              List coord = (List) info;

              resp.setCoordinate(new GeoCoordinate(DOUBLE.build(coord.get(0)),
                  DOUBLE.build(coord.get(1))));
            } else {
              // distance
              resp.setDistance(DOUBLE.build(info));
            }
          }

          responses.add(resp);
        }
      } else {
        // list of members
        for (Object obj : objectList) {
          responses.add(new GeoRadiusResponse((byte[]) obj));
        }
      }

      return responses;
    }

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

  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";
    }

  };

  private BuilderFactory() {
    throw new InstantiationError( "Must not instantiate this class" );
  }

}