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.command;
018    
019    /**
020     * @openwire:marshaller code="123"
021     * 
022     */
023    public class ProducerId implements DataStructure {
024    
025        public static final byte DATA_STRUCTURE_TYPE = CommandTypes.PRODUCER_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 ProducerId() {
036        }
037    
038        public ProducerId(SessionId sessionId, long producerId) {
039            this.connectionId = sessionId.getConnectionId();
040            this.sessionId = sessionId.getValue();
041            this.value = producerId;
042        }
043    
044        public ProducerId(ProducerId id) {
045            this.connectionId = id.getConnectionId();
046            this.sessionId = id.getSessionId();
047            this.value = id.getValue();
048        }
049    
050        public ProducerId(String producerKey) {
051            // Parse off the producerId
052            int p = producerKey.lastIndexOf(":");
053            if (p >= 0) {
054                value = Long.parseLong(producerKey.substring(p + 1));
055                producerKey = producerKey.substring(0, p);
056            }
057            setProducerSessionKey(producerKey);
058        }
059    
060        public SessionId getParentId() {
061            if (parentId == null) {
062                parentId = new SessionId(this);
063            }
064            return parentId;
065        }
066    
067        public int hashCode() {
068            if (hashCode == 0) {
069                hashCode = connectionId.hashCode() ^ (int)sessionId ^ (int)value;
070            }
071            return hashCode;
072        }
073    
074        public boolean equals(Object o) {
075            if (this == o) {
076                return true;
077            }
078            if (o == null || o.getClass() != ProducerId.class) {
079                return false;
080            }
081            ProducerId id = (ProducerId)o;
082            return sessionId == id.sessionId && value == id.value && connectionId.equals(id.connectionId);
083        }
084    
085        /**
086         * @param sessionKey
087         */
088        private void setProducerSessionKey(String sessionKey) {
089            // Parse off the value
090            int p = sessionKey.lastIndexOf(":");
091            if (p >= 0) {
092                sessionId = Long.parseLong(sessionKey.substring(p + 1));
093                sessionKey = sessionKey.substring(0, p);
094            }
095            // The rest is the value
096            connectionId = sessionKey;
097        }
098    
099        public String toString() {
100            if (key == null) {
101                key = connectionId + ":" + sessionId + ":" + value;
102            }
103            return key;
104        }
105    
106        public byte getDataStructureType() {
107            return DATA_STRUCTURE_TYPE;
108        }
109    
110        /**
111         * @openwire:property version=1 cache=true
112         */
113        public String getConnectionId() {
114            return connectionId;
115        }
116    
117        public void setConnectionId(String connectionId) {
118            this.connectionId = connectionId;
119        }
120    
121        /**
122         * @openwire:property version=1
123         */
124        public long getValue() {
125            return value;
126        }
127    
128        public void setValue(long producerId) {
129            this.value = producerId;
130        }
131    
132        /**
133         * @openwire:property version=1
134         */
135        public long getSessionId() {
136            return sessionId;
137        }
138    
139        public void setSessionId(long sessionId) {
140            this.sessionId = sessionId;
141        }
142    
143        public boolean isMarshallAware() {
144            return false;
145        }
146    }