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.params; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.Serializable; |
michael@0 | 31 | import java.util.Map; |
michael@0 | 32 | import java.util.HashMap; |
michael@0 | 33 | import java.util.Iterator; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Default implementation of {@link HttpParams} interface. |
michael@0 | 39 | * <p> |
michael@0 | 40 | * Please note access to the internal structures of this class is not |
michael@0 | 41 | * synchronized and therefore this class may be thread-unsafe. |
michael@0 | 42 | * |
michael@0 | 43 | * @since 4.0 |
michael@0 | 44 | */ |
michael@0 | 45 | public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable { |
michael@0 | 46 | |
michael@0 | 47 | private static final long serialVersionUID = -7086398485908701455L; |
michael@0 | 48 | |
michael@0 | 49 | /** Map of HTTP parameters that this collection contains. */ |
michael@0 | 50 | private final HashMap parameters = new HashMap(); |
michael@0 | 51 | |
michael@0 | 52 | public BasicHttpParams() { |
michael@0 | 53 | super(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | public Object getParameter(final String name) { |
michael@0 | 57 | return this.parameters.get(name); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | public HttpParams setParameter(final String name, final Object value) { |
michael@0 | 61 | this.parameters.put(name, value); |
michael@0 | 62 | return this; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | public boolean removeParameter(String name) { |
michael@0 | 66 | //this is to avoid the case in which the key has a null value |
michael@0 | 67 | if (this.parameters.containsKey(name)) { |
michael@0 | 68 | this.parameters.remove(name); |
michael@0 | 69 | return true; |
michael@0 | 70 | } else { |
michael@0 | 71 | return false; |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * Assigns the value to all the parameter with the given names |
michael@0 | 77 | * |
michael@0 | 78 | * @param names array of parameter names |
michael@0 | 79 | * @param value parameter value |
michael@0 | 80 | */ |
michael@0 | 81 | public void setParameters(final String[] names, final Object value) { |
michael@0 | 82 | for (int i = 0; i < names.length; i++) { |
michael@0 | 83 | setParameter(names[i], value); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * Is the parameter set? |
michael@0 | 89 | * <p> |
michael@0 | 90 | * Uses {@link #getParameter(String)} (which is overrideable) to |
michael@0 | 91 | * fetch the parameter value, if any. |
michael@0 | 92 | * <p> |
michael@0 | 93 | * Also @see {@link #isParameterSetLocally(String)} |
michael@0 | 94 | * |
michael@0 | 95 | * @param name parameter name |
michael@0 | 96 | * @return true if parameter is defined and non-null |
michael@0 | 97 | */ |
michael@0 | 98 | public boolean isParameterSet(final String name) { |
michael@0 | 99 | return getParameter(name) != null; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Is the parameter set in this object? |
michael@0 | 104 | * <p> |
michael@0 | 105 | * The parameter value is fetched directly. |
michael@0 | 106 | * <p> |
michael@0 | 107 | * Also @see {@link #isParameterSet(String)} |
michael@0 | 108 | * |
michael@0 | 109 | * @param name parameter name |
michael@0 | 110 | * @return true if parameter is defined and non-null |
michael@0 | 111 | */ |
michael@0 | 112 | public boolean isParameterSetLocally(final String name) { |
michael@0 | 113 | return this.parameters.get(name) != null; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Removes all parameters from this collection. |
michael@0 | 118 | */ |
michael@0 | 119 | public void clear() { |
michael@0 | 120 | this.parameters.clear(); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * Creates a copy of these parameters. |
michael@0 | 125 | * This implementation calls {@link #clone()}. |
michael@0 | 126 | * |
michael@0 | 127 | * @return a new set of params holding a copy of the |
michael@0 | 128 | * <i>local</i> parameters in this object. |
michael@0 | 129 | * |
michael@0 | 130 | * @deprecated |
michael@0 | 131 | * @throws UnsupportedOperationException if the clone() fails |
michael@0 | 132 | */ |
michael@0 | 133 | public HttpParams copy() { |
michael@0 | 134 | try { |
michael@0 | 135 | return (HttpParams) clone(); |
michael@0 | 136 | } catch (CloneNotSupportedException ex) { |
michael@0 | 137 | throw new UnsupportedOperationException("Cloning not supported"); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * Clones the instance. |
michael@0 | 143 | * Uses {@link #copyParams(HttpParams)} to copy the parameters. |
michael@0 | 144 | */ |
michael@0 | 145 | public Object clone() throws CloneNotSupportedException { |
michael@0 | 146 | BasicHttpParams clone = (BasicHttpParams) super.clone(); |
michael@0 | 147 | copyParams(clone); |
michael@0 | 148 | return clone; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | protected void copyParams(HttpParams target) { |
michael@0 | 152 | Iterator iter = parameters.entrySet().iterator(); |
michael@0 | 153 | while (iter.hasNext()) { |
michael@0 | 154 | Map.Entry me = (Map.Entry) iter.next(); |
michael@0 | 155 | if (me.getKey() instanceof String) |
michael@0 | 156 | target.setParameter((String)me.getKey(), me.getValue()); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | } |