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

ch.epfl.labos.iu.orm.BaseSet Maven / Gradle / Ivy

Go to download

Jinq public API for extending Java 8 streams with database functionality

There is a newer version: 2.0.2
Show newest version
package ch.epfl.labos.iu.orm;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.jinq.tuples.Pair;

abstract public class BaseSet implements DBSet
{
   abstract public int size();
   abstract public Iterator iterator();
   abstract public boolean add(T o);
   abstract public boolean remove(Object o);

   // Reusable stuff that depends on other api

   public T getValue()
   {
      assert(size() == 1);
      return iterator().next();
   }
   public T get()
   {
      return getValue();
   }

   public DBSet with(T toAdd)
   {
      DBSet newset = new VectorSet();
      for (T o: this)
         newset.add(o);
      newset.add(toAdd);
      return newset;
   }
   
   public DBSet sortedByIntAscending(final IntSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return (new Integer(sorter.value(o1))).compareTo(new Integer(sorter.value(o2)));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByIntDescending(final IntSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return -(new Integer(sorter.value(o1))).compareTo(new Integer(sorter.value(o2)));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByDoubleAscending(final DoubleSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return (new Double(sorter.value(o1))).compareTo(new Double(sorter.value(o2)));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByDoubleDescending(final DoubleSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return -(new Double(sorter.value(o1))).compareTo(new Double(sorter.value(o2)));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByStringAscending(final StringSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return sorter.value(o1).compareTo(sorter.value(o2));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByStringDescending(final StringSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return -sorter.value(o1).compareTo(sorter.value(o2));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByDateAscending(final DateSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return sorter.value(o1).compareTo(sorter.value(o2));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet sortedByDateDescending(final DateSorter sorter)
   {
      Vector sorted = new Vector();
      sorted.addAll(this);
      Collections.sort(sorted, new Comparator(){
         public int compare(T o1, T o2)
         {
            return -sorter.value(o1).compareTo(sorter.value(o2));
         }
      });
      VectorSet newSet = new VectorSet();
      newSet.addAll(sorted);
      return newSet;
   }
   public DBSet firstN(int n)
   {
      VectorSet newSet = new VectorSet();
      for (T obj: this)
      {
         if (n <= 0) return newSet;
         n--;
         newSet.add(obj);
      }
      return newSet;
   }
   
   public boolean isEmpty()
   {
      return size() == 0;
   }

   public boolean contains(Object o)
   {
      for (T obj: this)
         if (obj.equals(o))
            return true;
      return false;
   }

   public Object[] toArray()
   {
      Object [] arr = new Object[size()];
      int n = 0;
      for (T obj: this)
      {
         arr[n] = obj;
         n++;
      }
      return arr;
   }

   public  U[] toArray(U[] a)
   {
      U [] arr;
      if (a.length >= size())
         arr = a;
      else
         arr = (U[])java.lang.reflect.Array.newInstance(
            a.getClass().getComponentType(), size());
      int n = 0;
      for (T obj: this)
      {
         arr[n] = (U)obj;
         n++;
      }
      return arr;
   }

   public boolean containsAll(Collection c)
   {
      for (Object o: c)
      {
         if (!contains(o))
            return false;
      }
      return true;
   }

   public boolean addAll(Collection c)
   {
      boolean changed = false;
      for (T obj: c)
         changed |= add(obj);
      return changed;
   }

   public boolean removeAll(Collection c)
   {
      boolean changed = false;
      for (Object obj: c)
         changed |= remove(obj);
      return changed;
   }

   public boolean retainAll(Collection c)
   {
      Vector toDelete = new Vector();
      for (T obj: this)
         if (!c.contains(obj))
            toDelete.add(obj);
      return removeAll(toDelete);
   }

   public void clear()
   {
      Vector toDelete = new Vector();
      for (T obj: this)
         toDelete.add(obj);
      removeAll(toDelete);
   }
   
   // New stuff for Queryll2
   public DBSet where(Where test)
   {
      DBSet newset = new VectorSet();
      for (T o: this)
         if (test.where(o))
            newset.add(o);
      return newset;
   }

   public  DBSet select(Select select)
   {
      DBSet newset = new VectorSet();
      for (T o: this)
         newset.add(select.select(o));
      return newset;
   }
   
   public  DBSet> join(Join join)
   {
      DBSet> newset = new VectorSet>();
      for (T o: this)
      {
         DBSet other = join.join(o);
         for (U p: other)
            newset.add(new Pair(o, p));
      }
      return newset;
   }
   
   public double sumDouble(AggregateDouble aggregate)
   {
      double val = 0;
      for (T o: this)
         val += aggregate.aggregate(o);
      return val;
   }
   public int sumInt(AggregateInteger aggregate)
   {
      int val = 0;
      for (T o: this)
         val += aggregate.aggregate(o);
      return val;
   }
   public double maxDouble(AggregateDouble aggregate)
   {
      double val = Double.NEGATIVE_INFINITY;
      for (T o: this)
         val = Math.max(val, aggregate.aggregate(o));
      return val;
   }
   public int maxInt(AggregateInteger aggregate)
   {
      int val = Integer.MIN_VALUE;
      for (T o: this)
         val = Math.max(val, aggregate.aggregate(o));
      return val;
   }
   public  U selectAggregates(AggregateSelect aggregate)
   {
      U val = aggregate.aggregateSelect(this);
      return val;
   }
   
   public DBSet unique()
   {
      DBSet newset = new VectorSet();
      for (T o: this)
         if (!newset.contains(o))
            newset.add(o);
      return newset;
   }

   public  DBSet> group(Select select, AggregateGroup aggregate)
   {
      HashMap> map = new HashMap>();
      for (T o: this)
      {
         U key = select.select(o);
         if (!map.containsKey(key))
            map.put(key, new VectorSet());
         map.get(key).add(o);
      }
      DBSet> result = new VectorSet>();
      for (Map.Entry> entry: map.entrySet())
      {
         result.add(new Pair(entry.getKey(), aggregate.aggregateSelect(entry.getKey(), entry.getValue())));
      }
      return result;
   }
}