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    public class ConnectionKey {
023    
024        private final String userName;
025        private final String password;
026        private int hash;
027    
028        public ConnectionKey(String userName, String password) {
029            this.password = password;
030            this.userName = userName;
031            hash = 31;
032            if (userName != null) {
033                hash += userName.hashCode();
034            }
035            hash *= 31;
036            if (password != null) {
037                hash += password.hashCode();
038            }
039        }
040    
041        public int hashCode() {
042            return hash;
043        }
044    
045        public boolean equals(Object that) {
046            if (this == that) {
047                return true;
048            }
049            if (that instanceof ConnectionKey) {
050                return equals((ConnectionKey)that);
051            }
052            return false;
053        }
054    
055        public boolean equals(ConnectionKey that) {
056            return isEqual(this.userName, that.userName) && isEqual(this.password, that.password);
057        }
058    
059        public String getPassword() {
060            return password;
061        }
062    
063        public String getUserName() {
064            return userName;
065        }
066    
067        public static boolean isEqual(Object o1, Object o2) {
068            if (o1 == o2) {
069                return true;
070            }
071            return o1 != null && o2 != null && o1.equals(o2);
072        }
073    
074    }