JML

org.jmlspecs.models
Class JMLObjectBag

java.lang.Object
  extended byorg.jmlspecs.models.JMLObjectBag
All Implemented Interfaces:
Cloneable, JMLCollection, JMLType, Serializable

public class JMLObjectBag
extends Object
implements JMLCollection

Bags (i.e., multisets) of objects. This type uses "==" to compare elements, and does not clone elements that are passed into and returned from the bag's methods.

Version:
$Revision: 1.70 $
Author:
Gary T. Leavens, with help from Albert Baker, Clyde Ruby, and others.
See Also:
JMLCollection, JMLType, JMLValueSet, JMLObjectSetEnumerator, JMLObjectSet, JMLValueSet

Field Summary
static JMLObjectBag EMPTY
          The empty JMLObjectBag.
protected  int size
          The size of this bag.
protected  JMLObjectBagEntryNode the_list
          The list representing the contents of this bag.
 
Constructor Summary
  JMLObjectBag()
          An equational specification of the properties of bags.
  JMLObjectBag(Object elem)
          Initialize this bag to contain the given element.
protected JMLObjectBag(JMLObjectBagEntryNode ls, int sz)
          Initialize this bag with the given representation.
 
Method Summary
 Object choose()
          Return an arbitrary element of this.
 Object clone()
          Return a clone of this object.
 boolean containsAll(Collection c)
          Tell whether, for each element in the given collection, there is a "==" element in this bag.
static JMLObjectBag convertFrom(Object[] a)
          Return the bag containing all the elements in the given array.
static JMLObjectBag convertFrom(Collection c)
          Return the bag containing all the object in the given collection.
static JMLObjectBag convertFrom(JMLCollection c)
          Return the bag containing all the object in the given JMLCollection.
 int count(Object elem)
          Tell how many times the given element occurs "==" to an element in this bag.
 JMLObjectBag difference(JMLObjectBag b2)
          Return a bag containing the items in this bag minus the items in the given bag.
 JMLObjectBagEnumerator elements()
          Returns an Enumeration over this bag.
 boolean equals(Object b2)
          Test whether this object's value is equal to the given argument.
protected  JMLObjectBagEntry getMatchingEntry(Object item)
          Find a JMLObjectBagEntry that is for the same element, if possible.
 boolean has(Object elem)
          Tell whether the given element occurs "==" to an element in this bag.
 int hashCode()
          Return a hash code for this object
 JMLObjectBag insert(Object elem)
          Return a bag containing the given item and the ones in this bag.
 JMLObjectBag insert(Object elem, int cnt)
          Return a bag containing the given item the given number of times, in addition to the ones in this bag.
 int int_size()
          Tell the number of elements in this bag.
 JMLObjectBag intersection(JMLObjectBag b2)
          Return a bag containing the items in both this bag and the given bag.
 boolean isEmpty()
          Tell whether this bag has no elements.
 boolean isProperSubbag(JMLObjectBag b2)
          Tells whether every item in this bag is contained in the argument, but the argument is strictly larger.
 boolean isProperSuperbag(JMLObjectBag b2)
          Tells whether every item in the argument is contained in this bag argument, but this bag is strictly larger.
 boolean isSubbag(JMLObjectBag b2)
          Tells whether every item in this bag is contained in the argument.
 boolean isSuperbag(JMLObjectBag b2)
          Tells whether every item in the argument is contained in this bag.
 JMLIterator iterator()
          Returns an iterator over this bag.
 JMLObjectBag remove(Object elem)
          Return a bag containing the items in this bag except for one of the given element.
 JMLObjectBag remove(Object elem, int cnt)
          Return a bag containing the items in this bag, except for the given number of the given element.
 JMLObjectBag removeAll(Object elem)
          Return a bag containing the items in this bag, except for all items that are "==" to the given item.
