예제 #1
0
파일: Storage.java 프로젝트: TradeLine/TWT
@JSClass
public final class Storage {

  public static Storage LOCAL = new Storage(Script.code("localStorage"));
  public static Storage SESSION = new Storage(Script.code("sessionStorage"));
  private final Object js;

  private Storage(Object js) {
    this.js = js;
  }

  public Optional<String> get(String key) {
    String s = Script.code(js, ".getItem(", key, ")");
    Optional<String> res = Optional.ofNullable(s);
    return res;
  }

  public Optional<String> get(int index) {
    Optional<String> key = getIndexByKey(index);
    if (key.isPresent()) return get(key.get());
    return Optional.empty();
  }

  public Optional<String> remove(String key) {
    Optional<String> r = get(key);
    if (r.isPresent()) Script.code(js, ".removeItem(", key, ")");
    return r;
  }

  public Optional<String> remove(int index) {
    Optional<String> key = getIndexByKey(index);
    if (key.isPresent()) return remove(key.get());
    return Optional.empty();
  }

  public Optional<String> getIndexByKey(int index) {
    return Optional.ofNullable(Script.code(js, ".key(", CastUtil.toObject(index), ")"));
  }

  public int size() {
    return CastUtil.toInt(Script.code(js, ".length"));
  }

  public Storage set(String key, String value) {
    Script.code(js, ".setItem(", key, ",", value, ")");
    return this;
  }

  public Storage set(int index, String value) {
    Optional<String> key = getIndexByKey(index);
    if (key.isPresent()) return set(key.get(), value);
    return this;
  }

  public Storage clear() {
    Script.code(js, ".clear()");
    return this;
  }
}
예제 #2
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public Storage clear() {
   Script.code(js, ".clear()");
   return this;
 }
예제 #3
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public Storage set(String key, String value) {
   Script.code(js, ".setItem(", key, ",", value, ")");
   return this;
 }
예제 #4
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public int size() {
   return CastUtil.toInt(Script.code(js, ".length"));
 }
예제 #5
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public Optional<String> getIndexByKey(int index) {
   return Optional.ofNullable(Script.code(js, ".key(", CastUtil.toObject(index), ")"));
 }
예제 #6
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public Optional<String> remove(String key) {
   Optional<String> r = get(key);
   if (r.isPresent()) Script.code(js, ".removeItem(", key, ")");
   return r;
 }
예제 #7
0
파일: Storage.java 프로젝트: TradeLine/TWT
 public Optional<String> get(String key) {
   String s = Script.code(js, ".getItem(", key, ")");
   Optional<String> res = Optional.ofNullable(s);
   return res;
 }