KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > jdbc > util > DBKey


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/util/Attic/DBKey.java,v 1.10 2004/02/26 00:22:27 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.jdbc.util;
20
21 import java.io.Serializable JavaDoc;
22
23 public class DBKey implements Serializable JavaDoc //TODO does it need to be serializable?
24
{
25     private final String JavaDoc driver;
26     private final String JavaDoc url;
27     private final String JavaDoc username;
28     private final String JavaDoc password;
29
30     /**
31      * Cache for the hashCode value since this class will frequently be used
32      * as a Map key.
33      */

34     private transient int hashCode = 0;
35
36     
37     public DBKey(
38         String JavaDoc driver,
39         String JavaDoc url,
40         String JavaDoc username,
41         String JavaDoc password)
42     {
43         if (driver == null)
44         {
45             throw new IllegalArgumentException JavaDoc(
46                     "DBKey 'driver' must be non-null");
47         }
48
49         if (url == null)
50         {
51             throw new IllegalArgumentException JavaDoc("DBKey 'url' must be non-null");
52         }
53
54         // Other fields are allowed to be null
55

56         this.driver = driver;
57         this.url = url;
58         this.username = username;
59         this.password = password;
60         
61         // All the fields used by the hashCode are now fixed, so calculate it
62
hashCode = calculateHashCode();
63     }
64     
65     // Dummy constructor to allow JMeter test suite to work
66
public DBKey(){
67         this("","","","");
68     }
69
70     public String JavaDoc getUrl()
71     {
72         return url;
73     }
74
75     public String JavaDoc getUsername()
76     {
77         return username;
78     }
79
80     public String JavaDoc getPassword()
81     {
82         return password;
83     }
84
85     public String JavaDoc getDriver()
86     {
87         return driver;
88     }
89
90     /**
91      * Determines if the two DBKey objects have the same property values.
92      * @param o2 DBKey to compare with this one.
93      * @return bool True if equal, false otherwise.
94      */

95     public boolean equals(Object JavaDoc o2)
96     {
97         if (this == o2)
98         {
99             return true;
100         }
101         
102         if (!(o2 instanceof DBKey))
103         {
104             return false;
105         }
106         
107         DBKey key = (DBKey)o2;
108         
109         if (!driver.equals(key.driver))
110         {
111             return false;
112         }
113         
114         if (!url.equals(key.url))
115         {
116             return false;
117         }
118         
119         if (username == null)
120         {
121             if (key.username != null)
122             {
123                 return false;
124             }
125         }
126         else
127         {
128             if (!username.equals(key.username))
129             {
130                 return false;
131             }
132         }
133         
134         if (password == null)
135         {
136             if (key.password != null)
137             {
138                 return false;
139             }
140         }
141         else
142         {
143             if (!password.equals(key.password))
144             {
145                 return false;
146             }
147         }
148         
149         return true;
150     }
151
152     public int hashCode()
153     {
154         hashCode = calculateHashCode();
155         return hashCode;
156     }
157     
158     private int calculateHashCode()
159     {
160         // Implementation based on Joshua Bloch's _Effective Java_
161
// http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
162
int result = 17;
163         
164         result = 37 * result + driver.hashCode();
165         result = 37 * result + url.hashCode();
166         result = 37 * result + (username == null ? 0 : username.hashCode());
167         result = 37 * result + (password == null ? 0 : password.hashCode());
168         
169         return result;
170     }
171
172     public String JavaDoc toString()
173     {
174         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
175         ret.append("Class=DBKey(" + "\n");
176         ret.append("driver=" + driver + "\n");
177         ret.append("url=" + url + "\n");
178         ret.append("username=" + username + "\n");
179         ret.append(")");
180         return ret.toString();
181     }
182 }
183
Popular Tags