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

com.twitter.finagle.memcached.java.ClientBase Maven / Gradle / Ivy

There is a newer version: 21.2.0
Show newest version
package com.twitter.finagle.memcached.java;

import java.util.List;
import java.util.Map;

import scala.Option;
import scala.Tuple2;
import scala.collection.JavaConversions;

import com.twitter.finagle.memcached.GetResult;
import com.twitter.finagle.memcached.GetsResult;
import com.twitter.io.Buf;
import com.twitter.util.Function;
import com.twitter.util.Future;
import com.twitter.util.Time;

public class ClientBase extends Client {

  protected com.twitter.finagle.memcached.Client underlying;

  public ClientBase(com.twitter.finagle.memcached.Client underlying) {
    this.underlying = underlying;
  }

  public Future get(String key) {
    Future> result = underlying.get(key);
    return result.map(new Function, Buf>() {
      public Buf apply(Option value) {
        if (value.isDefined()) {
          return value.get();
        } else {
          return null;
        }
      }
    });
  }

  public Future gets(String key) {
    Future>> result = underlying.gets(key);
    return result.map(new Function>, ResultWithCAS>() {
      public ResultWithCAS apply(Option> value) {
        if (value.isDefined()) {
          return new ResultWithCAS(value.get()._1(), value.get()._2());
        } else {
          return null;
        }
      }
    });
  }

  public Future> get(List keys) {
    Future> result =
      underlying.get(JavaConversions.asScalaBuffer(keys));
    return result.map(
      new Function, Map>() {
        public Map apply(scala.collection.immutable.Map map) {
          return JavaConversions.mapAsJavaMap(map);
        }
      }
    );
  }

  public Future> gets(List keys) {
    Future>> result =
      underlying.gets(JavaConversions.asScalaBuffer(keys));
    return result.map(new Function<
      scala.collection.immutable.Map>,
      Map>() {
      public Map apply(
        scala.collection.immutable.Map> map) {
        return JavaConversions.mapAsJavaMap(
            map.mapValues(new Function, ResultWithCAS>() {
              public ResultWithCAS apply(Tuple2 tuple) {
                return new ResultWithCAS(tuple._1(), tuple._2());
              }
            })
        );
      }
    });
  }

  public Future getResult(List keys) {
    return underlying.getResult(JavaConversions.asScalaBuffer(keys));
  }

  public Future getsResult(List keys) {
    return underlying.getsResult(JavaConversions.asScalaBuffer(keys));
  }

  public Future set(String key, Buf value) {
    Future result = underlying.set(key, value);
    return result.map(new Function() {
      public Void apply(scala.runtime.BoxedUnit obj) {
        return null;
      }
    });
  }

  public Future set(String key, int flags, Time expiry, Buf value) {
    Future result = underlying.set(key, flags, expiry, value);
    return result.map(new Function() {
      public Void apply(scala.runtime.BoxedUnit obj) {
        return null;
      }
    });
  }

  public Future add(String key, Buf value) {
    return underlying.add(key, value);
  }

  public Future add(String key, int flags, Time expiry, Buf value) {
    return underlying.add(key, flags, expiry, value);
  }

  public Future append(String key, Buf value) {
    return underlying.append(key, value);
  }

  public Future prepend(String key, Buf value) {
    return underlying.prepend(key, value);
  }

  public Future replace(String key, Buf value) {
    return underlying.replace(key, value);
  }

  public Future replace(String key, int flags, Time expiry, Buf value) {
    return underlying.replace(key, flags, expiry, value);
  }

  public Future cas(String key, Buf value, Buf casUnique) {
    return underlying.cas(key, value, casUnique);
  }

  public Future cas(
    String key, int flags, Time expiry,
    Buf value, Buf casUnique) {
    return underlying.cas(key, flags, expiry, value, casUnique);
  }

  public Future delete(String key) {
    return underlying.delete(key);
  }

  public Future incr(String key) {
    Future> result = underlying.incr(key);
    return result.map(new Function, Long>() {
      public Long apply(Option value) {
        if (value.isDefined()) {
          return value.get();
        } else {
          return -1L;
        }
      }
    });
  }

  public Future incr(String key, long delta) {
    Future> result = underlying.incr(key, delta);
    return result.map(new Function, Long>() {
      public Long apply(Option value) {
        if (value.isDefined()) {
          return value.get();
        } else {
          return -1L;
        }
      }
    });
  }

  public Future decr(String key) {
    Future> result = underlying.decr(key);
    return result.map(new Function, Long>() {
      public Long apply(Option value) {
        if (value.isDefined()) {
          return value.get();
        } else {
          return -1L;
        }
      }
    });
  }

  public Future decr(String key, long delta) {
    Future> result = underlying.decr(key, delta);
    return result.map(new Function, Long>() {
      public Long apply(Option value) {
        if (value.isDefined()) {
          return value.get();
        } else {
          return -1L;
        }
      }
    });
  }

  public void release() {
    underlying.release();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy