KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.lenya.ac.impl;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.lenya.ac.Accreditable;
25 import org.apache.lenya.ac.Group;
26 import org.apache.lenya.ac.Groupable;
27
28 /**
29  * Abstract implementation for group members.
30  * @version $Id: AbstractGroupable.java 43241 2004-08-16 16:36:57Z andreas $
31  */

32 public abstract class AbstractGroupable extends AbstractItem implements Groupable, Accreditable {
33     private Set JavaDoc groups = new HashSet JavaDoc();
34
35     /**
36      * @see org.apache.lenya.ac.Groupable#addedToGroup(org.apache.lenya.ac.Group)
37      */

38     public void addedToGroup(Group group) {
39         assert group != null;
40         assert group.contains(this);
41         groups.add(group);
42     }
43
44     /**
45      * @see org.apache.lenya.ac.Groupable#removedFromGroup(org.apache.lenya.ac.Group)
46      */

47     public void removedFromGroup(Group group) {
48         assert group != null;
49         assert !group.contains(this);
50         groups.remove(group);
51     }
52
53     /**
54      * @see org.apache.lenya.ac.Groupable#getGroups()
55      */

56     public Group[] getGroups() {
57         return (Group[]) groups.toArray(new Group[groups.size()]);
58     }
59
60     /**
61      * Removes this groupable from all its groups.
62      */

63     public void removeFromAllGroups() {
64         Group[] groups = getGroups();
65
66         for (int i = 0; i < groups.length; i++) {
67             groups[i].remove(this);
68         }
69     }
70
71     /**
72      * @see org.apache.lenya.ac.Accreditable#getAccreditables()
73      */

74     public Accreditable[] getAccreditables() {
75         Set JavaDoc accreditables = new HashSet JavaDoc();
76         accreditables.add(this);
77
78         Group[] groups = getGroups();
79
80         for (int i = 0; i < groups.length; i++) {
81             Accreditable[] groupAccreditables = groups[i].getAccreditables();
82             accreditables.addAll(Arrays.asList(groupAccreditables));
83         }
84
85         return (Accreditable[]) accreditables.toArray(new Accreditable[accreditables.size()]);
86     }
87
88 }
Popular Tags