001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.pool;
018
019 /**
020 * A cache key for the connection details
021 *
022 * @version $Revision: 1.1 $
023 */
024 public class ConnectionKey {
025 private String userName;
026 private String password;
027 private int hash;
028
029 public ConnectionKey(String userName, String password) {
030 this.password = password;
031 this.userName = userName;
032 hash = 31;
033 if (userName != null) {
034 hash += userName.hashCode();
035 }
036 hash *= 31;
037 if (password != null) {
038 hash += password.hashCode();
039 }
040 }
041
042 public int hashCode() {
043 return hash;
044 }
045
046 public boolean equals(Object that) {
047 if (this == that) {
048 return true;
049 }
050 if (that instanceof ConnectionKey) {
051 return equals((ConnectionKey)that);
052 }
053 return false;
054 }
055
056 public boolean equals(ConnectionKey that) {
057 return isEqual(this.userName, that.userName) && isEqual(this.password, that.password);
058 }
059
060 public String getPassword() {
061 return password;
062 }
063
064 public String getUserName() {
065 return userName;
066 }
067
068 public static boolean isEqual(Object o1, Object o2) {
069 if (o1 == o2) {
070 return true;
071 }
072 return o1 != null && o2 != null && o1.equals(o2);
073 }
074
075 }