static JMLObjectBag singleton(Object e)
          Return the singleton bag containing the given element.
 Object[] toArray()
          Return a new array containing all the elements of this.
 JMLObjectSequence toSequence()
          Return a new JMLObjectSequence containing all the elements of this.
 JMLObjectSet toSet()
          Return a new JMLObjectSet containing all the elements of this.
 String toString()
          Return a string representation of this object.
 JMLObjectBag union(JMLObjectBag b2)
          Return a bag containing the items in either this bag or the given bag.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

the_list

protected final JMLObjectBagEntryNode the_list
The list representing the contents of this bag. Each element of this list is of type JMLObjectBagEntry.


size

protected final int size
The size of this bag.


EMPTY

public static final JMLObjectBag EMPTY
The empty JMLObjectBag.

See Also:
JMLObjectBag()
Constructor Detail

JMLObjectBag

public JMLObjectBag()
An equational specification of the properties of bags. Initialize this bag to be empty.

See Also:
EMPTY

JMLObjectBag

public JMLObjectBag(Object elem)
Initialize this bag to contain the given element.

Parameters:
elem - the element that is the sole contents of this bag.
See Also:
singleton(java.lang.Object)

JMLObjectBag

protected JMLObjectBag(JMLObjectBagEntryNode ls,
                       int sz)
Initialize this bag with the given representation.

Method Detail

singleton

public static JMLObjectBag singleton(Object e)
Return the singleton bag containing the given element.

See Also:
JMLObjectBag(Object)

convertFrom

public static JMLObjectBag convertFrom(Object[] a)
Return the bag containing all the elements in the given array.


convertFrom

public static JMLObjectBag convertFrom(Collection c)
                                throws ClassCastException
Return the bag containing all the object in the given collection.

Throws:
ClassCastException - if some element in c is not an instance of Object.
See Also:
containsAll(java.util.Collection)

convertFrom

public static JMLObjectBag convertFrom(JMLCollection c)
                                throws ClassCastException
Return the bag containing all the object in the given JMLCollection.

Throws:
ClassCastException - if some element in c is not an instance of Object.

count

public int count(Object elem)
Tell how many times the given element occurs "==" to an element in this bag.

Parameters:
elem - the element sought.
Returns:
the number of times elem occurs in this bag.
See Also:
has(Object)

has

public boolean has(Object elem)
Tell whether the given element occurs "==" to an element in this bag.

Specified by:
has in interface JMLCollection
Parameters:
elem - the element sought.
See Also:
count(Object)

containsAll

public boolean containsAll(Collection c)
Tell whether, for each element in the given collection, there is a "==" element in this bag.

Parameters:
c - the collection whose elements are sought.
See Also:
#isSuperbag(JMLObjectSet), convertFrom(java.util.Collection)

equals

public boolean equals(Object b2)
Test whether this object's value is equal to the given argument. This comparison uses "==" to compare elements.

Note that the elementTypes may be different for otherwise equal bags.

Specified by:
equals in interface JMLType
Overrides:
equals in class Object

hashCode

public int hashCode()
Return a hash code for this object

Specified by:
hashCode in interface JMLType
Overrides:
hashCode in class Object

isEmpty

public boolean isEmpty()
Tell whether this bag has no elements.

See Also:
int_size(), has(Object)

int_size

public int int_size()
Tell the number of elements in this bag.

Specified by:
int_size in interface JMLCollection

isSubbag

public boolean isSubbag(JMLObjectBag b2)
Tells whether every item in this bag is contained in the argument.

See Also:
isProperSubbag(JMLObjectBag), isSuperbag(JMLObjectBag)

isProperSubbag

public boolean isProperSubbag(JMLObjectBag b2)
Tells whether every item in this bag is contained in the argument, but the argument is strictly larger.

See Also:
isSubbag(JMLObjectBag), isProperSuperbag(JMLObjectBag)

isSuperbag

public boolean isSuperbag(JMLObjectBag b2)
Tells whether every item in the argument is contained in this bag.

