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

xapi.collect.X_Collect Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
package xapi.collect;


import java.util.Comparator;

import xapi.collect.api.ClassTo;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.Dictionary;
import xapi.collect.api.Fifo;
import xapi.collect.api.HasValues;
import xapi.collect.api.IntTo;
import xapi.collect.api.ObjectTo;
import xapi.collect.api.StringDictionary;
import xapi.collect.api.StringTo;
import xapi.collect.impl.ArrayIterator;
import xapi.collect.impl.HashComparator;
import xapi.collect.impl.SingletonIterator;
import xapi.collect.service.CollectionService;
import xapi.inject.X_Inject;
import xapi.util.api.ReceivesValue;
import static xapi.collect.api.CollectionOptions.asImmutableList;
import static xapi.collect.api.CollectionOptions.asImmutableSet;
import static xapi.collect.api.CollectionOptions.asMutable;
import static xapi.collect.api.CollectionOptions.asMutableList;
import static xapi.collect.api.CollectionOptions.asMutableSet;
import static xapi.inject.X_Inject.singleton;

public class X_Collect {

  private X_Collect() {}
  
  public static final CollectionService service = singleton(CollectionService.class);

  public static final CollectionOptions IMMUTABLE = asMutable(false).build();
  public static final CollectionOptions IMMUTABLE_LIST = asImmutableList().build();
  public static final CollectionOptions IMMUTABLE_SET = asImmutableSet().build();
  public static final CollectionOptions MUTABLE = asMutable(true).build();
  public static final CollectionOptions MUTABLE_LIST = asMutableList().build();
  public static final CollectionOptions MUTABLE_SET = asMutableSet().build();

  public static  IntTo newList(Class cls) {
    return service.newList(cls, MUTABLE_LIST);
  }

  public static  IntTo asList(@SuppressWarnings("unchecked") V ... elements) {
    @SuppressWarnings("unchecked")
    IntTo list = (IntTo)newList(elements.getClass().getComponentType());
    for (V item : elements)
      list.push(item);
    return list;
  }

  public static  ObjectTo newMap(Class keyCls, Class valueCls) {
    return service.newMap(keyCls, valueCls, MUTABLE);
  }
  public static  ObjectTo newMap(Class keyCls, Class valueCls, CollectionOptions opts) {
    return service.newMap(keyCls, valueCls, opts);
  }

  public static  ClassTo newClassMap(Class valueCls) {
    return service.newClassMap(valueCls, MUTABLE);
  }

  public static  StringTo newStringMap(Class valueCls) {
    return service.newStringMap(valueCls, MUTABLE);
  }

  public static StringDictionary newStringDictionary() {
    return service.newDictionary();
  }

  public static  IntTo newSet(Class cls) {
    return service.newList(cls, MUTABLE_SET);
  }

  public static  IntTo asSet(@SuppressWarnings("unchecked") V ... elements) {
    @SuppressWarnings("unchecked")
    IntTo list = (IntTo)newSet(elements.getClass().getComponentType());
    for (V item : elements)
      list.push(item);
    return list;
  }

  public static  Comparator getComparator(CollectionOptions opts) {
    return new HashComparator();
  }

  @SuppressWarnings("unchecked")
  public static  Fifo newFifo() {
    return service.newFifo();
  }

  public static  void copyInto(HasValues from, HasValues into) {
    into.putAll(from.entries());
  }

  public static  void copyDictionary(final Dictionary from,final Dictionary into) {
    from.forKeys(new ReceivesValue() {
      @Override
      public void set(K key) {
        into.setValue(key, from.getValue(key));
      }
    });
  }

  public static  Iterable iterable(S item) {
    return new SingletonIterator(item);
  }
  public static  Iterable iterable(S ... items) {
    return new ArrayIterator(items);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy