mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/ClientParamsStack.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

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.impl.client;
michael@0 29
michael@0 30 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 33 import ch.boye.httpclientandroidlib.params.AbstractHttpParams;
michael@0 34
michael@0 35 /**
michael@0 36 * Represents a stack of parameter collections.
michael@0 37 * When retrieving a parameter, the stack is searched in a fixed order
michael@0 38 * and the first match returned. Setting parameters via the stack is
michael@0 39 * not supported. To minimize overhead, the stack has a fixed size and
michael@0 40 * does not maintain an internal array.
michael@0 41 * The supported stack entries, sorted by increasing priority, are:
michael@0 42 * <ol>
michael@0 43 * <li>Application parameters:
michael@0 44 * expected to be the same for all clients used by an application.
michael@0 45 * These provide "global", that is application-wide, defaults.
michael@0 46 * </li>
michael@0 47 * <li>Client parameters:
michael@0 48 * specific to an instance of
michael@0 49 * {@link ch.boye.httpclientandroidlib.client.HttpClient HttpClient}.
michael@0 50 * These provide client specific defaults.
michael@0 51 * </li>
michael@0 52 * <li>Request parameters:
michael@0 53 * specific to a single request execution.
michael@0 54 * For overriding client and global defaults.
michael@0 55 * </li>
michael@0 56 * <li>Override parameters:
michael@0 57 * specific to an instance of
michael@0 58 * {@link ch.boye.httpclientandroidlib.client.HttpClient HttpClient}.
michael@0 59 * These can be used to set parameters that cannot be overridden
michael@0 60 * on a per-request basis.
michael@0 61 * </li>
michael@0 62 * </ol>
michael@0 63 * Each stack entry may be <code>null</code>. That is preferable over
michael@0 64 * an empty params collection, since it avoids searching the empty collection
michael@0 65 * when looking up parameters.
michael@0 66 *
michael@0 67 *
michael@0 68 *
michael@0 69 * @since 4.0
michael@0 70 */
michael@0 71 @NotThreadSafe
michael@0 72 public class ClientParamsStack extends AbstractHttpParams {
michael@0 73
michael@0 74 /** The application parameter collection, or <code>null</code>. */
michael@0 75 protected final HttpParams applicationParams;
michael@0 76
michael@0 77 /** The client parameter collection, or <code>null</code>. */
michael@0 78 protected final HttpParams clientParams;
michael@0 79
michael@0 80 /** The request parameter collection, or <code>null</code>. */
michael@0 81 protected final HttpParams requestParams;
michael@0 82
michael@0 83 /** The override parameter collection, or <code>null</code>. */
michael@0 84 protected final HttpParams overrideParams;
michael@0 85
michael@0 86
michael@0 87 /**
michael@0 88 * Creates a new parameter stack from elements.
michael@0 89 * The arguments will be stored as-is, there is no copying to
michael@0 90 * prevent modification.
michael@0 91 *
michael@0 92 * @param aparams application parameters, or <code>null</code>
michael@0 93 * @param cparams client parameters, or <code>null</code>
michael@0 94 * @param rparams request parameters, or <code>null</code>
michael@0 95 * @param oparams override parameters, or <code>null</code>
michael@0 96 */
michael@0 97 public ClientParamsStack(HttpParams aparams, HttpParams cparams,
michael@0 98 HttpParams rparams, HttpParams oparams) {
michael@0 99 applicationParams = aparams;
michael@0 100 clientParams = cparams;
michael@0 101 requestParams = rparams;
michael@0 102 overrideParams = oparams;
michael@0 103 }
michael@0 104
michael@0 105
michael@0 106 /**
michael@0 107 * Creates a copy of a parameter stack.
michael@0 108 * The new stack will have the exact same entries as the argument stack.
michael@0 109 * There is no copying of parameters.
michael@0 110 *
michael@0 111 * @param stack the stack to copy
michael@0 112 */
michael@0 113 public ClientParamsStack(ClientParamsStack stack) {
michael@0 114 this(stack.getApplicationParams(),
michael@0 115 stack.getClientParams(),
michael@0 116 stack.getRequestParams(),
michael@0 117 stack.getOverrideParams());
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 /**
michael@0 122 * Creates a modified copy of a parameter stack.
michael@0 123 * The new stack will contain the explicitly passed elements.
michael@0 124 * For elements where the explicit argument is <code>null</code>,
michael@0 125 * the corresponding element from the argument stack is used.
michael@0 126 * There is no copying of parameters.
michael@0 127 *
michael@0 128 * @param stack the stack to modify
michael@0 129 * @param aparams application parameters, or <code>null</code>
michael@0 130 * @param cparams client parameters, or <code>null</code>
michael@0 131 * @param rparams request parameters, or <code>null</code>
michael@0 132 * @param oparams override parameters, or <code>null</code>
michael@0 133 */
michael@0 134 public ClientParamsStack(ClientParamsStack stack,
michael@0 135 HttpParams aparams, HttpParams cparams,
michael@0 136 HttpParams rparams, HttpParams oparams) {
michael@0 137 this((aparams != null) ? aparams : stack.getApplicationParams(),
michael@0 138 (cparams != null) ? cparams : stack.getClientParams(),
michael@0 139 (rparams != null) ? rparams : stack.getRequestParams(),
michael@0 140 (oparams != null) ? oparams : stack.getOverrideParams());
michael@0 141 }
michael@0 142
michael@0 143
michael@0 144 /**
michael@0 145 * Obtains the application parameters of this stack.
michael@0 146 *
michael@0 147 * @return the application parameters, or <code>null</code>
michael@0 148 */
michael@0 149 public final HttpParams getApplicationParams() {
michael@0 150 return applicationParams;
michael@0 151 }
michael@0 152
michael@0 153 /**
michael@0 154 * Obtains the client parameters of this stack.
michael@0 155 *
michael@0 156 * @return the client parameters, or <code>null</code>
michael@0 157 */
michael@0 158 public final HttpParams getClientParams() {
michael@0 159 return clientParams;
michael@0 160 }
michael@0 161
michael@0 162 /**
michael@0 163 * Obtains the request parameters of this stack.
michael@0 164 *
michael@0 165 * @return the request parameters, or <code>null</code>
michael@0 166 */
michael@0 167 public final HttpParams getRequestParams() {
michael@0 168 return requestParams;
michael@0 169 }
michael@0 170
michael@0 171 /**
michael@0 172 * Obtains the override parameters of this stack.
michael@0 173 *
michael@0 174 * @return the override parameters, or <code>null</code>
michael@0 175 */
michael@0 176 public final HttpParams getOverrideParams() {
michael@0 177 return overrideParams;
michael@0 178 }
michael@0 179
michael@0 180
michael@0 181 /**
michael@0 182 * Obtains a parameter from this stack.
michael@0 183 * See class comment for search order.
michael@0 184 *
michael@0 185 * @param name the name of the parameter to obtain
michael@0 186 *
michael@0 187 * @return the highest-priority value for that parameter, or
michael@0 188 * <code>null</code> if it is not set anywhere in this stack
michael@0 189 */
michael@0 190 public Object getParameter(String name) {
michael@0 191 if (name == null) {
michael@0 192 throw new IllegalArgumentException
michael@0 193 ("Parameter name must not be null.");
michael@0 194 }
michael@0 195
michael@0 196 Object result = null;
michael@0 197
michael@0 198 if (overrideParams != null) {
michael@0 199 result = overrideParams.getParameter(name);
michael@0 200 }
michael@0 201 if ((result == null) && (requestParams != null)) {
michael@0 202 result = requestParams.getParameter(name);
michael@0 203 }
michael@0 204 if ((result == null) && (clientParams != null)) {
michael@0 205 result = clientParams.getParameter(name);
michael@0 206 }
michael@0 207 if ((result == null) && (applicationParams != null)) {
michael@0 208 result = applicationParams.getParameter(name);
michael@0 209 }
michael@0 210 return result;
michael@0 211 }
michael@0 212
michael@0 213 /**
michael@0 214 * Does <i>not</i> set a parameter.
michael@0 215 * Parameter stacks are read-only. It is possible, though discouraged,
michael@0 216 * to access and modify specific stack entries.
michael@0 217 * Derived classes may change this behavior.
michael@0 218 *
michael@0 219 * @param name ignored
michael@0 220 * @param value ignored
michael@0 221 *
michael@0 222 * @return nothing
michael@0 223 *
michael@0 224 * @throws UnsupportedOperationException always
michael@0 225 */
michael@0 226 public HttpParams setParameter(String name, Object value)
michael@0 227 throws UnsupportedOperationException {
michael@0 228
michael@0 229 throw new UnsupportedOperationException
michael@0 230 ("Setting parameters in a stack is not supported.");
michael@0 231 }
michael@0 232
michael@0 233
michael@0 234 /**
michael@0 235 * Does <i>not</i> remove a parameter.
michael@0 236 * Parameter stacks are read-only. It is possible, though discouraged,
michael@0 237 * to access and modify specific stack entries.
michael@0 238 * Derived classes may change this behavior.
michael@0 239 *
michael@0 240 * @param name ignored
michael@0 241 *
michael@0 242 * @return nothing
michael@0 243 *
michael@0 244 * @throws UnsupportedOperationException always
michael@0 245 */
michael@0 246 public boolean removeParameter(String name) {
michael@0 247 throw new UnsupportedOperationException
michael@0 248 ("Removing parameters in a stack is not supported.");
michael@0 249 }
michael@0 250
michael@0 251
michael@0 252 /**
michael@0 253 * Does <i>not</i> copy parameters.
michael@0 254 * Parameter stacks are lightweight objects, expected to be instantiated
michael@0 255 * as needed and to be used only in a very specific context. On top of
michael@0 256 * that, they are read-only. The typical copy operation to prevent
michael@0 257 * accidental modification of parameters passed by the application to
michael@0 258 * a framework object is therefore pointless and disabled.
michael@0 259 * Create a new stack if you really need a copy.
michael@0 260 * <br/>
michael@0 261 * Derived classes may change this behavior.
michael@0 262 *
michael@0 263 * @return <code>this</code> parameter stack
michael@0 264 */
michael@0 265 public HttpParams copy() {
michael@0 266 return this;
michael@0 267 }
michael@0 268
michael@0 269
michael@0 270 }

mercurial