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

xapi.collect.impl.IntToList 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.collect.impl;

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.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import xapi.collect.api.CollectionOptions;
import xapi.collect.api.IntTo;
import xapi.collect.api.ObjectTo;
import xapi.except.NotYetImplemented;
import xapi.util.X_Util;
import xapi.util.impl.AbstractPair;

public class IntToList implements IntTo {

  private final ArrayList list = new ArrayList(10);
  private final Class type;
  
  public IntToList(Class cls) {
    this.type = cls;
  }

  @Override
  public Iterable forEach() {
    return list;
  }

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

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

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

  @Override
  public E get(Object key) {
    return list.get((Integer)key);
  }

  @Override
  @SuppressWarnings("unchecked")
  public void setValue(Object key, Object value) {
    int k = (Integer)key;
    list.ensureCapacity(k);
    list.set(k, (E)value);
  }

  @Override
  public E remove(Object key) {
    int k = (Integer)key;
    if (k < list.size())
      return list.remove(k);
    return null;
  }

  @Override
  public int size() {
    return list.size();
  }

  @Override
  @SuppressWarnings("unchecked")
  public E[] toArray() {
    return list.toArray((E[])Array.newInstance(type, list.size()));
  }

  @Override
  public Collection toCollection(Collection into) {
    if (into == null)
      into = newCollection();
    into.addAll(list);
    return into;
  }

  protected Collection newCollection() {
    return new ArrayList();
  }

  @Override
  public Map toMap(Map into) {
    if (into == null) {
      into = newMap();
    }
    ListIterator iter = list.listIterator();
    while (iter.hasNext()) {
      into.put(iter.nextIndex(), iter.next());
    }
    return into;
  }

  private Map newMap() {
    return new LinkedHashMap();
  }

  @Override
  public boolean isEmpty() {
    return list.isEmpty();
  }

  @Override
  public void clear() {
    list.clear();
  }

  @Override
  public boolean add(E item) {
    return list.add(item);
  }
  
  @Override
  @SuppressWarnings("unchecked")
  public boolean addAll(E... items) {
    for (E item : items) {
      list.add(item);
    }
    return true;
  }
  
  @Override
  public boolean addAll(Iterable items) {
    if (items instanceof Collection) {
      list.addAll((Collection)items);
    } else {
      for (E item : items) {
        list.add(item);
      }
    }
    return true;
  }
  
  @Override
  public boolean insert(int pos, E item) {
    list.add(pos, item);
    return true;
  }

  @Override
  public boolean contains(E value) {
    return list.contains(value);
  }

  @Override
  public E at(int index) {
    if (index < list.size())
      return list.get(index);
    return null;
  }

  @Override
  public int indexOf(E value) {
    return list.indexOf(value);
  }

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

  @Override
  public boolean findRemove(E value, boolean all) {
    ListIterator iter = list.listIterator();
    boolean removed = false;
    while (iter.hasNext()) {
      E next = iter.next();
      if (X_Util.equal(next, value)) {
        iter.remove();
        if (!all)
          return true;
        removed = true;
      }
    }
    return removed;
  }

  @Override
  public void set(int index, E value) {
    ensureCapacity(index);
    list.set(index, value);
  }
  
  private void ensureCapacity(int index) {
    while(list.size()<=index) {
      list.add(getDefaultValue());
    }
  }

  private E getDefaultValue() {
    return null;
  }

  @Override
  public void push(E value) {
    list.add(value);
  }

  @Override
  public E pop() {
    if (list.size() == 0)
      return null;
    return list.remove(0);
  }

  @Override
  public List asList() {
    ArrayList list = new ArrayList();
    list.addAll(this.list);
    return list;
  }

  @Override
  public Set asSet() {
    Set set = new LinkedHashSet();
    set.addAll(list);
    return set;
  }

  @Override
  public Deque asDeque() {
    LinkedList deque = new LinkedList();
    deque.addAll(list);
    return deque;
  }
  
  @Override
  public String toString() {
    return list.toString();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy