KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > AbstractGroup


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

17
18 /* $Id: AbstractGroup.java 43241 2004-08-16 16:36:57Z andreas $ */
19
20 package org.apache.lenya.ac.impl;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.lenya.ac.AccessControlException;
26 import org.apache.lenya.ac.Accreditable;
27 import org.apache.lenya.ac.Group;
28 import org.apache.lenya.ac.Groupable;
29
30
31 /**
32  * A group is a set of {@link Groupable}s.
33  */

34 public abstract class AbstractGroup extends AbstractItem implements Accreditable, Group {
35     /**
36      * Creates a new group.
37      */

38     public AbstractGroup() {
39     }
40
41     /**
42      * Creates a new group.
43      * @param id The group ID.
44      */

45     public AbstractGroup(String JavaDoc id) {
46         setId(id);
47     }
48
49     private Set JavaDoc members = new HashSet JavaDoc();
50
51     /**
52      * Returns the members of this group.
53      * @return An array of {@link Groupable}s.
54      */

55     public Groupable[] getMembers() {
56         return (Groupable[]) members.toArray(new Groupable[members.size()]);
57     }
58
59     /**
60     * Adds a member to this group.
61      * @param member The member to add.
62      */

63     public void add(Groupable member) {
64         assert (member != null) && !members.contains(member);
65         members.add(member);
66         member.addedToGroup(this);
67     }
68
69     /**
70      * Removes a member from this group.
71      * @param member The member to remove.
72      */

73     public void remove(Groupable member) {
74         assert (member != null) && members.contains(member);
75         members.remove(member);
76         member.removedFromGroup(this);
77     }
78     
79     /**
80      * Removes all members from this group.
81      */

82     public void removeAllMembers() {
83         Groupable[] members = getMembers();
84         for (int i = 0; i < members.length; i++) {
85             remove(members[i]);
86         }
87     }
88
89     /**
90      * Returns if this group contains this member.
91      * @param member The member to check.
92      * @return A boolean value.
93      */

94     public boolean contains(Groupable member) {
95         return members.contains(member);
96     }
97
98     /**
99      * @see org.apache.lenya.ac.Accreditable#getAccreditables()
100      */

101     public Accreditable[] getAccreditables() {
102         Accreditable[] accreditables = { this };
103         return accreditables;
104     }
105     
106     /**
107      * Delete a group
108      *
109      * @throws AccessControlException if the delete failed
110      */

111     public void delete() throws AccessControlException {
112         Groupable[] members = getMembers();
113         for (int i = 0; i < members.length; i++) {
114             remove(members[i]);
115         }
116     }
117
118 }
119
Popular Tags