FileDocCategorySizeDatePackage
BasicListHeaderIterator.javaAPI DocAndroid 1.5 API5763Wed May 06 22:41:10 BST 2009org.apache.http.message

BasicListHeaderIterator

public class BasicListHeaderIterator extends Object implements HeaderIterator
Implementation of a {@link HeaderIterator} based on a {@link List}. For use by {@link HeaderGroup}.
version
$Revision: 584542 $

Fields Summary
protected final List
allHeaders
A list of headers to iterate over. Not all elements of this array are necessarily part of the iteration.
protected int
currentIndex
The position of the next header in {@link #allHeaders allHeaders}. Negative if the iteration is over.
protected int
lastIndex
The position of the last returned header. Negative if none has been returned so far.
protected String
headerName
The header name to filter by. null to iterate over all headers in the array.
Constructors Summary
public BasicListHeaderIterator(List headers, String name)
Creates a new header iterator.

param
headers a list of headers over which to iterate
param
name the name of the headers over which to iterate, or null for any

        if (headers == null) {
            throw new IllegalArgumentException
                ("Header list must not be null.");
        }

        this.allHeaders = headers;
        this.headerName = name;
        this.currentIndex = findNext(-1);
        this.lastIndex = -1;
    
Methods Summary
protected booleanfilterHeader(int index)
Checks whether a header is part of the iteration.

param
index the index of the header to check
return
true if the header should be part of the iteration, false to skip

        if (this.headerName == null)
            return true;

        // non-header elements, including null, will trigger exceptions
        final String name = ((Header)this.allHeaders.get(index)).getName();

        return this.headerName.equalsIgnoreCase(name);
    
protected intfindNext(int from)
Determines the index of the next header.

param
from one less than the index to consider first, -1 to search for the first header
return
the index of the next header that matches the filter name, or negative if there are no more headers

        if (from < -1)
            return -1;

        final int to = this.allHeaders.size()-1;
        boolean found = false;
        while (!found && (from < to)) {
            from++;
            found = filterHeader(from);
        }
        return found ? from : -1;
    
public booleanhasNext()

        return (this.currentIndex >= 0);
    
public final java.lang.Objectnext()
Returns the next header. Same as {@link #nextHeader nextHeader}, but not type-safe.

return
the next header in this iteration
throws
NoSuchElementException if there are no more headers

        return nextHeader();
    
public org.apache.http.HeadernextHeader()
Obtains the next header from this iteration.

return
the next header in this iteration
throws
NoSuchElementException if there are no more headers


        final int current = this.currentIndex;
        if (current < 0) {
            throw new NoSuchElementException("Iteration already finished.");
        }

        this.lastIndex    = current;
        this.currentIndex = findNext(current);

        return (Header) this.allHeaders.get(current);
    
public voidremove()
Removes the header that was returned last.


        if (this.lastIndex < 0) {
            throw new IllegalStateException("No header to remove.");
        }
        this.allHeaders.remove(this.lastIndex);
        this.lastIndex = -1;
        this.currentIndex--; // adjust for the removed element