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