jaggregate
Class Bag<E>

java.lang.Object
  extended by jaggregate.AbstractCollection<E>
      extended by jaggregate.AbstractExtensibleCollection<E>
          extended by jaggregate.AbstractBag<E>
              extended by jaggregate.Bag<E>
Type Parameters:
E - a restriction on the types of elements that may be included in the bag
All Implemented Interfaces:
Collection<E>, ExtensibleCollection<E>, Serializable

public class Bag<E>
extends AbstractBag<E>
implements Serializable

A bag for which equivalence is defined by equals.

Version:
$Id: Bag.java,v 1.5 2008/05/09 03:12:56 pholser Exp $
Author:
Paul Holser
See Also:
Serialized Form

Constructor Summary
Bag()
          Creates an empty bag.
Bag(Collection<? extends E> elements)
          Creates a bag containing the elements in the given collection.
Bag(E... elements)
          Creates a bag containing the elements in the given array.
Bag(Iterable<? extends E> elements)
          Creates a bag containing the elements given by an iterable object.
 
Method Summary
static
<T> Bag<T>
bagFrom(Collection<? extends T> elements)
          Creates a bag containing the elements in the given collection.
static
<T> Bag<T>
bagFrom(Iterable<? extends T> elements)
          Creates a bag containing the elements given by an iterable object.
static
<T> Bag<T>
bagFrom(T[] elements)
          Creates a bag containing the elements in the given array.
static
<T> Bag<T>
bagWith(T newElement, T... restOfNewElements)
          Creates a bag containing the given elements.
<R> Bag<R>
collect(UnaryFunctor<? super E,? extends R> transformer)
          Evaluates the given transformer for each element of this collection, with the element as the parameter, and answers a new collection containing the results of these evaluations.

The elements are traversed in the order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection.

static
<T> Bag<T>
emptyBag()
          Creates an empty bag.
 Bag<E> reject(UnaryCondition<? super E> discriminator)
          Answers a new collection which contains only the elements in this collection which cause the given discriminator to answer false.

For each element of this collection, discriminator is evaluated with the element as the parameter. Each element which causes discriminator to answer false is included in the new collection.

The elements are traversed in the order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection. If both this collection and the result maintain an ordering of their elements, the elements of the result will be in the same relative order as the elements of this collection.

 Bag<E> select(UnaryCondition<? super E> discriminator)
          Answers a new collection which contains only the elements in this collection which cause the given discriminator to answer true.

For each element of this collection, discriminator is evaluated with the element as the parameter. Each element which causes discriminator to answer true is included in the new collection.

The elements are traversed in order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection. If both this collection and the result maintain an ordering of their elements, the elements of the result will be in the same relative order as the elements of this collection.

 Bag<E> toBag()
          Answers a bag with the same elements as this collection.

Answers self.

 
Methods inherited from class jaggregate.AbstractBag
add, add, equals, forEachDo, hashCode, includes, occurrencesOf, rehash, remove, size, toString
 
Methods inherited from class jaggregate.AbstractExtensibleCollection
addAll, addAll, addAll, addAll, removeAll, removeAll, removeAll, removeAll, removeIf, retainAll, retainAll, retainAll, retainAll, retainIf
 
Methods inherited from class jaggregate.AbstractCollection
allSatisfy, anySatisfy, detect, inject, isEmpty, toArray, toArray, toIdentityBag, toIdentitySet, toOrderedCollection, toSet, toSortedCollection, toSortedCollection
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface jaggregate.Collection
allSatisfy, anySatisfy, detect, inject, isEmpty, toArray, toArray, toIdentityBag, toIdentitySet, toOrderedCollection, toSet, toSortedCollection, toSortedCollection
 

Constructor Detail

Bag

public Bag()
Creates an empty bag.


Bag

public Bag(Collection<? extends E> elements)
Creates a bag containing the elements in the given collection.

Parameters:
elements - elements to add to the new bag
Throws:
NullPointerException - if elements is null

Bag

public Bag(E... elements)
Creates a bag containing the elements in the given array.

Parameters:
elements - elements to add to the new bag
Throws:
NullPointerException - if elements is null

Bag

