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 */
017package org.apache.activemq.jms.pool;
018
019/**
020 * A cache key for the connection details
021 */
022public 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    @Override
042    public int hashCode() {
043        return hash;
044    }
045
046    @Override
047    public boolean equals(Object that) {
048        if (this == that) {
049            return true;
050        }
051        if (that instanceof ConnectionKey) {
052            return equals((ConnectionKey) that);
053        }
054        return false;
055    }
056
057    public boolean equals(ConnectionKey that) {
058        return isEqual(this.userName, that.userName) && isEqual(this.password, that.password);
059    }
060
061    public String getPassword() {
062        return password;
063    }
064
065    public String getUserName() {
066        return userName;
067    }
068
069    public static boolean isEqual(Object o1, Object o2) {
070        if (o1 == o2) {
071            return true;
072        }
073        return o1 != null && o2 != null && o1.equals(o2);
074    }
075}