04f8be223c7569c0c6a1c00641478271cb9d2b2e
[utils] / security / src / main / java / org / wamblee / usermgt / InMemoryGroupSet.java
1 /*
2  * Copyright 2005 the original author or authors.
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 package org.wamblee.usermgt;
18
19 import java.util.Set;
20 import java.util.TreeSet;
21
22 /**
23  * In-memory group set implementation. 
24  */
25 public class InMemoryGroupSet implements GroupSet {
26     
27     /**
28      * Groups. 
29      */
30     private Set<Group> _groups; 
31     
32     /**
33      * Constructs an empty group set. 
34      */
35     public InMemoryGroupSet() { 
36         _groups = new TreeSet<Group>();
37     }
38     
39     /* (non-Javadoc)
40      * @see org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
41      */
42     public void groupModified(Group aGroup) {
43         _groups.remove(aGroup); 
44         _groups.add(aGroup);
45     }
46
47     /* (non-Javadoc)
48      * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
49      */
50     public Group find(String aName) {
51         for (Group group: _groups) { 
52             if ( group.getName().equals(aName)) { 
53                 return new Group(group);
54             }
55         }
56         return null; 
57     }
58
59     /* (non-Javadoc)
60      * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
61      */
62     public boolean contains(Group aGroup) {
63         return _groups.contains(aGroup);
64     }
65
66     /* (non-Javadoc)
67      * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
68      */
69     public boolean add(Group aGroup) {
70         return _groups.add(aGroup);
71     }
72
73     /* (non-Javadoc)
74      * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
75      */
76     public boolean remove(Group aGroup) {
77         return _groups.remove(aGroup);
78     }
79
80     /* (non-Javadoc)
81      * @see org.wamblee.usermgt.GroupSet#list()
82      */
83     public Set<Group> list() {
84         Set<Group> list = new TreeSet<Group>(); 
85         for (Group group: _groups) { 
86             list.add(new Group(group));
87         }
88         return list;
89     }
90     
91     /* (non-Javadoc)
92      * @see org.wamblee.usermgt.GroupSet#size()
93      */
94     public int size() {
95         return _groups.size(); 
96     }
97
98 }