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