15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java $
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * $Revision: 581981 $
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * $Date: 2007-10-04 11:26:26 -0700 (Thu, 04 Oct 2007) $
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ====================================================================
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed to the Apache Software Foundation (ASF) under one
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * or more contributor license agreements.  See the NOTICE file
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed with this work for additional information
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * regarding copyright ownership.  The ASF licenses this file
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * to you under the Apache License, Version 2.0 (the
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * "License"); you may not use this file except in compliance
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * with the License.  You may obtain a copy of the License at
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   http://www.apache.org/licenses/LICENSE-2.0
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * software distributed under the License is distributed on an
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * KIND, either express or implied.  See the License for the
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * specific language governing permissions and limitations
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * under the License.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ====================================================================
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This software consists of voluntary contributions made by many
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * individuals on behalf of the Apache Software Foundation.  For more
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * information on the Apache Software Foundation, please see
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * <http://www.apache.org/>.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)package org.apache.http.message;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import java.util.NoSuchElementException;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.apache.http.Header;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.apache.http.HeaderIterator;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Basic implementation of a {@link HeaderIterator}.
43 *
44 * @version $Revision: 581981 $
45 *
46 * @deprecated Please use {@link java.net.URL#openConnection} instead.
47 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
48 *     for further details.
49 */
50@Deprecated
51public class BasicHeaderIterator implements HeaderIterator {
52
53    /**
54     * An array of headers to iterate over.
55     * Not all elements of this array are necessarily part of the iteration.
56     * This array will never be modified by the iterator.
57     * Derived implementations are expected to adhere to this restriction.
58     */
59    protected final Header[] allHeaders;
60
61
62    /**
63     * The position of the next header in {@link #allHeaders allHeaders}.
64     * Negative if the iteration is over.
65     */
66    protected int currentIndex;
67
68
69    /**
70     * The header name to filter by.
71     * <code>null</code> to iterate over all headers in the array.
72     */
73    protected String headerName;
74
75
76
77    /**
78     * Creates a new header iterator.
79     *
80     * @param headers   an array of headers over which to iterate
81     * @param name      the name of the headers over which to iterate, or
82     *                  <code>null</code> for any
83     */
84    public BasicHeaderIterator(Header[] headers, String name) {
85        if (headers == null) {
86            throw new IllegalArgumentException
87                ("Header array must not be null.");
88        }
89
90        this.allHeaders = headers;
91        this.headerName = name;
92        this.currentIndex = findNext(-1);
93    }
94
95
96    /**
97     * Determines the index of the next header.
98     *
99     * @param from      one less than the index to consider first,
100     *                  -1 to search for the first header
101     *
102     * @return  the index of the next header that matches the filter name,
103     *          or negative if there are no more headers
104     */
105    protected int findNext(int from) {
106        if (from < -1)
107            return -1;
108
109        final int to = this.allHeaders.length-1;
110        boolean found = false;
111        while (!found && (from < to)) {
112            from++;
113            found = filterHeader(from);
114        }
115        return found ? from : -1;
116    }
117
118
119    /**
120     * Checks whether a header is part of the iteration.
121     *
122     * @param index     the index of the header to check
123     *
124     * @return  <code>true</code> if the header should be part of the
125     *          iteration, <code>false</code> to skip
126     */
127    protected boolean filterHeader(int index) {
128        return (this.headerName == null) ||
129            this.headerName.equalsIgnoreCase(this.allHeaders[index].getName());
130    }
131
132
133    // non-javadoc, see interface HeaderIterator
134    public boolean hasNext() {
135        return (this.currentIndex >= 0);
136    }
137
138
139    /**
140     * Obtains the next header from this iteration.
141     *
142     * @return  the next header in this iteration
143     *
144     * @throws NoSuchElementException   if there are no more headers
145     */
146    public Header nextHeader()
147        throws NoSuchElementException {
148
149        final int current = this.currentIndex;
150        if (current < 0) {
151            throw new NoSuchElementException("Iteration already finished.");
152        }
153
154        this.currentIndex = findNext(current);
155
156        return this.allHeaders[current];
157    }
158
159
160    /**
161     * Returns the next header.
162     * Same as {@link #nextHeader nextHeader}, but not type-safe.
163     *
164     * @return  the next header in this iteration
165     *
166     * @throws NoSuchElementException   if there are no more headers
167     */
168    public final Object next()
169        throws NoSuchElementException {
170        return nextHeader();
171    }
172
173
174    /**
175     * Removing headers is not supported.
176     *
177     * @throws UnsupportedOperationException    always
178     */
179    public void remove()
180        throws UnsupportedOperationException {
181
182        throw new UnsupportedOperationException
183            ("Removing headers is not supported.");
184    }
185}
186