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.command;
018
019/**
020 * @openwire:marshaller code="122"
021 * 
022 */
023public class ConsumerId implements DataStructure {
024
025    public static final byte DATA_STRUCTURE_TYPE = CommandTypes.CONSUMER_ID;
026
027    protected String connectionId;
028    protected long sessionId;
029    protected long value;
030
031    protected transient int hashCode;
032    protected transient String key;
033    protected transient SessionId parentId;
034
035    public ConsumerId() {
036    }
037
038    public ConsumerId(String str){
039        if (str != null){
040            String[] splits = str.split(":");
041            if (splits != null && splits.length >= 3){
042                this.connectionId = splits[0];
043                this.sessionId =  Long.parseLong(splits[1]);
044                this.value = Long.parseLong(splits[2]);
045            }
046        }
047    }
048
049    public ConsumerId(SessionId sessionId, long consumerId) {
050        this.connectionId = sessionId.getConnectionId();
051        this.sessionId = sessionId.getValue();
052        this.value = consumerId;
053    }
054
055    public ConsumerId(ConsumerId id) {
056        this.connectionId = id.getConnectionId();
057        this.sessionId = id.getSessionId();
058        this.value = id.getValue();
059    }
060
061    public SessionId getParentId() {
062        if (parentId == null) {
063            parentId = new SessionId(this);
064        }
065        return parentId;
066    }
067
068    public int hashCode() {
069        if (hashCode == 0) {
070            hashCode = connectionId.hashCode() ^ (int)sessionId ^ (int)value;
071        }
072        return hashCode;
073    }
074
075    public boolean equals(Object o) {
076        if (this == o) {
077            return true;
078        }
079        if (o == null || o.getClass() != ConsumerId.class) {
080            return false;
081        }
082        ConsumerId id = (ConsumerId)o;
083        return sessionId == id.sessionId && value == id.value && connectionId.equals(id.connectionId);
084    }
085
086    public byte getDataStructureType() {
087        return DATA_STRUCTURE_TYPE;
088    }
089
090    public String toString() {
091        if (key == null) {
092            key = connectionId + ":" + sessionId + ":" + value;
093        }
094        return key;
095    }
096
097    /**
098     * @openwire:property version=1
099     */
100    public String getConnectionId() {
101        return connectionId;
102    }
103
104    public void setConnectionId(String connectionId) {
105        this.connectionId = connectionId;
106    }
107
108    /**
109     * @openwire:property version=1
110     */
111    public long getSessionId() {
112        return sessionId;
113    }
114
115    public void setSessionId(long sessionId) {
116        this.sessionId = sessionId;
117    }
118
119    /**
120     * @openwire:property version=1
121     */
122    public long getValue() {
123        return value;
124    }
125
126    public void setValue(long consumerId) {
127        this.value = consumerId;
128    }
129
130    public boolean isMarshallAware() {
131        return false;
132    }
133}