View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/NameValuePair.java,v 1.17 2004/04/18 23:51:35 jsdever Exp $
3    * $Revision: 1.17 $
4    * $Date: 2004/04/18 23:51:35 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient;
31  
32  import java.io.Serializable;
33  
34  /***
35   * <p>A simple class encapsulating a name/value pair.</p>
36   * 
37   * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
38   * @author Sean C. Sullivan
39   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
40   * 
41   * @version $Revision: 1.17 $ $Date: 2004/04/18 23:51:35 $
42   * 
43   */
44  public class NameValuePair implements Serializable {
45  
46      // ----------------------------------------------------------- Constructors
47  
48      /***
49       * Default constructor.
50       * 
51       */
52      public NameValuePair() {
53          this (null, null);
54      }
55  
56      /***
57       * Constructor.
58       * @param name The name.
59       * @param value The value.
60       */
61      public NameValuePair(String name, String value) {
62          this.name = name;
63          this.value = value;
64      }
65  
66      // ----------------------------------------------------- Instance Variables
67  
68      /***
69       * Name.
70       */
71      private String name = null;
72  
73      /***
74       * Value.
75       */
76      private String value = null;
77  
78      // ------------------------------------------------------------- Properties
79  
80      /***
81       * Set the name.
82       *
83       * @param name The new name
84       * @see #getName()
85       */
86      public void setName(String name) {
87          this.name = name;
88      }
89  
90  
91      /***
92       * Return the name.
93       *
94       * @return String name The name
95       * @see #setName(String)
96       */
97      public String getName() {
98          return name;
99      }
100 
101 
102     /***
103      * Set the value.
104      *
105      * @param value The new value.
106      */
107     public void setValue(String value) {
108         this.value = value;
109     }
110 
111 
112     /***
113      * Return the current value.
114      *
115      * @return String value The current value.
116      */
117     public String getValue() {
118         return value;
119     }
120 
121     // --------------------------------------------------------- Public Methods
122 
123     /***
124      * Get a String representation of this pair.
125      * @return A string representation.
126      */
127     public String toString() {
128         return ("name=" + name + ", " + "value=" + value);
129     }
130 
131     /***
132      * Test if the given <i>object</i> is equal to me. <tt>NameValuePair</tt>s
133      * are equals if both their <tt>name</tt> and <tt>value</tt> fields are equal.
134      * If <tt>object</tt> is <tt>null</tt> this method returns <tt>false</tt>.
135      *
136      * @param object the {@link Object} to compare to or <tt>null</tt>
137      * @return true if the objects are equal.
138      */
139     public boolean equals(Object object) {
140         if (object == null) return false;
141         if (this == object) return true;
142         if (!(object instanceof NameValuePair)) return false;
143         
144         NameValuePair pair = (NameValuePair) object;
145         return ((null == name ? null == pair.name : name.equals(pair.name))
146               && (null == value ? null == pair.value : value.equals(pair.value)));
147     }
148 
149     /***
150      * hashCode. Returns a hash code for this object such that if <tt>a.{@link
151      * #equals equals}(b)</tt> then <tt>a.hashCode() == b.hashCode()</tt>.
152      * @return The hash code.
153      */
154     public int hashCode() {
155         return (this.getClass().hashCode() 
156             ^ (null == name ? 0 : name.hashCode()) 
157             ^ (null == value ? 0 : value.hashCode()));
158     }
159 
160     /*
161     public Object clone() {
162         try {
163             NameValuePair that = (NameValuePair)(super.clone());
164             that.setName(this.getName());
165             that.setValue(this.getValue());
166             return that;
167         } catch(CloneNotSupportedException e) {
168             // this should never happen
169             throw new RuntimeException("Panic. super.clone not supported in NameValuePair.");
170         }
171     }
172     */
173 }