0ccbba7237198b8341c97ea93f91ed1473200038
[utils] / security / src / main / java / org / wamblee / usermgt / InMemoryUserSet.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 import org.wamblee.security.encryption.MessageDigester;
23
24 /**
25  * In-memory user set.
26  */
27 public class InMemoryUserSet extends AbstractUserSet {
28
29     /**
30      * Users. All users in this set have their password validator and encoder set. 
31      */
32     private Set<User> _users;
33
34     /**
35      * Constructs an empty user set.
36      */
37     public InMemoryUserSet(NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
38         super(aPasswordValidator, aPasswordEncoder);
39         _users = new TreeSet<User>();
40     }
41
42     /*
43      * (non-Javadoc)
44      * 
45      * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
46      */
47     public void userModified(User aUser) {
48         _users.remove(aUser);
49        setPasswordInfo(aUser); 
50         _users.add(aUser);
51     }
52
53     /*
54      * (non-Javadoc)
55      * 
56      * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
57      */
58     public User find(String aName) {
59         for (User user : _users) {
60             if (user.getName().equals(aName)) {
61                 return new User(user);
62             }
63         }
64         return null;
65     }
66
67     /*
68      * (non-Javadoc)
69      * 
70      * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
71      */
72     public boolean add(User aUser) {
73         setPasswordInfo(aUser);
74         return _users.add(aUser);
75     }
76
77     /*
78      * (non-Javadoc)
79      * 
80      * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
81      */
82     public boolean contains(User aUser) {
83         return _users.contains(aUser);
84     }
85
86     /*
87      * (non-Javadoc)
88      * 
89      * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
90      */
91     public boolean remove(User aUser) {
92         return _users.remove(aUser);
93     }
94
95     /*
96      * (non-Javadoc)
97      * 
98      * @see org.wamblee.usermgt.UserSet#list()
99      */
100     public Set<User> list() {
101         Set<User> list = new TreeSet<User>();
102         for (User user : _users) {
103             list.add(new User(user));
104         }
105         return list;
106     }
107
108     /*
109      * (non-Javadoc)
110      * 
111      * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
112      */
113     public Set<User> list(Group aGroup) {
114         Set<User> result = new TreeSet<User>();
115         for (User user : _users) {
116             if (user.getGroups().contains(aGroup)) {
117                 result.add(new User(user));
118             }
119         }
120         return result;
121     }
122     
123     /* (non-Javadoc)
124      * @see org.wamblee.usermgt.UserSet#size()
125      */
126     public int size() {
127         return _users.size(); 
128     }
129
130 }