Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
28 package ch.boye.httpclientandroidlib.message;
30 import java.util.NoSuchElementException;
32 import ch.boye.httpclientandroidlib.Header;
33 import ch.boye.httpclientandroidlib.HeaderIterator;
35 /**
36 * Basic implementation of a {@link HeaderIterator}.
37 *
38 * @since 4.0
39 */
40 public class BasicHeaderIterator implements HeaderIterator {
42 /**
43 * An array of headers to iterate over.
44 * Not all elements of this array are necessarily part of the iteration.
45 * This array will never be modified by the iterator.
46 * Derived implementations are expected to adhere to this restriction.
47 */
48 protected final Header[] allHeaders;
51 /**
52 * The position of the next header in {@link #allHeaders allHeaders}.
53 * Negative if the iteration is over.
54 */
55 protected int currentIndex;
58 /**
59 * The header name to filter by.
60 * <code>null</code> to iterate over all headers in the array.
61 */
62 protected String headerName;
66 /**
67 * Creates a new header iterator.
68 *
69 * @param headers an array of headers over which to iterate
70 * @param name the name of the headers over which to iterate, or
71 * <code>null</code> for any
72 */
73 public BasicHeaderIterator(Header[] headers, String name) {
74 if (headers == null) {
75 throw new IllegalArgumentException
76 ("Header array must not be null.");
77 }
79 this.allHeaders = headers;
80 this.headerName = name;
81 this.currentIndex = findNext(-1);
82 }
85 /**
86 * Determines the index of the next header.
87 *
88 * @param from one less than the index to consider first,
89 * -1 to search for the first header
90 *
91 * @return the index of the next header that matches the filter name,
92 * or negative if there are no more headers
93 */
94 protected int findNext(int from) {
95 if (from < -1)
96 return -1;
98 final int to = this.allHeaders.length-1;
99 boolean found = false;
100 while (!found && (from < to)) {
101 from++;
102 found = filterHeader(from);
103 }
104 return found ? from : -1;
105 }
108 /**
109 * Checks whether a header is part of the iteration.
110 *
111 * @param index the index of the header to check
112 *
113 * @return <code>true</code> if the header should be part of the
114 * iteration, <code>false</code> to skip
115 */
116 protected boolean filterHeader(int index) {
117 return (this.headerName == null) ||
118 this.headerName.equalsIgnoreCase(this.allHeaders[index].getName());
119 }
122 // non-javadoc, see interface HeaderIterator
123 public boolean hasNext() {
124 return (this.currentIndex >= 0);
125 }
128 /**
129 * Obtains the next header from this iteration.
130 *
131 * @return the next header in this iteration
132 *
133 * @throws NoSuchElementException if there are no more headers
134 */
135 public Header nextHeader()
136 throws NoSuchElementException {
138 final int current = this.currentIndex;
139 if (current < 0) {
140 throw new NoSuchElementException("Iteration already finished.");
141 }
143 this.currentIndex = findNext(current);
145 return this.allHeaders[current];
146 }
149 /**
150 * Returns the next header.
151 * Same as {@link #nextHeader nextHeader}, but not type-safe.
152 *
153 * @return the next header in this iteration
154 *
155 * @throws NoSuchElementException if there are no more headers
156 */
157 public final Object next()
158 throws NoSuchElementException {
159 return nextHeader();
160 }
163 /**
164 * Removing headers is not supported.
165 *
166 * @throws UnsupportedOperationException always
167 */
168 public void remove()
169 throws UnsupportedOperationException {
171 throw new UnsupportedOperationException
172 ("Removing headers is not supported.");
173 }
174 }