de.cm.frw.core.fun.impl
Class Bag

java.lang.Object
  extended by de.cm.frw.core.fun.impl.Bag
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection

public class Bag
extends java.lang.Object
implements java.util.Collection

This is a collection wrapper useful for preparing argument lists for apply. It implements all collection method by delegating them to the wrapped collection. Additional methods are provided that return the Bag instance. This allows to chain calls in a single line, e.g.

Function sum = (Function) reduce.apply(new Bag().put(add).put(new Double(0.0)));

for expressing sum = reduce add 0 (see WhyFPExamples)

Author:
cm

Constructor Summary
Bag()
           
Bag(java.util.Collection c)
           
Bag(java.lang.Object[] a)
           
 
Method Summary
 boolean add(java.lang.Object e)
           
 boolean addAll(java.util.Collection c)
           
 void clear()
           
 boolean contains(java.lang.Object o)
           
 boolean containsAll(java.util.Collection c)
           
static Bag drop(Bag bag, Predicate pred)
          Drop all elements of a given bag where given predicate holds
 Bag drop(java.lang.Object e)
           
 Bag dropAll(java.util.Collection c)
           
static Bag from(int i)
           
static java.util.Collection fromArray(java.lang.Object[] a)
          Create a bag putting in the given objects
 boolean isEmpty()
           
 java.util.Iterator iterator()
           
static Bag keep(Bag bag, Predicate pred)
          Keep all elements of a given bag where given predicate holds
static java.util.Collection newList()
           
static java.util.Collection newList(int[] a)
          Create a bag of boxed integers from a given int array
 Bag put(boolean x)
           
 Bag put(byte x)
           
 Bag put(char x)
           
 Bag put(double x)
           
 Bag put(float x)
           
 Bag put(int x)
           
 Bag put(long x)
           
 Bag put(java.lang.Object e)
           
 Bag putAll(boolean[] x)
           
 Bag putAll(byte[] x)
           
 Bag putAll(char[] x)
           
 Bag putAll(java.util.Collection c)
           
 Bag putAll(double[] x)
           
 Bag putAll(float[] x)
           
 Bag putAll(int[] x)
           
 Bag putAll(long[] x)
           
 java.util.Collection putAll(java.lang.Object[] objects)
           
 boolean remove(java.lang.Object e)
           
 boolean removeAll(java.util.Collection c)
           
 Bag removeIfNot(Predicate pred)
          Destructively remove all items from the bag for which the given predicate does not hold
 boolean retainAll(java.util.Collection c)
           
 int size()
           
 Bag to(int end)
           
 java.lang.Object[] toArray()
           
 java.lang.Object[] toArray(java.lang.Object[] a)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

Bag

public Bag()

Bag

public Bag(java.lang.Object[] a)

Bag

public Bag(java.util.Collection c)
Method Detail

put

public Bag put(java.lang.Object e)

put

public Bag put(boolean x)

putAll

public Bag putAll(boolean[] x)

put

public Bag put(byte x)

putAll

public Bag putAll(byte[] x)

put

public Bag put(char x)

putAll

public Bag putAll(char[] x)

put

public Bag put(double x)

putAll

public Bag putAll(double[] x)

put

public Bag put(float x)

putAll

public Bag putAll(float[] x)

put

public Bag put(int x)

putAll

public Bag putAll(int[] x)

put

public Bag put(long x)

putAll

public Bag putAll(long[] x)

add

public boolean add(java.lang.Object e)
Specified by:
add in interface java.util.Collection

putAll

public Bag putAll(java.util.Collection c)

addAll

public boolean addAll(java.util.Collection c)
Specified by:
addAll in interface java.util.Collection

clear

public void clear()
Specified by:
clear in interface java.util.Collection

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection

containsAll

public boolean containsAll(java.util.Collection c)
Specified by:
containsAll in interface java.util.Collection

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection

drop

public Bag drop(java.lang.Object e)

remove

public boolean remove(java.lang.Object e)
Specified by:
remove in interface java.util.Collection

dropAll

public Bag dropAll(java.util.Collection c)

removeAll

public boolean removeAll(java.util.Collection c)
Specified by:
removeAll in interface java.util.Collection

retainAll

public boolean retainAll(java.util.Collection c)
Specified by:
retainAll in interface java.util.Collection

size

public int size()
Specified by:
size in interface java.util.Collection

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Specified by:
toArray in interface java.util.Collection

newList

public static java.util.Collection newList(int[] a)
Create a bag of boxed integers from a given int array

Parameters:
a - the array of integers to be boxed and bagged
Returns:

fromArray

public static java.util.Collection fromArray(java.lang.Object[] a)
Create a bag putting in the given objects

Parameters:
a - the array of objects to be bagged
Returns:

newList

public static java.util.Collection newList()

from

public static Bag from(int i)

to

public Bag to(int end)

removeIfNot

public Bag removeIfNot(Predicate pred)
                throws Function.InvalidArguments
Destructively remove all items from the bag for which the given predicate does not hold

Parameters:
pred - the predicate used to filter
Returns:
the decimated bag
Throws:
Function.InvalidArguments

keep

public static Bag keep(Bag bag,
                       Predicate pred)
                throws Function.InvalidArguments
Keep all elements of a given bag where given predicate holds

Parameters:
bag -
pred -
Returns:
a new Bag keeping all elements of bag where pred holds
Throws:
Function.InvalidArguments

drop

public static Bag drop(Bag bag,
                       Predicate pred)
                throws Function.InvalidArguments
Drop all elements of a given bag where given predicate holds

Parameters:
bag -
pred -
Returns:
a new Bag dropping all elements of bag where pred holds
Throws:
Function.InvalidArguments

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

putAll

public java.util.Collection putAll(java.lang.Object[] objects)