KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > group > MessageGroupHashBucket


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.broker.region.group;
19
20 import org.apache.activemq.command.ConsumerId;
21
22 /**
23  * Uses hash-code buckets to associate consumers with sets of message group IDs.
24  *
25  * @version $Revision: 426384 $
26  */

27 public class MessageGroupHashBucket implements MessageGroupMap {
28
29     private final int bucketCount;
30     private final ConsumerId[] consumers;
31
32     public MessageGroupHashBucket(int bucketCount) {
33         this.bucketCount = bucketCount;
34         this.consumers = new ConsumerId[bucketCount];
35     }
36
37     public void put(String JavaDoc groupId, ConsumerId consumerId) {
38         int bucket = getBucketNumber(groupId);
39         consumers[bucket] = consumerId;
40     }
41
42     public ConsumerId get(String JavaDoc groupId) {
43         int bucket = getBucketNumber(groupId);
44         return consumers[bucket];
45     }
46
47     public ConsumerId removeGroup(String JavaDoc groupId) {
48         int bucket = getBucketNumber(groupId);
49         ConsumerId answer = consumers[bucket];
50         consumers[bucket] = null;
51         return answer;
52     }
53
54     public MessageGroupSet removeConsumer(ConsumerId consumerId) {
55         MessageGroupSet answer = null;
56         for (int i = 0; i < consumers.length; i++) {
57             ConsumerId owner = consumers[i];
58             if (owner != null && owner.equals(consumerId)) {
59                 answer = createMessageGroupSet(i, answer);
60                 consumers[i] = null;
61             }
62         }
63         if (answer == null) {
64             // make an empty set
65
answer = EmptyMessageGroupSet.INSTANCE;
66         }
67         return answer;
68     }
69
70     public String JavaDoc toString() {
71         int count = 0;
72         for (int i = 0; i < consumers.length; i++) {
73             if (consumers[i] != null) {
74                 count++;
75             }
76         }
77         return "active message group buckets: " + count;
78     }
79
80     protected MessageGroupSet createMessageGroupSet(int bucketNumber, final MessageGroupSet parent) {
81         final MessageGroupSet answer = createMessageGroupSet(bucketNumber);
82         if (parent == null) {
83             return answer;
84         }
85         else {
86             // union the two sets together
87
return new MessageGroupSet() {
88                 public boolean contains(String JavaDoc groupID) {
89                     return parent.contains(groupID) || answer.contains(groupID);
90                 }
91             };
92         }
93     }
94
95     protected MessageGroupSet createMessageGroupSet(final int bucketNumber) {
96         return new MessageGroupSet() {
97             public boolean contains(String JavaDoc groupID) {
98                 int bucket = getBucketNumber(groupID);
99                 return bucket == bucketNumber;
100             }
101         };
102     }
103
104     protected int getBucketNumber(String JavaDoc groupId) {
105         int bucket = groupId.hashCode() % bucketCount;
106         // bucket could be negative
107
if( bucket < 0 )
108             bucket *= -1;
109         return bucket;
110     }
111 }
112
Popular Tags