001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java $
003: * $Revision: 581981 $
004: * $Date: 2007-10-04 20:26:26 +0200 (Thu, 04 Oct 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import java.util.NoSuchElementException;
035:
036: import org.apache.http.Header;
037: import org.apache.http.HeaderIterator;
038:
039: /**
040: * Basic implementation of a {@link HeaderIterator}.
041: *
042: * @version $Revision: 581981 $
043: */
044: public class BasicHeaderIterator implements HeaderIterator {
045:
046: /**
047: * An array of headers to iterate over.
048: * Not all elements of this array are necessarily part of the iteration.
049: * This array will never be modified by the iterator.
050: * Derived implementations are expected to adhere to this restriction.
051: */
052: protected final Header[] allHeaders;
053:
054: /**
055: * The position of the next header in {@link #allHeaders allHeaders}.
056: * Negative if the iteration is over.
057: */
058: protected int currentIndex;
059:
060: /**
061: * The header name to filter by.
062: * <code>null</code> to iterate over all headers in the array.
063: */
064: protected String headerName;
065:
066: /**
067: * Creates a new header iterator.
068: *
069: * @param headers an array of headers over which to iterate
070: * @param name the name of the headers over which to iterate, or
071: * <code>null</code> for any
072: */
073: public BasicHeaderIterator(Header[] headers, String name) {
074: if (headers == null) {
075: throw new IllegalArgumentException(
076: "Header array must not be null.");
077: }
078:
079: this .allHeaders = headers;
080: this .headerName = name;
081: this .currentIndex = findNext(-1);
082: }
083:
084: /**
085: * Determines the index of the next header.
086: *
087: * @param from one less than the index to consider first,
088: * -1 to search for the first header
089: *
090: * @return the index of the next header that matches the filter name,
091: * or negative if there are no more headers
092: */
093: protected int findNext(int from) {
094: if (from < -1)
095: return -1;
096:
097: final int to = this .allHeaders.length - 1;
098: boolean found = false;
099: while (!found && (from < to)) {
100: from++;
101: found = filterHeader(from);
102: }
103: return found ? from : -1;
104: }
105:
106: /**
107: * Checks whether a header is part of the iteration.
108: *
109: * @param index the index of the header to check
110: *
111: * @return <code>true</code> if the header should be part of the
112: * iteration, <code>false</code> to skip
113: */
114: protected boolean filterHeader(int index) {
115: return (this .headerName == null)
116: || this .headerName
117: .equalsIgnoreCase(this .allHeaders[index]
118: .getName());
119: }
120:
121: // non-javadoc, see interface HeaderIterator
122: public boolean hasNext() {
123: return (this .currentIndex >= 0);
124: }
125:
126: /**
127: * Obtains the next header from this iteration.
128: *
129: * @return the next header in this iteration
130: *
131: * @throws NoSuchElementException if there are no more headers
132: */
133: public Header nextHeader() throws NoSuchElementException {
134:
135: final int current = this .currentIndex;
136: if (current < 0) {
137: throw new NoSuchElementException(
138: "Iteration already finished.");
139: }
140:
141: this .currentIndex = findNext(current);
142:
143: return this .allHeaders[current];
144: }
145:
146: /**
147: * Returns the next header.
148: * Same as {@link #nextHeader nextHeader}, but not type-safe.
149: *
150: * @return the next header in this iteration
151: *
152: * @throws NoSuchElementException if there are no more headers
153: */
154: public final Object next() throws NoSuchElementException {
155: return nextHeader();
156: }
157:
158: /**
159: * Removing headers is not supported.
160: *
161: * @throws UnsupportedOperationException always
162: */
163: public void remove() throws UnsupportedOperationException {
164:
165: throw new UnsupportedOperationException(
166: "Removing headers is not supported.");
167: }
168: }
|