Home | History | Annotate | Download | only in params
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/params/BasicHttpParams.java $
      3  * $Revision: 610464 $
      4  * $Date: 2008-01-09 09:10:55 -0800 (Wed, 09 Jan 2008) $
      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  * This class represents a collection of HTTP protocol parameters.
     43  * Protocol parameters may be linked together to form a hierarchy.
     44  * If a particular parameter value has not been explicitly defined
     45  * in the collection itself, its value will be drawn from the parent
     46  * collection of parameters.
     47  *
     48  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     49  *
     50  * @version $Revision: 610464 $
     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 HashMap parameters;
     59 
     60     public BasicHttpParams() {
     61         super();
     62     }
     63 
     64     public Object getParameter(final String name) {
     65         // See if the parameter has been explicitly defined
     66         Object param = null;
     67         if (this.parameters != null) {
     68             param = this.parameters.get(name);
     69         }
     70         return param;
     71     }
     72 
     73     public HttpParams setParameter(final String name, final Object value) {
     74         if (this.parameters == null) {
     75             this.parameters = new HashMap();
     76         }
     77         this.parameters.put(name, value);
     78         return this;
     79     }
     80 
     81     public boolean removeParameter(String name) {
     82         if (this.parameters == null) {
     83             return false;
     84         }
     85         //this is to avoid the case in which the key has a null value
     86         if (this.parameters.containsKey(name)) {
     87             this.parameters.remove(name);
     88             return true;
     89         } else {
     90             return false;
     91         }
     92     }
     93 
     94 
     95     /**
     96      * Assigns the value to all the parameter with the given names
     97      *
     98      * @param names array of parameter name
     99      * @param value parameter value
    100      */
    101     public void setParameters(final String[] names, final Object value) {
    102         for (int i = 0; i < names.length; i++) {
    103             setParameter(names[i], value);
    104         }
    105     }
    106 
    107     public boolean isParameterSet(final String name) {
    108         return getParameter(name) != null;
    109     }
    110 
    111     public boolean isParameterSetLocally(final String name) {
    112         return this.parameters != null && 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.setParameter((String)me.getKey(), me.getValue());
    157         }
    158     }
    159 
    160 }
    161