| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector; |
| 34 | | | |
| 35 | | | /** |
| 36 | | | * Provides UserName and Password. |
| 37 | | | * |
| 38 | | | */ |
| 39 | | (1) | 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 | | | } |
| 196 | | | |