KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > NameValuePair


1 /**
2  * $Id: NameValuePair.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 1997-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.io.Serializable JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Luggage class implementation of a name-value pair. <em>Security note</em>:
36  * NameValuePairs are both <span class="src">Cloneable</span> and
37  * <span class="src">Serializable</span> by definition.
38  *
39  * @since JWare/core 0.5
40  * @author ssmc, &copy;1997-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
41  * @version 0.5
42  * @.safety single
43  * @.group api,helper
44  * @see NameValuePair#NONE
45  **/

46
47 public final class NameValuePair implements Cloneable JavaDoc, Serializable JavaDoc, Map.Entry JavaDoc
48 {
49     /**
50      * Initializes new blank name-value item; both name
51      * and value are empty strings.
52      **/

53     public NameValuePair()
54     {
55         this("","");
56     }
57
58
59     /**
60      * Initializes new name-value item with given values.
61      **/

62     public NameValuePair(String JavaDoc k, Object JavaDoc v)
63     {
64         m_nam = k;
65         m_val = v;
66     }
67
68
69     /**
70      * Symbolic singleton NameValuePair that can represent "no value"
71      * or "empty".
72      * @.pattern JWare.Placeholder
73      **/

74     public static final NameValuePair NONE = new NameValuePair();
75
76
77
78     /**
79      * Returns name portion of this name-value item.
80      **/

81     public String JavaDoc getName()
82     {
83         return m_nam;
84     }
85
86
87     /**
88      * Updates name portion of this name-value item.
89      **/

90     public void setName(String JavaDoc k)
91     {
92         m_nam = k;
93     }
94
95
96     /**
97      * <span class="src">Map.Entry</span> version of
98      * {@linkplain #getName}.
99      * @since JWare/core 0.7
100      **/

101     public final Object JavaDoc getKey()
102     {
103         return getName();
104     }
105
106
107     /**
108      * Returns value portion of this name-value item.
109      **/

110     public Object JavaDoc getValue()
111     {
112         return m_val;
113     }
114
115
116     /**
117      * Returns value portion of this name-value item as a
118      * <span class="src">boolean</span>.
119      **/

120     public boolean getBoolean()
121     {
122         return m_val==Boolean.TRUE ? true : false;
123     }
124
125
126     /**
127      * Updates value portion of this name-value item.
128      **/

129     public Object JavaDoc setValue(Object JavaDoc v)
130     {
131         Object JavaDoc old = m_val;
132         m_val = v;
133         return old;
134     }
135
136
137     /**
138      * Updates value to a <span class="src">boolean</span>.
139      * Normalizes to use shared <span class="src'>Boolean.TRUE</span>
140      * or <span class="src">Boolean.FALSE</span> settings.
141      **/

142     public Object JavaDoc setValue(boolean b)
143     {
144         Object JavaDoc old = m_val;
145         m_val = b ? Boolean.TRUE : Boolean.FALSE;
146         return old;
147     }
148
149
150     /**
151      * Returns shallow clone of this name-value item; returned
152      * clone references this item's name and value.
153      **/

154     public Object JavaDoc clone()
155     {
156         try {
157             return super.clone();
158         }
159         catch (CloneNotSupportedException JavaDoc clnx) {
160             throw new Error JavaDoc();
161         }
162     }
163
164
165     /**
166      * Determines if given item is semantically equivalent to
167      * this item.
168      **/

169     public boolean equals(Object JavaDoc o)
170     {
171         if (o==this) {
172             return true;
173         }
174         if (o!=null && o.getClass()==NameValuePair.class) {
175             NameValuePair it= (NameValuePair)o;
176             if (nameEquals(it.m_nam)) {
177                 if (m_val==null) {
178                     return it.m_val==null;
179                 }
180                 return m_val.equals(it.m_val);
181             }
182         }
183         return false;
184     }
185
186
187     /**
188      * Returns a hash value for this item (depends on name
189      * and value).
190      **/

191     public int hashCode()
192     {
193         int hc = 31;
194         if (m_nam!=null) {
195             hc = 31*hc + m_nam.hashCode();
196         }
197         if (m_val!=null) {
198             hc = 31*hc + m_val.hashCode();
199         }
200         return hc;
201     }
202
203
204     /**
205      * Check if this item's name is equivalent to given
206      * string.
207      **/

208     public boolean nameEquals(String JavaDoc othernam)
209     {
210         if (m_nam==null) {
211             return othernam==null;
212         }
213         if (othernam==null) {
214             return m_nam==null;
215         }
216         return m_nam.equals(othernam);
217     }
218
219
220     /**
221      * Returns the name of this name-value item.
222      **/

223     public String JavaDoc toString()
224     {
225         return m_nam;
226     }
227
228     private String JavaDoc m_nam;
229     private Object JavaDoc m_val;
230 }
231
232 /* end-of-NameValuePair.java */
233
Popular Tags