michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.message; michael@0: michael@0: import java.util.NoSuchElementException; michael@0: michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderIterator; michael@0: michael@0: /** michael@0: * Basic implementation of a {@link HeaderIterator}. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class BasicHeaderIterator implements HeaderIterator { michael@0: michael@0: /** michael@0: * An array of headers to iterate over. michael@0: * Not all elements of this array are necessarily part of the iteration. michael@0: * This array will never be modified by the iterator. michael@0: * Derived implementations are expected to adhere to this restriction. michael@0: */ michael@0: protected final Header[] allHeaders; michael@0: michael@0: michael@0: /** michael@0: * The position of the next header in {@link #allHeaders allHeaders}. michael@0: * Negative if the iteration is over. michael@0: */ michael@0: protected int currentIndex; michael@0: michael@0: michael@0: /** michael@0: * The header name to filter by. michael@0: * null to iterate over all headers in the array. michael@0: */ michael@0: protected String headerName; michael@0: michael@0: michael@0: michael@0: /** michael@0: * Creates a new header iterator. michael@0: * michael@0: * @param headers an array of headers over which to iterate michael@0: * @param name the name of the headers over which to iterate, or michael@0: * null for any michael@0: */ michael@0: public BasicHeaderIterator(Header[] headers, String name) { michael@0: if (headers == null) { michael@0: throw new IllegalArgumentException michael@0: ("Header array must not be null."); michael@0: } michael@0: michael@0: this.allHeaders = headers; michael@0: this.headerName = name; michael@0: this.currentIndex = findNext(-1); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Determines the index of the next header. michael@0: * michael@0: * @param from one less than the index to consider first, michael@0: * -1 to search for the first header michael@0: * michael@0: * @return the index of the next header that matches the filter name, michael@0: * or negative if there are no more headers michael@0: */ michael@0: protected int findNext(int from) { michael@0: if (from < -1) michael@0: return -1; michael@0: michael@0: final int to = this.allHeaders.length-1; michael@0: boolean found = false; michael@0: while (!found && (from < to)) { michael@0: from++; michael@0: found = filterHeader(from); michael@0: } michael@0: return found ? from : -1; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Checks whether a header is part of the iteration. michael@0: * michael@0: * @param index the index of the header to check michael@0: * michael@0: * @return true if the header should be part of the michael@0: * iteration, false to skip michael@0: */ michael@0: protected boolean filterHeader(int index) { michael@0: return (this.headerName == null) || michael@0: this.headerName.equalsIgnoreCase(this.allHeaders[index].getName()); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HeaderIterator michael@0: public boolean hasNext() { michael@0: return (this.currentIndex >= 0); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Obtains the next header from this iteration. michael@0: * michael@0: * @return the next header in this iteration michael@0: * michael@0: * @throws NoSuchElementException if there are no more headers michael@0: */ michael@0: public Header nextHeader() michael@0: throws NoSuchElementException { michael@0: michael@0: final int current = this.currentIndex; michael@0: if (current < 0) { michael@0: throw new NoSuchElementException("Iteration already finished."); michael@0: } michael@0: michael@0: this.currentIndex = findNext(current); michael@0: michael@0: return this.allHeaders[current]; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns the next header. michael@0: * Same as {@link #nextHeader nextHeader}, but not type-safe. michael@0: * michael@0: * @return the next header in this iteration michael@0: * michael@0: * @throws NoSuchElementException if there are no more headers michael@0: */ michael@0: public final Object next() michael@0: throws NoSuchElementException { michael@0: return nextHeader(); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Removing headers is not supported. michael@0: * michael@0: * @throws UnsupportedOperationException always michael@0: */ michael@0: public void remove() michael@0: throws UnsupportedOperationException { michael@0: michael@0: throw new UnsupportedOperationException michael@0: ("Removing headers is not supported."); michael@0: } michael@0: }