1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activecluster.group;
19
20 import org.activecluster.Node;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /***
26 * Represents a logical group of nodes in a cluster,
27 * such as a Master and a number of Slaves which operate as a
28 * logical unit.
29 * <p/>
30 * A cluster can be divided into a single group, or many groups
31 * depending on the policy required.
32 * <p/>
33 * The number of groups could be application defined; created on demand
34 * or there could even be one group for each node, with other nodes acting
35 * as buddy nodes in each nodes' group (i.e. each node is a master with N
36 * buddies/slaves)
37 *
38 * @version $Revision: 1.1 $
39 */
40 public class Group {
41 private int minimumMemberCount;
42 private int maximumMemberCount;
43 private List members = new ArrayList();
44 private int memberCount;
45
46 public Group() {
47 }
48
49 public Group(int minimumMemberCount, int maximumMemberCount) {
50 this.minimumMemberCount = minimumMemberCount;
51 this.maximumMemberCount = maximumMemberCount;
52 }
53
54 public synchronized List getMembers() {
55 return new ArrayList(members);
56 }
57
58 /***
59 * Adds a node to the given group
60 *
61 * @return the index of the node in the group (0 = master, 1..N = slave)
62 */
63 public synchronized int addMember(Node node) {
64 int index = members.indexOf(node);
65 if (index >= 0) {
66 return index;
67 }
68 members.add(node);
69 return memberCount++;
70 }
71
72 public synchronized boolean removeMember(Node node) {
73 boolean answer = members.remove(node);
74 if (answer) {
75 memberCount--;
76 }
77 return answer;
78 }
79
80
81 /***
82 * Returns true if the group is usable, that it has enough members to be used.
83 */
84 public boolean isUsable() {
85 return memberCount >= minimumMemberCount;
86 }
87
88 /***
89 * Returns true if the group cannot accept any more new members
90 */
91 public boolean isFull() {
92 return memberCount >= maximumMemberCount;
93 }
94
95 public int getMemberCount() {
96 return memberCount;
97 }
98
99 public int getMaximumMemberCount() {
100 return maximumMemberCount;
101 }
102
103 public void setMaximumMemberCount(int maximumMemberCount) {
104 this.maximumMemberCount = maximumMemberCount;
105 }
106
107 public int getMinimumMemberCount() {
108 return minimumMemberCount;
109 }
110
111 public void setMinimumMemberCount(int minimumMemberCount) {
112 this.minimumMemberCount = minimumMemberCount;
113 }
114 }