| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.connector; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Provides UserName and Password. |
|---|
| 37 | * |
|---|
| 38 | */ |
|---|
| 39 | public final class UserPassword |
|---|
| 40 | { |
|---|
| 41 | /** A UserPassword instance with a null userName and null password. */ |
|---|
| 42 | public static final UserPassword EMPTY_USER_PASSWORD = new UserPassword (); |
|---|
| 43 | |
|---|
| 44 | private final String mUser; |
|---|
| 45 | private final String mPassword; |
|---|
| 46 | private final int mHashCode; |
|---|
| 47 | |
|---|
| 48 | private UserPassword (UserPassword up) |
|---|
| 49 | { |
|---|
| 50 | mHashCode = up.mHashCode; |
|---|
| 51 | mUser = up.mUser; |
|---|
| 52 | mPassword = up.mPassword; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | private UserPassword () |
|---|
| 56 | { |
|---|
| 57 | this(null, null); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | private UserPassword (String user, String password) |
|---|
| 61 | { |
|---|
| 62 | mUser = user; |
|---|
| 63 | mPassword = password; |
|---|
| 64 | mHashCode = new StringBuffer(UserPassword.class.getName()).append(mUser) |
|---|
| 65 | .append(mPassword).toString().hashCode(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Constructs a new UserPassword instance containing exactly the same user |
|---|
| 70 | * and password as the <code>up</code> instance provides. |
|---|
| 71 | * |
|---|
| 72 | * @param up The UserPassword instance to be cloned. |
|---|
| 73 | * |
|---|
| 74 | * @return a new UserPassword instance containing the same user and |
|---|
| 75 | * password as the <code>up</code> instance provides. |
|---|
| 76 | */ |
|---|
| 77 | public static UserPassword fromUserPassword (UserPassword up) |
|---|
| 78 | { |
|---|
| 79 | final UserPassword result; |
|---|
| 80 | if (EMPTY_USER_PASSWORD.equals(up)) |
|---|
| 81 | { |
|---|
| 82 | result = EMPTY_USER_PASSWORD; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | result = new UserPassword(up); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return result; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Returns a new UserPassword instance wrapping the given <code>user</code> |
|---|
| 94 | * and <code>password</code>. |
|---|
| 95 | * |
|---|
| 96 | * @param user UserName |
|---|
| 97 | * @param password Password |
|---|
| 98 | * |
|---|
| 99 | * @return a new UserPassword instance wrapping the given <code>user</code> |
|---|
| 100 | * and <code>password</code>. |
|---|
| 101 | */ |
|---|
| 102 | public static UserPassword fromUserPassword (final String user, |
|---|
| 103 | final String password) |
|---|
| 104 | { |
|---|
| 105 | final UserPassword result; |
|---|
| 106 | if (user == null && password == null) |
|---|
| 107 | { |
|---|
| 108 | result = EMPTY_USER_PASSWORD; |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | { |
|---|
| 112 | result = new UserPassword(user, password); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | return result; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * @return Returns the Password. |
|---|
| 121 | */ |
|---|
| 122 | public String getPassword () |
|---|
| 123 | { |
|---|
| 124 | return mPassword; |
|---|
| 125 | } |
|---|
| 126 | /** |
|---|
| 127 | * @return Returns the User. |
|---|
| 128 | */ |
|---|
| 129 | public String getUserName () |
|---|
| 130 | { |
|---|
| 131 | return mUser; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Returns true if the UserPassword instance <code>up</code> contains a null |
|---|
| 136 | * UserName and null Password. |
|---|
| 137 | * |
|---|
| 138 | * @param up UserPassword to be checked. |
|---|
| 139 | * |
|---|
| 140 | * @return true if the UserPassword instance <code>up</code> contains a null |
|---|
| 141 | * UserName and null Password; otherwise false. |
|---|
| 142 | */ |
|---|
| 143 | public static boolean isEmpty (UserPassword up) |
|---|
| 144 | { |
|---|
| 145 | return UserPassword.EMPTY_USER_PASSWORD.equals(up); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** {@inheritDoc} */ |
|---|
| 149 | public boolean equals (Object other) |
|---|
| 150 | { |
|---|
| 151 | boolean result = false; |
|---|
| 152 | if (this == other) |
|---|
| 153 | { |
|---|
| 154 | result = true; |
|---|
| 155 | } |
|---|
| 156 | else if (other != null) |
|---|
| 157 | { |
|---|
| 158 | if (mHashCode == other.hashCode() && other instanceof UserPassword) |
|---|
| 159 | { |
|---|
| 160 | final UserPassword up = (UserPassword) other; |
|---|
| 161 | result = isStringsEquals(mUser, up.mUser) |
|---|
| 162 | && isStringsEquals(mPassword, up.mPassword); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | return result; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** {@inheritDoc} */ |
|---|
| 170 | public int hashCode () |
|---|
| 171 | { |
|---|
| 172 | return mHashCode; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | private static boolean isStringsEquals (final String a, final String b) |
|---|
| 176 | { |
|---|
| 177 | boolean equals = false; |
|---|
| 178 | if (a == null && b == null) |
|---|
| 179 | { |
|---|
| 180 | equals = true; |
|---|
| 181 | } |
|---|
| 182 | else if (a != null && a.equals(b)) |
|---|
| 183 | { |
|---|
| 184 | equals = true; |
|---|
| 185 | } |
|---|
| 186 | return equals; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** {@inheritDoc} */ |
|---|
| 190 | public String toString () |
|---|
| 191 | { |
|---|
| 192 | return new StringBuffer("<UserPassword user: '").append(mUser) |
|---|
| 193 | .append("', password: 'xxx'>").toString(); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|