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/DefaultedHttpParams.java $
      3  * $Revision: 610763 $
      4  * $Date: 2008-01-10 04:01:13 -0800 (Thu, 10 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 org.apache.http.params.HttpParams;
     35 
     36 /**
     37  * {@link HttpParams} implementation that delegates resolution of a parameter
     38  * to the given default {@link HttpParams} instance if the parameter is not
     39  * present in the local one. The state of the local collection can be mutated,
     40  * whereas the default collection is treated as read-only.
     41  *
     42  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     43  *
     44  * @version $Revision: 610763 $
     45  */
     46 public final class DefaultedHttpParams extends AbstractHttpParams {
     47 
     48     private final HttpParams local;
     49     private final HttpParams defaults;
     50 
     51     public DefaultedHttpParams(final HttpParams local, final HttpParams defaults) {
     52         super();
     53         if (local == null) {
     54             throw new IllegalArgumentException("HTTP parameters may not be null");
     55         }
     56         this.local = local;
     57         this.defaults = defaults;
     58     }
     59 
     60     /**
     61      * Creates a copy of the local collection with the same default
     62      */
     63     public HttpParams copy() {
     64         HttpParams clone = this.local.copy();
     65         return new DefaultedHttpParams(clone, this.defaults);
     66     }
     67 
     68     /**
     69      * Retrieves the value of the parameter from the local collection and, if the
     70      * parameter is not set locally, delegates its resolution to the default
     71      * collection.
     72      */
     73     public Object getParameter(final String name) {
     74         Object obj = this.local.getParameter(name);
     75         if (obj == null && this.defaults != null) {
     76             obj = this.defaults.getParameter(name);
     77         }
     78         return obj;
     79     }
     80 
     81     /**
     82      * Attempts to remove the parameter from the local collection. This method
     83      * <i>does not</i> modify the default collection.
     84      */
     85     public boolean removeParameter(final String name) {
     86         return this.local.removeParameter(name);
     87     }
     88 
     89     /**
     90      * Sets the parameter in the local collection. This method <i>does not</i>
     91      * modify the default collection.
     92      */
     93     public HttpParams setParameter(final String name, final Object value) {
     94         return this.local.setParameter(name, value);
     95     }
     96 
     97     public HttpParams getDefaults() {
     98         return this.defaults;
     99     }
    100 
    101 }
    102