KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > NameValuePair


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Properties JavaDoc;
24
25
26 /**
27  * Holds a pair of string values appropriate for name / value pairs.
28  *
29  * @author Unknown
30  */

31 public class NameValuePair implements Serializable JavaDoc {
32     
33     // Private instance variables
34

35     private String JavaDoc name = null;
36     private String JavaDoc value = null;
37
38     /**
39      * Default constructor.
40      *
41      */

42     public NameValuePair() {
43         this (null, null);
44     }
45
46     /**
47      * Constructor.
48      * @param name The name.
49      * @param value The value.
50      */

51     public NameValuePair(String JavaDoc name, String JavaDoc value) {
52         this.name = name;
53         this.value = value;
54     }
55
56     /**
57      * Take a name / value pair string separating on an equals symbol (=)
58      * and construct a new name value pair object. If no separator can be
59      * found then the value will be an empty string
60      *
61      * @param nameValuePair name value pair string separated by =
62      */

63     public NameValuePair(String JavaDoc nameValuePair) {
64         int idx = nameValuePair.indexOf('=');
65         if(idx == -1) {
66             name = nameValuePair;
67             value = "";
68         }
69         else {
70             name = nameValuePair.substring(0, idx);
71             value = nameValuePair.substring(idx + 1);
72         }
73     }
74
75     /**
76      * Set the name.
77      *
78      * @param name The new name
79      * @see #getName()
80      */

81     public void setName(String JavaDoc name) {
82         this.name = name;
83     }
84
85     /**
86      * Return the name.
87      *
88      * @return String name The name
89      * @see #setName(String)
90      */

91     public String JavaDoc getName() {
92         return name;
93     }
94
95     /**
96      * Set the value.
97      *
98      * @param value The new value.
99      */

100     public void setValue(String JavaDoc value) {
101         this.value = value;
102     }
103
104
105     /**
106      * Return the current value.
107      *
108      * @return String value The current value.
109      */

110     public String JavaDoc getValue() {
111         return value;
112     }
113
114     // --------------------------------------------------------- Public Methods
115

116     /**
117      * Get a String representation of this pair.
118      * @return A string representation.
119      */

120     public String JavaDoc toString() {
121         return ("name=" + name + ", " + "value=" + value);
122     }
123
124     /**
125      * Test if the given <i>object</i> is equal to me. <tt>NameValuePair</tt>s
126      * are equals if both their <tt>name</tt> and <tt>value</tt> fields are equal.
127      * If <tt>object</tt> is <tt>null</tt> this method returns <tt>false</tt>.
128      *
129      * @param object the {@link Object} to compare to or <tt>null</tt>
130      * @return true if the objects are equal.
131      */

132     public boolean equals(Object JavaDoc object) {
133         if (object == null) return false;
134         if (this == object) return true;
135         if (!(object instanceof NameValuePair)) return false;
136
137         NameValuePair pair = (NameValuePair) object;
138         return ((null == name ? null == pair.name : name.equals(pair.name))
139               && (null == value ? null == pair.value : value.equals(pair.value)));
140     }
141
142     /**
143      * hashCode. Returns a hash code for this object such that if <tt>a.{@link
144      * #equals equals}(b)</tt> then <tt>a.hashCode() == b.hashCode()</tt>.
145      * @return The hash code.
146      */

147     public int hashCode() {
148         return (this.getClass().hashCode()
149             ^ (null == name ? 0 : name.hashCode())
150             ^ (null == value ? 0 : value.hashCode()));
151     }
152
153     /**
154      * Add the name value pair to a {@link Properties} object.
155      *
156      * @param p properties
157      */

158     public void add(Properties JavaDoc p) {
159         p.setProperty(name, value);
160     }
161 }
162
Popular Tags