KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > MessageId


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20
21 /**
22  * @openwire:marshaller code="110"
23  * @version $Revision: 1.12 $
24  */

25 public class MessageId implements DataStructure, Comparable JavaDoc<MessageId> {
26
27     public static final byte DATA_STRUCTURE_TYPE=CommandTypes.MESSAGE_ID;
28     
29     protected ProducerId producerId;
30     protected long producerSequenceId;
31     protected long brokerSequenceId;
32     
33     transient private String JavaDoc key;
34     transient private int hashCode;
35
36     public MessageId() {
37         this.producerId = new ProducerId();
38     }
39     
40     public MessageId(ProducerInfo producerInfo, long producerSequenceId) {
41         this.producerId = producerInfo.getProducerId();
42         this.producerSequenceId = producerSequenceId;
43     }
44
45     public MessageId(String JavaDoc messageKey) {
46         setValue(messageKey);
47     }
48     
49     public MessageId(String JavaDoc producerId, long producerSequenceId) {
50         this( new ProducerId(producerId), producerSequenceId);
51     }
52     
53     public MessageId(ProducerId producerId, long producerSequenceId) {
54         this.producerId=producerId;
55         this.producerSequenceId = producerSequenceId;
56     }
57     
58     /**
59      * Sets the value as a String
60      */

61     public void setValue(String JavaDoc messageKey) {
62         key = messageKey;
63             // Parse off the sequenceId
64
int p = messageKey.lastIndexOf(":");
65             if( p >= 0 ) {
66                 producerSequenceId = Long.parseLong(messageKey.substring(p+1));
67                 messageKey = messageKey.substring(0,p);
68             }
69             producerId = new ProducerId(messageKey);
70     }
71     
72     /**
73      * Sets the transient text view of the message which will be ignored
74      * if the message is marshaled on a transport; so is only for in-JVM changes
75      * to accommodate foreign JMS message IDs
76      */

77     public void setTextView(String JavaDoc key) {
78         this.key = key;
79     }
80
81     public byte getDataStructureType() {
82         return DATA_STRUCTURE_TYPE;
83     }
84
85     public boolean equals(Object JavaDoc o) {
86         if( this == o )
87             return true;
88         if( o==null || o.getClass() != getClass() )
89             return false;
90         
91         MessageId id = (MessageId) o;
92         return producerSequenceId==id.producerSequenceId && producerId.equals(id.producerId);
93     }
94     
95     public int hashCode() {
96         if( hashCode == 0 ) {
97             hashCode = producerId.hashCode() ^ (int)producerSequenceId;
98         }
99         return hashCode;
100     }
101     
102     public String JavaDoc toString() {
103         if(key==null) {
104             key = producerId.toString()+":"+producerSequenceId;
105         }
106         return key;
107     }
108
109     /**
110      * @openwire:property version=1 cache=true
111      */

112     public ProducerId getProducerId() {
113         return producerId;
114     }
115     public void setProducerId(ProducerId producerId) {
116         this.producerId = producerId;
117     }
118
119     /**
120      * @openwire:property version=1
121      */

122     public long getProducerSequenceId() {
123         return producerSequenceId;
124     }
125     public void setProducerSequenceId(long producerSequenceId) {
126         this.producerSequenceId = producerSequenceId;
127     }
128
129     /**
130      * @openwire:property version=1
131      */

132     public long getBrokerSequenceId() {
133         return brokerSequenceId;
134     }
135     public void setBrokerSequenceId(long brokerSequenceId) {
136         this.brokerSequenceId = brokerSequenceId;
137     }
138
139     public boolean isMarshallAware() {
140         return false;
141     }
142     
143     public MessageId copy(){
144         MessageId copy = new MessageId(producerId, producerSequenceId);
145         copy.key = key;
146         copy.brokerSequenceId = brokerSequenceId ;
147         return copy;
148     }
149
150     /**
151      * @param o
152      * @return
153      * @see java.lang.Comparable#compareTo(java.lang.Object)
154      */

155     public int compareTo(MessageId other){
156         int result = -1;
157         if (other != null) {
158             result = this.toString().compareTo(other.toString());
159         }
160         return result;
161     }
162 }
163
Popular Tags