Professional Documents
Culture Documents
Lecture 4(a):
Iteration abstraction
2013
Outline
Why iteration abstraction?
Generator
Iteration in Java
Specify an iteration
Implement an iteration
Using iteration
List's iteration
2013
2013
return sum
2013
2013
no bounds on size
2013
specified as an interface
FIT330 Software Engineering
Generator
Auto-populated collections:
2013
Pre-populated generator
IntSet
Vector
(els)
x1
P
2013
Generator
x2
...
xn
Auto-populated: PrimeSet
/**
* @overview
*
PrimeSet is an immutable
*
sub-type of IntSet, which contains a natural
*
sequence of prime numbers up to a given value.
*/
Iteration in Java
Generator is specified by interface
java.util.Iterator
Iterator defines 3 methods:
hasNext: ask
next: get
(optional) remove
2013
10
IntSet
elements()
Vector
(els)
x1
P
2013
Generator
x2
...
xn
11
Multiple iterators
IntSet
elements()
GenA
P
GenB
Vector
(els)
x1
x2
...
xn
reverse
Elements()
2013
12
java.util.Iterator
<<interface>
Iterator
+ hasNext(): boolean
+ next(): Object
+ remove()
2013
13
Interface: Iterator
java.util.Iterator
Note:
2013
14
hasNext()
/**
* @effects
*
if there are more elements to yield
*
return true
*
else
*
return false
*
*/
public boolean hasNext();
2013
15
next()
/**
* @modifies this
* @effects
*
if there are more results to yield
*
returns the next result
*
and modifies the state of
*
this to record the yield.
*
else
*
throws NoSuchElementException
*/
public Object next() throws NoSuchElementException;
2013
16
remove()
/**
* @effects
*
remove from the underlying collection the last
*
element returned by the call to next;
*
throw
*
UnsupportedOperationException if remove is not
*
supported,
*
IllegalStateException if next has not yet been
*
called, or remove has already been called after
*
the last call to next.
*
*
The behavior is unspecified if the underlying
*
collection is modified while the iteration is in
*
progress in any way other than by calling this
*
method.
*/
public void remove() throws UnsupportedOperationException,
IllegalStateException;
2013
17
2013
Collection
Set
Queue
18
Specify an iteration
Common iterator method names: elements,
iterators
Typically contains @effects and @requires
@effects:
19
Specify a generator
A private inner class of the collection class
Implements java.util.Iterator
The rep keeps track of the iteration state
Rep invariant refers to attributes of the
enclosing class
AF: maps rep to a sequence of elements
Operations:
2013
no repOK
20
Example: IntSet.elements
/**
* @effects
<pre>
*
if this is empty
*
throw EmptyException
*
else
*
return a generator that will produce all
*
the elements of this (as Integers), each
*
exactly once, in arbitrary order.</pre>
*
* @requires <tt>this</tt> must not be modified
*
while the generator is in use
*/
21
Generator: IntSet.IntSetGen
/**
* @overview
*
IntSet.IntSetGen represents a generator of the
*
elements of an IntSet.
* @attributes
* ind Integer
* @abstract_properties
* mutable(ind)=false /\ min(ind)=0 /\
* ind<IntSet.size()
* @abstraction_function
*
AF(c) = [x1,...] where each xi is in
*
c.IntSet.elements and are arranged in same
*
(arbitrary) order as c.IntSet.elements.
* @rep_invariant
*
ind >= 0 /\ ind < IntSet.size()
*/
2013
22
...
private class IntSetGen implements Iterator {
@DomainConstraint(type="Integer",mutable=false,
min=0)
private int ind;
}
2013
23
Implements an iteration
hasNext:
next:
2013
24
Example: IntSet
ch6.IntSet
Note:
IntSetGen:
2013
25
IntSet.elements
public Iterator elements() throws EmptyException {
if (size() == 0)
throw new EmptyException("IntSet.elements");
return new IntSetGen();
}
2013
26
IntSet.IntSetGen
private class IntSetGen implements Iterator {
private int ind; // next index
// constructor method
public IntSetGen() {
ind = 0;
}
public boolean hasNext() {
return (ind < size());
}
2013
27
IntSet.IntSetGen
public Object next() throws NoSuchElementException {
if (hasNext()) {
Object next = elements.get(ind);
ind++;
return next;
}
throw new
NoSuchElementException("IntSet.elements");
}
public void remove() {
// do nothing
}
}
2013
28
Vector
(els)
p1
P
Generator
max
29
PrimeSet
E xercise
ch6.PrimeSet
Note:
PrimeGen:
2013
30
Using iteration
2013
hasNext or
31
Example
ch6.usage.IteratorUsage
Note:
2013
32
2013
33
E xercise
2013
34
List's iteration
If your data type is a List, then simply
invokes its iterator method:
iterator(): Iterator
Example: ch7.lists.ListExample
2013
35
Summary
Iteration abstraction provides a convenient and
efficient generic method to iterate the elements
of a collection
Iteration abstraction is defined by a
combination of an iterator method and a
generator
iterator method creates a new generator
Generator implements
java.util.Iterator and is specified as an
inner class
Java's List classes implement iterators
2013
36
Questions?
2013
37