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.util.Set;
31  
32  import org.apache.http.params.HttpParams;
33  
34  
35  /**
36   * Abstract base class for parameter collections.
37   * Type specific setters and getters are mapped to the abstract,
38   * generic getters and setters.
39   *
40   * @since 4.0
41   */
42  public abstract class AbstractHttpParams implements HttpParams, HttpParamsNames {
43  
44      /**
45       * Instantiates parameters.
46       */
47      protected AbstractHttpParams() {
48          super();
49      }
50  
51      public long getLongParameter(final String name, long defaultValue) {
52          Object param = getParameter(name);
53          if (param == null) {
54              return defaultValue;
55          }
56          return ((Long)param).longValue();
57      }
58  
59      public HttpParams setLongParameter(final String name, long value) {
60          setParameter(name, new Long(value));
61          return this;
62      }
63  
64      public int getIntParameter(final String name, int defaultValue) {
65          Object param = getParameter(name);
66          if (param == null) {
67              return defaultValue;
68          }
69          return ((Integer)param).intValue();
70      }
71  
72      public HttpParams setIntParameter(final String name, int value) {
73          setParameter(name, new Integer(value));
74          return this;
75      }
76  
77      public double getDoubleParameter(final String name, double defaultValue) {
78          Object param = getParameter(name);
79          if (param == null) {
80              return defaultValue;
81          }
82          return ((Double)param).doubleValue();
83      }
84  
85      public HttpParams setDoubleParameter(final String name, double value) {
86          setParameter(name, new Double(value));
87          return this;
88      }
89  
90      public boolean getBooleanParameter(final String name, boolean defaultValue) {
91          Object param = getParameter(name);
92          if (param == null) {
93              return defaultValue;
94          }
95          return ((Boolean)param).booleanValue();
96      }
97  
98      public HttpParams setBooleanParameter(final String name, boolean value) {
99          setParameter(name, value ? Boolean.TRUE : Boolean.FALSE);
100         return this;
101     }
102 
103     public boolean isParameterTrue(final String name) {
104         return getBooleanParameter(name, false);
105     }
106 
107     public boolean isParameterFalse(final String name) {
108         return !getBooleanParameter(name, false);
109     }
110 
111     /**
112      * {@inheritDoc}
113      * <p/>
114      * Dummy implementation - must be overridden by subclasses.
115      *
116      * @since 4.2
117      * @throws UnsupportedOperationException - always
118      */
119     public Set<String> getNames(){
120         throw new UnsupportedOperationException();
121     }
122 
123 } // class AbstractHttpParams