Source code: org/apache/http/impl/DefaultHttpParams.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/DefaultHttpParams.java $
3 * $Revision: 390703 $
4 * $Date: 2006-04-01 19:35:45 +0200 (Sat, 01 Apr 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.impl;
31
32 import java.io.Serializable;
33 import java.util.HashMap;
34
35 import org.apache.http.params.HttpParams;
36
37 /**
38 * This class represents a collection of HTTP protocol parameters. Protocol parameters
39 * may be linked together to form a hierarchy. If a particular parameter value has not been
40 * explicitly defined in the collection itself, its value will be drawn from the parent
41 * collection of parameters.
42 *
43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44 *
45 * @version $Revision: 390703 $
46 *
47 * @since 3.0
48 */
49 public class DefaultHttpParams implements HttpParams, Serializable {
50
51 static final long serialVersionUID = -8296449161405728403L;
52
53 /** The set of default values to defer to */
54 private HttpParams defaults = null;
55
56 /** Hash map of HTTP parameters that this collection contains */
57 private HashMap parameters = null;
58
59 /**
60 * Creates a new collection of parameters with the given parent.
61 * The collection will defer to its parent for a default value
62 * if a particular parameter is not explicitly set in the collection
63 * itself.
64 *
65 * @param defaults the parent collection to defer to, if a parameter
66 * is not explictly set in the collection itself.
67 */
68 public DefaultHttpParams(final HttpParams defaults) {
69 super();
70 this.defaults = defaults;
71 }
72
73 public DefaultHttpParams() {
74 this(null);
75 }
76
77 public synchronized HttpParams getDefaults() {
78 return this.defaults;
79 }
80
81 public synchronized void setDefaults(final HttpParams params) {
82 this.defaults = params;
83 }
84
85 public synchronized Object getParameter(final String name) {
86 // See if the parameter has been explicitly defined
87 Object param = null;
88 if (this.parameters != null) {
89 param = this.parameters.get(name);
90 }
91 if (param != null) {
92 // If so, return
93 return param;
94 } else {
95 // If not, see if defaults are available
96 if (this.defaults != null) {
97 // Return default parameter value
98 return this.defaults.getParameter(name);
99 } else {
100 // Otherwise, return null
101 return null;
102 }
103 }
104 }
105
106 public synchronized HttpParams setParameter(final String name, final Object value) {
107 if (this.parameters == null) {
108 this.parameters = new HashMap();
109 }
110 this.parameters.put(name, value);
111 return this;
112 }
113
114 /**
115 * Assigns the value to all the parameter with the given names
116 *
117 * @param names array of parameter name
118 * @param value parameter value
119 */
120 public synchronized void setParameters(final String[] names, final Object value) {
121 for (int i = 0; i < names.length; i++) {
122 setParameter(names[i], value);
123 }
124 }
125
126 public long getLongParameter(final String name, long defaultValue) {
127 Object param = getParameter(name);
128 if (param == null) {
129 return defaultValue;
130 }
131 return ((Long)param).longValue();
132 }
133
134 public HttpParams setLongParameter(final String name, long value) {
135 setParameter(name, new Long(value));
136 return this;
137 }
138
139 public int getIntParameter(final String name, int defaultValue) {
140 Object param = getParameter(name);
141 if (param == null) {
142 return defaultValue;
143 }
144 return ((Integer)param).intValue();
145 }
146
147 public HttpParams setIntParameter(final String name, int value) {
148 setParameter(name, new Integer(value));
149 return this;
150 }
151
152 public double getDoubleParameter(final String name, double defaultValue) {
153 Object param = getParameter(name);
154 if (param == null) {
155 return defaultValue;
156 }
157 return ((Double)param).doubleValue();
158 }
159
160 public HttpParams setDoubleParameter(final String name, double value) {
161 setParameter(name, new Double(value));
162 return this;
163 }
164
165 public boolean getBooleanParameter(final String name, boolean defaultValue) {
166 Object param = getParameter(name);
167 if (param == null) {
168 return defaultValue;
169 }
170 return ((Boolean)param).booleanValue();
171 }
172
173 public HttpParams setBooleanParameter(final String name, boolean value) {
174 setParameter(name, value ? Boolean.TRUE : Boolean.FALSE);
175 return this;
176 }
177
178 public boolean isParameterSet(final String name) {
179 return getParameter(name) != null;
180 }
181
182 public boolean isParameterSetLocally(final String name) {
183 return this.parameters != null && this.parameters.get(name) != null;
184 }
185
186 public boolean isParameterTrue(final String name) {
187 return getBooleanParameter(name, false);
188 }
189
190 public boolean isParameterFalse(final String name) {
191 return !getBooleanParameter(name, false);
192 }
193
194 /**
195 * Removes all parameters from this collection.
196 */
197 public void clear() {
198 this.parameters = null;
199 }
200
201 }