b9d133abcd397287b3ebf400d6ce48e252eb2625
[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  * @author Erik Brakkee
26  */
27 public class InMemoryGroupSet implements GroupSet {
28     
29     /**
30      * Groups. 
31      */
32     private Set<Group> groups; 
33     
34     /**
35      * Constructs an empty group set. 
36      */
37     public InMemoryGroupSet() { 
38         groups = new TreeSet<Group>();
39     }
40     
41     /* (non-Javadoc)
42      * @see org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
43      */
44     public void groupModified(Group aGroup) {
45         groups.remove(aGroup); 
46         groups.add(aGroup);
47     }
48
49     /* (non-Javadoc)
50      * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
51      */
52     public Group find(String aName) {
53         for (Group group: groups) { 
54             if ( group.getName().equals(aName)) { 
55                 return new Group(group);
56             }
57         }
58         return null; 
59     }
60
61     /* (non-Javadoc)
62      * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
63      */
64     public boolean contains(Group aGroup) {
65         return groups.contains(aGroup);
66     }
67
68     /* (non-Javadoc)
69      * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
70      */
71     public boolean add(Group aGroup) {
72         return groups.add(aGroup);
73     }
74
75     /* (non-Javadoc)
76      * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
77      */
78     public boolean remove(Group aGroup) {
79         return groups.remove(aGroup);
80     }
81
82     /* (non-Javadoc)
83      * @see org.wamblee.usermgt.GroupSet#list()
84      */
85     public Set<Group> list() {
86         Set<Group> list = new TreeSet<Group>(); 
87         for (Group group: groups) { 
88             list.add(new Group(group));
89         }
90         return list;
91     }
92     
93     /* (non-Javadoc)
94      * @see org.wamblee.usermgt.GroupSet#size()
95      */
96     public int size() {
97         return groups.size(); 
98     }
99
100 }