Software Engineering

Compound iterator implementation

 German version  up

The compound iterator follows the bridge design pattern. It uses a behavior that contains the base iterator, the subiterator, and the function composeItems. Furthermore, the behavior contains a factory method for creating the so-specified compound iterator.

Abstract class CompoundIteratorBehavior

Any behavior of a compound iterator extends the abstract class CompoundIteratorBehavior that requires methods baseIterator, subIterator and composeItems. Furthermore, the class CompoundIteratorBehavior contains the factory method iterator that creates the so-specified compound iterator.

public abstract class CompoundIteratorBehavior
{
    /** the base iterator
     */
    public abstract Iterator baseIterator();

    /** the subiterator
     *  o0 is the cursor object of the base iterator
     */ 
    public abstract Iterator subIterator(Object o0);

    /** specifies the object composed of the two
     *  cursor objects o0 and o1
     */ 
    public abstract Object composeItems(Object o0, Object o1);

    /** factory method that creates the compound iterator
     */
    public Iterator iterator()
    {
        return new CompoundIterator(this);
    }

}

Class CompoundIterator

In the following, the implementation of the compound iterator is given.

public class CompoundIterator implements Iterator
{
    private CompoundIteratorBehavior cb;
    private boolean hasnext;
    private Iterator i0, i1;
    private Object o0, o1;

    public CompoundIterator(CompoundIteratorBehavior cb_)
    {
        cb=cb_;
        hasnext=true;
        i0=cb.baseIterator();
        o1=tryNext0();
    }

    public boolean hasNext()
    {
        return hasnext;
    }

    public Object next()
    {
        Object o=o1;
        o1=tryNext1();
        return o;
    }

    private Object tryNext0()
    {
        while (i0.hasNext())
        {
            o0=i0.next();
            if (o0!=null)
            {
                i1=cb.subIterator(o0);
                return tryNext1();
            }
        }
        hasnext=false;
        return null;
    }

    private Object tryNext1()
    {
        if (i1.hasNext())
            return cb.composeItems(o0, i1.next());
        else
            return tryNext0();
    }

    public void remove()
    {
        // not implemented
    }

}

 

The classes CompoundIterator and CompoundIteratorBehavior are included in the Java archive de.fh-flensburg.inf.lang.iterators-untyped.jar.

 

Next:  [Usage of the compound iterator]   or   up

 

homeH.W. Lang   Hochschule Flensburg   lang@hs-flensburg.de   Impressum   ©   Created: 05.02.2008   Updated: 02.03.2017
Valid HTML 4.01 Transitional

Hochschule Flensburg
Campus Flensburg

Informatik in Flensburg studieren...

 

Neu gestaltetes Studienangebot:

Bachelor-Studiengang
Angewandte Informatik

mit Schwerpunkten auf den Themen Software, Web, Mobile, Security und Usability.

Ihr Abschluss
nach 7 Semestern:
Bachelor of Science

 

Ebenfalls ganz neu:

Master-Studiengang
Angewandte Informatik

Ein projektorientiertes Studium auf höchstem Niveau mit den Schwerpunkten Internet-Sicherheit, Mobile Computing und Human-Computer Interaction.

Ihr Abschluss
nach 3 Semestern:
Master of Science

 

Weitere Informatik-Studienangebote an der Hochschule Flensburg:

Medieninformatik

Wirtschaftsinformatik