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.util;
018
019import org.apache.activemq.command.SubscriptionInfo;
020
021public class SubscriptionKey {
022
023    public final String clientId;
024    public final String subscriptionName;
025    private final int hashValue;
026
027    public SubscriptionKey(SubscriptionInfo info) {
028        this(info.getClientId(), info.getSubscriptionName());
029    }
030
031    public SubscriptionKey(String clientId, String subscriptionName) {
032        this.clientId = clientId;
033        this.subscriptionName = subscriptionName != null ? subscriptionName : "NOT_SET";
034        hashValue = clientId.hashCode() ^ this.subscriptionName.hashCode();
035    }
036
037    public int hashCode() {
038        return hashValue;
039    }
040
041    public boolean equals(Object o) {
042        try {
043            SubscriptionKey key = (SubscriptionKey)o;
044            return key.clientId.equals(clientId) && key.subscriptionName.equals(subscriptionName);
045        } catch (Throwable e) {
046            return false;
047        }
048    }
049
050    public String toString() {
051        return clientId + ":" + subscriptionName;
052    }
053
054    public String getClientId() {
055        return clientId;
056    }
057
058    public String getSubscriptionName() {
059        return subscriptionName;
060    }
061}