View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.params;
29  
30  import java.io.Serializable;
31  import java.util.HashSet;
32  import java.util.Map;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.Set;
36  
37  import org.apache.http.annotation.NotThreadSafe;
38  import org.apache.http.params.HttpParams;
39  
40  /**
41   * Default implementation of {@link HttpParams} interface.
42   * <p>
43   * Please note access to the internal structures of this class is not
44   * synchronized and therefore this class may be thread-unsafe.
45   *
46   * @since 4.0
47   */
48  @NotThreadSafe
49  public class BasicHttpParams extends AbstractHttpParams implements Serializable, Cloneable {
50  
51      private static final long serialVersionUID = -7086398485908701455L;
52  
53      /** Map of HTTP parameters that this collection contains. */
54      private final HashMap<String, Object> parameters = new HashMap<String, Object>();
55  
56      public BasicHttpParams() {
57          super();
58      }
59  
60      public Object getParameter(final String name) {
61          return this.parameters.get(name);
62      }
63  
64      public HttpParams setParameter(final String name, final Object value) {
65          this.parameters.put(name, value);
66          return this;
67      }
68  
69      public boolean removeParameter(String name) {
70          //this is to avoid the case in which the key has a null value
71          if (this.parameters.containsKey(name)) {
72              this.parameters.remove(name);
73              return true;
74          } else {
75              return false;
76          }
77      }
78  
79      /**
80       * Assigns the value to all the parameter with the given names
81       *
82       * @param names array of parameter names
83       * @param value parameter value
84       */
85      public void setParameters(final String[] names, final Object value) {
86          for (int i = 0; i < names.length; i++) {
87              setParameter(names[i], value);
88          }
89      }
90  
91      /**
92       * Is the parameter set?
93       * <p>
94       * Uses {@link #getParameter(String)} (which is overrideable) to
95       * fetch the parameter value, if any.
96       * <p>
97       * Also @see {@link #isParameterSetLocally(String)}
98       *
99       * @param name parameter name
100      * @return true if parameter is defined and non-null
101      */
102     public boolean isParameterSet(final String name) {
103         return getParameter(name) != null;
104     }
105 
106     /**
107      * Is the parameter set in this object?
108      * <p>
109      * The parameter value is fetched directly.
110      * <p>
111      * Also @see {@link #isParameterSet(String)}
112      *
113      * @param name parameter name
114      * @return true if parameter is defined and non-null
115      */
116     public boolean isParameterSetLocally(final String name) {
117         return this.parameters.get(name) != null;
118     }
119 
120     /**
121      * Removes all parameters from this collection.
122      */
123     public void clear() {
124         this.parameters.clear();
125     }
126 
127     /**
128      * Creates a copy of these parameters.
129      * This implementation calls {@link #clone()}.
130      *
131      * @return  a new set of params holding a copy of the
132      *          <i>local</i> parameters in this object.
133      *
134      * @deprecated (4.1)
135      * @throws UnsupportedOperationException if the clone() fails
136      */
137     @Deprecated
138     public HttpParams copy() {
139         try {
140             return (HttpParams) clone();
141         } catch (CloneNotSupportedException ex) {
142             throw new UnsupportedOperationException("Cloning not supported");
143         }
144     }
145 
146     /**
147      * Clones the instance.
148      * Uses {@link #copyParams(HttpParams)} to copy the parameters.
149      */
150     @Override
151     public Object clone() throws CloneNotSupportedException {
152         BasicHttpParams clone = (BasicHttpParams) super.clone();
153         copyParams(clone);
154         return clone;
155     }
156 
157     /**
158      * Copies the locally defined parameters to the argument parameters.
159      * This method is called from {@link #clone()}.
160      *
161      * @param target    the parameters to which to copy
162      * @since 4.2
163      */
164     public void copyParams(HttpParams target) {
165         Iterator<Map.Entry<String, Object>> iter = this.parameters.entrySet().iterator();
166         while (iter.hasNext()) {
167             Map.Entry<String, Object> me = iter.next();
168             if (me.getKey() instanceof String)
169                 target.setParameter(me.getKey(), me.getValue());
170         }
171     }
172 
173     /**
174      * Returns the current set of names.
175      *
176      * Changes to the underlying HttpParams are not reflected
177      * in the set - it is a snapshot.
178      *
179      * @return the names, as a Set<String>
180      * @since 4.2
181      */
182     @Override
183     public Set<String> getNames() {
184         return new HashSet<String>(this.parameters.keySet());
185     }
186 }