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

xapi.gwt.collect.JsDictionary Maven / Gradle / Ivy

Go to download

This module exists solely to package all other gwt modules into a single uber jar. This makes deploying to non-mavenized targets much easier. Of course, you would be wise to inherit your dependencies individually; the uber jar is intended for projects like collide, which have complex configuration, and adding many jars would be a pain.

The newest version!
package xapi.gwt.collect;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import xapi.collect.impl.ArrayIterable;
import xapi.collect.impl.EntryValueAdapter;
import xapi.collect.impl.IteratorWrapper;

import com.google.gwt.core.client.JavaScriptObject;

public class JsDictionary  extends JavaScriptObject {

  protected JsDictionary() {
  }

  class KeyItr extends ArrayIterable{
    public KeyItr() {
      super(keyArray());
    }
    @Override
    protected void remove(String key) {
      JsDictionary.this.remove(key);
    }
  }

  class EntryItr implements Iterator> {

    int pos = 0;
    int max = keyArray().length;
    Entry entry;

    @Override
    public boolean hasNext() {
      return pos < max;
    }

    @Override
    public Entry next() {
      final String key = keyArray()[pos++];
      final V next = get(key);
      entry = new Entry() {

        @Override
        public String getKey() {
          return key;
        }

        @Override
        public V getValue() {
          return next;
        }

        @Override
        public V setValue(V value) {
          if (value == null) {
            return JsDictionary.this.removeAndReturn(key);
          }
          else
            return JsDictionary.this.put(key, value);
        }
      };
      return entry;
    }

    @Override
    public void remove() {
      assert entry != null : "You must call next() before remove() in JsDictionary.entries()";
      JsDictionary.this.remove(entry.getKey());
    }

  }

  public static native  JsDictionary create(Class valueType)
  /*-{
   return {_v$: valueType, _k: @xapi.gwt.collect.JsDictionary::newStringArray()() };
  }-*/;

  private static String[] newStringArray() {
    return new String[0];
  }

  protected final native String[] keyArray()
  /*-{
   return this._k;
   }-*/;

  public final native boolean containsKey(String key)
  /*-{
    return this.hasOwnProperty(key)&&this[key] != undefined;
  }-*/;

  public final boolean containsValue(V value) {
    String[] keys = keyArray();
    int i = keys.length;

    if (value == null) {
      for (;i-->0;) {
        if (get(keys[i]) == null)
         return true;
      }
    } else {
      for (;i-->0;) {
        if (value.equals(get(keys[i])))
          return true;
      }
    }
    return false;
  }

  public final native V get(String key)
  /*-{
    return this.hasOwnProperty(key) && this[key].v;
  }-*/;

  public final native V put(String key, V value)
  /*-{
    if (this.hasOwnProperty(key)) {
      var slot = this._k[this._k.length], v = slot.v;
      slot.v = value;
      return v;
    }
    var slot = {i : this._k.length, v: value};
    this[key] = slot;
    this._k[slot.i] = key;
    return null;
  }-*/;

  public final native boolean isEmpty()
  /*-{
    return this._k.length == 0;
  }-*/;

  public final native void clear()
  /*-{
    for (
      var i = this._k.length;
      i -->0;
      delete this[this._k[i]]
    );
    this._k.length = 0;
  }-*/;

  public final native boolean remove(String key)
  /*-{
    if (this.hasOwnProperty(key)) {
      var index = this._k.indexOf(key);
      delete this[key];
      this._k.splice(index, 1);
      return true;
    }
    // we don't allow you to delete items which aren't keys... like __prototype
    return false;
  }-*/;

  public final native V removeAndReturn(String key)
  /*-{
    if (this.hasOwnProperty(key)) {
      var value = this[key];
      delete this[key];
      return value;
    }
    return null;
  }-*/;

  public final void putAll(Iterable> items) {
    for (Entry item : items)
      put(item.getKey(), item.getValue());
  }

  public final void removeAll(Iterable items) {
    for (String item : items) {
      remove(item);
    }
  }

  public final Iterable keys() {
    return new KeyItr();
  }

  public final Iterable values() {
    return new EntryValueAdapter(entries());
  }

  public final Iterable> entries() {
    return new IteratorWrapper>(new EntryItr());
  }

  public final int size() {
    return keyArray().length;
  }

  public final V[] toArray() {
//    String[] keys = keyArray();
//    
//    V[] array = GwtReflect.newArray(valueType(), keys.length);
//    for (int i = keys.length; i --> 0; array[i] = get(keys[i]));
//    return array;
    throw new UnsupportedOperationException();
  }

  public final Collection toCollection(Collection into) {
    if (into == null) {
      into = new ArrayList();
    }
    String[] keys = keyArray();
    for (int i = keys.length; i-->0;into.add(get(keys[i])));
    return into;
  }

  public final Map toMap(Map into) {
    if (into == null) {
      into = new LinkedHashMap();
    }
    for (Entry entry : entries()) {
      into.put(entry.getKey(), entry.getValue());
    }
    return into;
  }

  public final native Class valueType()
  /*-{
    return this._v$;
  }-*/;

  public final Class keyType() {
    return String.class;
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy