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

xapi.gwt.collect.IntToListGwt 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.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.inject.Provider;

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

import xapi.annotation.inject.InstanceOverride;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.IntTo;
import xapi.collect.api.ObjectTo;
import xapi.except.NotYetImplemented;
import xapi.platform.GwtPlatform;
import xapi.util.X_Util;
import xapi.util.impl.AbstractPair;


@GwtPlatform
@InstanceOverride(implFor=IntTo.class)
@GwtScriptOnly
public class IntToListGwt  extends JavaScriptObject implements IntTo{

  public static  IntTo create(final Provider arrayProvider) {
    return JavaScriptObject.createArray().>cast().setArrayProvider(arrayProvider);
  }


  public static  IntTo createForClass(final Class cls) {
    return create(new Provider() {
      @SuppressWarnings("unchecked")
      @Override
      public V[] get() {
        return (V[])Array.newInstance(cls, 0);
      }
    });
  }

  public static native  IntTo newInstance()
  /*-{
   return [];
  }-*/;

  protected IntToListGwt() {}


  @Override
  public final boolean add(final E item) {
    set(size(), item);
    return true;
  }

  @Override
  @SuppressWarnings("unchecked")
  public final boolean addAll(final E... items) {
    for (final E item : items) {
      add(item);
    }
    return true;
  }

  @Override
  public final boolean addAll(final Iterable items) {
    for (final E item : items) {
      add(item);
    }
    return true;
  }

  @Override
  public final Deque asDeque() {
    final LinkedList set = new LinkedList();
    for (final E e : forEach()) {
      set.add(e);
    }
    return set;
  }
  @Override
  public final List asList() {
    final ArrayList list = new ArrayList();
    for (final E e : forEach()) {
      list.add(e);
    }
    return list;
  }

  @Override
  public final Set asSet() {
    final Set set = new LinkedHashSet();
    for (final E e : forEach()) {
      set.add(e);
    }
    return set;
  }

  @Override
  public final E at(final int index) {
    return getValue(index);
  }

  @Override
  public final native void clear()
  /*-{
    this.splice(0, this.length);
  }-*/;

  @Override
  public final ObjectTo clone(final CollectionOptions options) {
    throw new NotYetImplemented("IntToList.clone not yet supported");
  }

  @Override
  public final boolean contains(final E value) {
    for (final E e : forEach()) {
      if (X_Util.equal(value, e)) {
        return true;
      }
    }
    return false;
  }

  @Override
  @SuppressWarnings("unchecked")
  public final Entry entryFor(final Object key) {
    return new AbstractPair(size(), (E)key);
  }

  @Override
  public final boolean findRemove(final E value, final boolean all) {
    boolean removed = false;
    for (int i = 0, m = size(); i < m; i++) {
      final E next = getValue(i);
      if (X_Util.equal(next, value)) {
        remove(i--);
        if (!all) {
          return true;
        }
        removed = true;
      }
    }
    return removed;
  }

  @Override
  public final Iterable forEach() {
    return new IntTo.IntToIterable(this);
  }

  @Override
  public final E get(final Object key) {
    return getValue((Integer)key);
  }

  public final native E getValue(int key)
  /*-{
    return this[key]||null;
  }-*/;

  @Override
  public final int indexOf(final E value) {
    for (int i = 0, m = size(); i < m; i++) {
      if (X_Util.equal(getValue(i), value)) {
        return i;
      }
    }
    return -1;
  }

  @Override
  public final native boolean insert(int pos, E item)
  /*-{
    while(this.length < pos){
      this.push(undefined);
    }
    this.splice(pos, 0, item);
    return true;
  }-*/;

  @Override
  public final boolean isEmpty() {
    return size()==0;
  }

  @Override
  public final native E pop()
  /*-{
    return this.splice(this.length-1, 1);
  }-*/;

  @Override
  public final void push(final E value) {
    set(size(), value);
  }

  @Override
  public final E put(final Entry item) {
    set(item.getKey(), item.getValue());
    return item.getValue();
  }

  @Override
  public final boolean remove(final int index) {
    if (index < size()) {
      return splice(index) != null;
    }
    return false;
  }

  @Override
  public final E remove(final Object key) {
    return splice((Integer)key);
  }

  @Override
  public final native void set(int index, E value)
  /*-{
    while (this.length < index)
      this[this.length] = null;
    this[index] = value;
  }-*/;

  public final native IntToListGwt setArrayProvider(Provider provider)
  /*-{
     this.toArray = function(){return [email protected]::get()();}
     return this;
   }-*/;

  @Override
  @SuppressWarnings("unchecked")
  public final void setValue(final Object key, final Object value) {
    set((Integer)key, (E)value);
  }

  @Override
  public final native int size()
  /*-{
    return this.length;
  }-*/;

  public final native E splice(int key)
  /*-{
    if (key < this.length) {
      var ret = this[key];
      this.splice(key, 1);
      return ret;
    }
    return null;
  }-*/;


  @Override
  public final native E[] toArray()
  /*-{
    if (this.toArray) {
      var arr = this.toArray();
      this.forEach(function(i) {
        arr[arr.length] = i;
      });
      return arr;
    }
    throw "toArray not supported";
  }-*/;

  @Override
  public final Collection toCollection(Collection into) {
    if (into == null) {
      into = new ArrayList();
    }
    for (final E e : forEach()) {
      into.add(e);
    }
    return into;
  }

  @Override
  public final Map toMap(Map into) {
    if (into == null) {
      into = new LinkedHashMap();
    }
    for (int i = 0, m = size(); i < m; i++) {
      into.put(i, getValue(i));
    }
    return into;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy