001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/params/BasicHttpParams.java $
003: * $Revision: 610464 $
004: * $Date: 2008-01-09 18:10:55 +0100 (Wed, 09 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.params;
033:
034: import java.io.Serializable;
035: import java.util.Map;
036: import java.util.HashMap;
037: import java.util.Iterator;
038:
039: import org.apache.http.params.HttpParams;
040:
041: /**
042: * This class represents a collection of HTTP protocol parameters.
043: * Protocol parameters may be linked together to form a hierarchy.
044: * If a particular parameter value has not been explicitly defined
045: * in the collection itself, its value will be drawn from the parent
046: * collection of parameters.
047: *
048: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
049: *
050: * @version $Revision: 610464 $
051: */
052: public final class BasicHttpParams extends AbstractHttpParams implements
053: Serializable, Cloneable {
054:
055: private static final long serialVersionUID = -7086398485908701455L;
056:
057: /** Map of HTTP parameters that this collection contains. */
058: private HashMap parameters;
059:
060: public BasicHttpParams() {
061: super ();
062: }
063:
064: public Object getParameter(final String name) {
065: // See if the parameter has been explicitly defined
066: Object param = null;
067: if (this .parameters != null) {
068: param = this .parameters.get(name);
069: }
070: return param;
071: }
072:
073: public HttpParams setParameter(final String name, final Object value) {
074: if (this .parameters == null) {
075: this .parameters = new HashMap();
076: }
077: this .parameters.put(name, value);
078: return this ;
079: }
080:
081: public boolean removeParameter(String name) {
082: if (this .parameters == null) {
083: return false;
084: }
085: //this is to avoid the case in which the key has a null value
086: if (this .parameters.containsKey(name)) {
087: this .parameters.remove(name);
088: return true;
089: } else {
090: return false;
091: }
092: }
093:
094: /**
095: * Assigns the value to all the parameter with the given names
096: *
097: * @param names array of parameter name
098: * @param value parameter value
099: */
100: public void setParameters(final String[] names, final Object value) {
101: for (int i = 0; i < names.length; i++) {
102: setParameter(names[i], value);
103: }
104: }
105:
106: public boolean isParameterSet(final String name) {
107: return getParameter(name) != null;
108: }
109:
110: public boolean isParameterSetLocally(final String name) {
111: return this .parameters != null
112: && this .parameters.get(name) != null;
113: }
114:
115: /**
116: * Removes all parameters from this collection.
117: */
118: public void clear() {
119: this .parameters = null;
120: }
121:
122: /**
123: * Creates a copy of these parameters.
124: * The implementation here instantiates {@link BasicHttpParams},
125: * then calls {@link #copyParams(HttpParams)} to populate the copy.
126: *
127: * @return a new set of params holding a copy of the
128: * <i>local</i> parameters in this object.
129: */
130: public HttpParams copy() {
131: BasicHttpParams clone = new BasicHttpParams();
132: copyParams(clone);
133: return clone;
134: }
135:
136: public Object clone() throws CloneNotSupportedException {
137: BasicHttpParams clone = (BasicHttpParams) super .clone();
138: copyParams(clone);
139: return clone;
140: }
141:
142: /**
143: * Copies the locally defined parameters to the argument parameters.
144: * This method is called from {@link #copy()}.
145: *
146: * @param target the parameters to which to copy
147: */
148: protected void copyParams(HttpParams target) {
149: if (this .parameters == null)
150: return;
151:
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
157: .setParameter((String) me.getKey(), me
158: .getValue());
159: }
160: }
161:
162: }
|