public Bag(Iterable<? extends E> elements)
Creates a bag containing the elements given by an iterable object.

Parameters:
elements - elements to add to the new bag
Throws:
NullPointerException - if elements is null
Method Detail

emptyBag

public static <T> Bag<T> emptyBag()
Creates an empty bag.

Type Parameters:
T - the type of elements allowed in the new bag
Returns:
a new empty bag

bagFrom

public static <T> Bag<T> bagFrom(Collection<? extends T> elements)
Creates a bag containing the elements in the given collection.

Type Parameters:
T - the type of elements allowed in the new bag
Parameters:
elements - elements to add to the new bag
Returns:
the new bag
Throws:
NullPointerException - if elements is null

bagFrom

public static <T> Bag<T> bagFrom(T[] elements)
Creates a bag containing the elements in the given array.

Type Parameters:
T - the type of elements allowed in the new bag
Parameters:
elements - elements to add to the new bag
Returns:
the new bag
Throws:
NullPointerException - if elements is null

bagWith

public static <T> Bag<T> bagWith(T newElement,
                                 T... restOfNewElements)
Creates a bag containing the given elements.

Type Parameters:
T - the type of elements allowed in the new bag
Parameters:
newElement - first new element to add
restOfNewElements - remainder of the elements to add
Returns:
the new bag
Throws:
NullPointerException - if restOfNewElements is null
IllegalArgumentException - if any of the new elements is found to violate restrictions on the characteristics of valid elements
See Also:
AbstractExtensibleCollection.addAll(Collection)

bagFrom

public static <T> Bag<T> bagFrom(Iterable<? extends T> elements)
Creates a bag containing the elements given by an iterable object.

Type Parameters:
T - the type of elements allowed in the new bag
Parameters:
elements - elements to add to the new bag
Returns:
the new bag
Throws:
NullPointerException - if elements is null

toBag

public Bag<E> toBag()
Answers a bag with the same elements as this collection.

Answers self.

Specified by:
toBag in interface Collection<E>
Overrides:
toBag in class AbstractCollection<E>
Returns:
a bag of this collection's elements

collect

public <R> Bag<R> collect(UnaryFunctor<? super E,? extends R> transformer)
Evaluates the given transformer for each element of this collection, with the element as the parameter, and answers a new collection containing the results of these evaluations.

The elements are traversed in the order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection.

Specified by:
collect in interface Collection<E>
Specified by:
collect in interface ExtensibleCollection<E>
Overrides:
collect in class AbstractBag<E>
Type Parameters:
R - return type of the transformer
Parameters:
transformer - the transformer to evaluate
Returns:
a bag of results

reject

public Bag<E> reject(UnaryCondition<? super E> discriminator)
Answers a new collection which contains only the elements in this collection which cause the given discriminator to answer false.

For each element of this collection, discriminator is evaluated with the element as the parameter. Each element which causes discriminator to answer false is included in the new collection.

The elements are traversed in the order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection. If both this collection and the result maintain an ordering of their elements, the elements of the result will be in the same relative order as the elements of this collection.

Specified by:
reject in interface Collection<E>
Specified by:
reject in interface ExtensibleCollection<E>
Overrides:
reject in class AbstractBag<E>
Parameters:
discriminator - the discriminator to evaluate
Returns:
a bag of rejects

select

public Bag<E> select(UnaryCondition<? super E> discriminator)
Answers a new collection which contains only the elements in this collection which cause the given discriminator to answer true.

For each element of this collection, discriminator is evaluated with the element as the parameter. Each element which causes discriminator to answer true is included in the new collection.

The elements are traversed in order specified by forEachDo for this collection.

Unless specifically refined, this message is defined to answer an object conforming to the same protocol as this collection. If both this collection and the result maintain an ordering of their elements, the elements of the result will be in the same relative order as the elements of this collection.

Specified by:
select in interface Collection<E>
Specified by:
select in interface ExtensibleCollection<E>
Overrides:
select in class AbstractBag<E>
Parameters:
discriminator - the discriminator to evaluate
Returns:
a bag of selections


© Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. Licensed under the Academic Free License version 3.0. pholser@alumni.rice.edu