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