Home | History | Annotate | Download | only in message
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicListHeaderIterator.java $
      3  * $Revision: 584542 $
      4  * $Date: 2007-10-14 06:29:34 -0700 (Sun, 14 Oct 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.message;
     33 
     34 
     35 import java.util.List;
     36 import java.util.NoSuchElementException;
     37 
     38 import org.apache.http.Header;
     39 import org.apache.http.HeaderIterator;
     40 
     41 
     42 /**
     43  * Implementation of a {@link HeaderIterator} based on a {@link List}.
     44  * For use by {@link HeaderGroup}.
     45  *
     46  * @version $Revision: 584542 $
     47  *
     48  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     49  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     50  *     for further details.
     51  */
     52 @Deprecated
     53 public class BasicListHeaderIterator implements HeaderIterator {
     54 
     55     /**
     56      * A list of headers to iterate over.
     57      * Not all elements of this array are necessarily part of the iteration.
     58      */
     59     protected final List 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 position of the last returned header.
     71      * Negative if none has been returned so far.
     72      */
     73     protected int lastIndex;
     74 
     75 
     76     /**
     77      * The header name to filter by.
     78      * <code>null</code> to iterate over all headers in the array.
     79      */
     80     protected String headerName;
     81 
     82 
     83 
     84     /**
     85      * Creates a new header iterator.
     86      *
     87      * @param headers   a list of headers over which to iterate
     88      * @param name      the name of the headers over which to iterate, or
     89      *                  <code>null</code> for any
     90      */
     91     public BasicListHeaderIterator(List headers, String name) {
     92         if (headers == null) {
     93             throw new IllegalArgumentException
     94                 ("Header list must not be null.");
     95         }
     96 
     97         this.allHeaders = headers;
     98         this.headerName = name;
     99         this.currentIndex = findNext(-1);
    100         this.lastIndex = -1;
    101     }
    102 
    103 
    104     /**
    105      * Determines the index of the next header.
    106      *
    107      * @param from      one less than the index to consider first,
    108      *                  -1 to search for the first header
    109      *
    110      * @return  the index of the next header that matches the filter name,
    111      *          or negative if there are no more headers
    112      */
    113     protected int findNext(int from) {
    114         if (from < -1)
    115             return -1;
    116 
    117         final int to = this.allHeaders.size()-1;
    118         boolean found = false;
    119         while (!found && (from < to)) {
    120             from++;
    121             found = filterHeader(from);
    122         }
    123         return found ? from : -1;
    124     }
    125 
    126 
    127     /**
    128      * Checks whether a header is part of the iteration.
    129      *
    130      * @param index     the index of the header to check
    131      *
    132      * @return  <code>true</code> if the header should be part of the
    133      *          iteration, <code>false</code> to skip
    134      */
    135     protected boolean filterHeader(int index) {
    136         if (this.headerName == null)
    137             return true;
    138 
    139         // non-header elements, including null, will trigger exceptions
    140         final String name = ((Header)this.allHeaders.get(index)).getName();
    141 
    142         return this.headerName.equalsIgnoreCase(name);
    143     }
    144 
    145 
    146     // non-javadoc, see interface HeaderIterator
    147     public boolean hasNext() {
    148         return (this.currentIndex >= 0);
    149     }
    150 
    151 
    152     /**
    153      * Obtains the next header from this iteration.
    154      *
    155      * @return  the next header in this iteration
    156      *
    157      * @throws NoSuchElementException   if there are no more headers
    158      */
    159     public Header nextHeader()
    160         throws NoSuchElementException {
    161 
    162         final int current = this.currentIndex;
    163         if (current < 0) {
    164             throw new NoSuchElementException("Iteration already finished.");
    165         }
    166 
    167         this.lastIndex    = current;
    168         this.currentIndex = findNext(current);
    169 
    170         return (Header) this.allHeaders.get(current);
    171     }
    172 
    173 
    174     /**
    175      * Returns the next header.
    176      * Same as {@link #nextHeader nextHeader}, but not type-safe.
    177      *
    178      * @return  the next header in this iteration
    179      *
    180      * @throws NoSuchElementException   if there are no more headers
    181      */
    182     public final Object next()
    183         throws NoSuchElementException {
    184         return nextHeader();
    185     }
    186 
    187 
    188     /**
    189      * Removes the header that was returned last.
    190      */
    191     public void remove()
    192         throws UnsupportedOperationException {
    193 
    194         if (this.lastIndex < 0) {
    195             throw new IllegalStateException("No header to remove.");
    196         }
    197         this.allHeaders.remove(this.lastIndex);
    198         this.lastIndex = -1;
    199         this.currentIndex--; // adjust for the removed element
    200     }
    201 }
    202