|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.params; |
|
29 |
|
30 import java.io.Serializable; |
|
31 import java.util.Map; |
|
32 import java.util.HashMap; |
|
33 import java.util.Iterator; |
|
34 |
|
35 import ch.boye.httpclientandroidlib.params.HttpParams; |
|
36 |
|
37 /** |
|
38 * Default implementation of {@link HttpParams} interface. |
|
39 * <p> |
|
40 * Please note access to the internal structures of this class is not |
|
41 * synchronized and therefore this class may be thread-unsafe. |
|
42 * |
|
43 * @since 4.0 |
|
44 */ |
|
45 public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable { |
|
46 |
|
47 private static final long serialVersionUID = -7086398485908701455L; |
|
48 |
|
49 /** Map of HTTP parameters that this collection contains. */ |
|
50 private final HashMap parameters = new HashMap(); |
|
51 |
|
52 public BasicHttpParams() { |
|
53 super(); |
|
54 } |
|
55 |
|
56 public Object getParameter(final String name) { |
|
57 return this.parameters.get(name); |
|
58 } |
|
59 |
|
60 public HttpParams setParameter(final String name, final Object value) { |
|
61 this.parameters.put(name, value); |
|
62 return this; |
|
63 } |
|
64 |
|
65 public boolean removeParameter(String name) { |
|
66 //this is to avoid the case in which the key has a null value |
|
67 if (this.parameters.containsKey(name)) { |
|
68 this.parameters.remove(name); |
|
69 return true; |
|
70 } else { |
|
71 return false; |
|
72 } |
|
73 } |
|
74 |
|
75 /** |
|
76 * Assigns the value to all the parameter with the given names |
|
77 * |
|
78 * @param names array of parameter names |
|
79 * @param value parameter value |
|
80 */ |
|
81 public void setParameters(final String[] names, final Object value) { |
|
82 for (int i = 0; i < names.length; i++) { |
|
83 setParameter(names[i], value); |
|
84 } |
|
85 } |
|
86 |
|
87 /** |
|
88 * Is the parameter set? |
|
89 * <p> |
|
90 * Uses {@link #getParameter(String)} (which is overrideable) to |
|
91 * fetch the parameter value, if any. |
|
92 * <p> |
|
93 * Also @see {@link #isParameterSetLocally(String)} |
|
94 * |
|
95 * @param name parameter name |
|
96 * @return true if parameter is defined and non-null |
|
97 */ |
|
98 public boolean isParameterSet(final String name) { |
|
99 return getParameter(name) != null; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Is the parameter set in this object? |
|
104 * <p> |
|
105 * The parameter value is fetched directly. |
|
106 * <p> |
|
107 * Also @see {@link #isParameterSet(String)} |
|
108 * |
|
109 * @param name parameter name |
|
110 * @return true if parameter is defined and non-null |
|
111 */ |
|
112 public boolean isParameterSetLocally(final String name) { |
|
113 return this.parameters.get(name) != null; |
|
114 } |
|
115 |
|
116 /** |
|
117 * Removes all parameters from this collection. |
|
118 */ |
|
119 public void clear() { |
|
120 this.parameters.clear(); |
|
121 } |
|
122 |
|
123 /** |
|
124 * Creates a copy of these parameters. |
|
125 * This implementation calls {@link #clone()}. |
|
126 * |
|
127 * @return a new set of params holding a copy of the |
|
128 * <i>local</i> parameters in this object. |
|
129 * |
|
130 * @deprecated |
|
131 * @throws UnsupportedOperationException if the clone() fails |
|
132 */ |
|
133 public HttpParams copy() { |
|
134 try { |
|
135 return (HttpParams) clone(); |
|
136 } catch (CloneNotSupportedException ex) { |
|
137 throw new UnsupportedOperationException("Cloning not supported"); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Clones the instance. |
|
143 * Uses {@link #copyParams(HttpParams)} to copy the parameters. |
|
144 */ |
|
145 public Object clone() throws CloneNotSupportedException { |
|
146 BasicHttpParams clone = (BasicHttpParams) super.clone(); |
|
147 copyParams(clone); |
|
148 return clone; |
|
149 } |
|
150 |
|
151 protected void copyParams(HttpParams target) { |
|
152 Iterator iter = parameters.entrySet().iterator(); |
|
153 while (iter.hasNext()) { |
|
154 Map.Entry me = (Map.Entry) iter.next(); |
|
155 if (me.getKey() instanceof String) |
|
156 target.setParameter((String)me.getKey(), me.getValue()); |
|
157 } |
|
158 } |
|
159 |
|
160 } |