See Also:
isProperSuperbag(JMLObjectBag), isSubbag(JMLObjectBag), containsAll(java.util.Collection)

isProperSuperbag

public boolean isProperSuperbag(JMLObjectBag b2)
Tells whether every item in the argument is contained in this bag argument, but this bag is strictly larger.

See Also:
isSuperbag(JMLObjectBag), isProperSubbag(JMLObjectBag)

choose

public Object choose()
              throws JMLNoSuchElementException
Return an arbitrary element of this.

Throws:
JMLNoSuchElementException - if this is empty.
See Also:
isEmpty(), elements()

clone

public Object clone()
Return a clone of this object. This method does not clone the elements of the bag.

Specified by:
clone in interface JMLType
Overrides:
clone in class Object

getMatchingEntry

protected JMLObjectBagEntry getMatchingEntry(Object item)
Find a JMLObjectBagEntry that is for the same element, if possible.

Parameters:
item - the item sought.
Returns:
null if the item is not in the bag.

insert

public JMLObjectBag insert(Object elem)
                    throws IllegalStateException
Return a bag containing the given item and the ones in this bag.

Throws:
IllegalStateException
See Also:
insert(Object, int), has(Object), remove(Object)

insert

public JMLObjectBag insert(Object elem,
                           int cnt)
                    throws IllegalArgumentException,
                           IllegalStateException
Return a bag containing the given item the given number of times, in addition to the ones in this bag.

Throws:
IllegalArgumentException
IllegalStateException
See Also:
insert(Object), has(Object), remove(Object, int)

remove

public JMLObjectBag remove(Object elem)
Return a bag containing the items in this bag except for one of the given element.

See Also:
remove(Object, int), insert(Object)

remove

public JMLObjectBag remove(Object elem,
                           int cnt)
                    throws IllegalArgumentException
Return a bag containing the items in this bag, except for the given number of the given element.

Throws:
IllegalArgumentException
See Also:
remove(Object), insert(Object, int)

removeAll

public JMLObjectBag removeAll(Object elem)
Return a bag containing the items in this bag, except for all items that are "==" to the given item.

See Also:
remove(Object), remove(Object, int)

intersection

public JMLObjectBag intersection(JMLObjectBag b2)
Return a bag containing the items in both this bag and the given bag. Note that items occur the minimum number of times they occur in both bags.

See Also:
union(JMLObjectBag), difference(JMLObjectBag)

union

public JMLObjectBag union(JMLObjectBag b2)
Return a bag containing the items in either this bag or the given bag. Note that items occur the sum of times they occur in both bags.

See Also:
intersection(JMLObjectBag), difference(JMLObjectBag)

difference

public JMLObjectBag difference(JMLObjectBag b2)
Return a bag containing the items in this bag minus the items in the given bag. If an item occurs in this bag N times, and M times in the given bag, then it occurs N-M times in the result.

See Also:
union(JMLObjectBag), difference(JMLObjectBag)

toSequence

public JMLObjectSequence toSequence()
Return a new JMLObjectSequence containing all the elements of this.

See Also:
toArray(), toSet()

toSet

public JMLObjectSet toSet()
Return a new JMLObjectSet containing all the elements of this.

See Also:
toSequence()

toArray

public Object[] toArray()
Return a new array containing all the elements of this.

See Also:
toSequence()

elements

public JMLObjectBagEnumerator elements()
Returns an Enumeration over this bag.

See Also:
iterator()

iterator

public JMLIterator iterator()
Returns an iterator over this bag.

Specified by:
iterator in interface JMLCollection
See Also:
elements()

toString

public String toString()
Return a string representation of this object.

Overrides:
toString in class Object

JML

JML is Copyright (C) 1998-2002 by Iowa State University and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This release depends on code from the MultiJava project and is based in part on the Kopi project Copyright (C) 1990-99 DMS Decision Management Systems Ges.m.b.H.