59129213a4837ad41d8b04f55f253a73c750aadf
[utils] / security / impl / src / main / java / org / wamblee / security / authentication / InMemoryUserSet.java
1 /*
2  * Copyright 2005-2010 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 package org.wamblee.security.authentication;
17
18 import org.wamblee.security.encryption.MessageDigester;
19
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 /**
24  * In-memory user set.
25  * 
26  * @author Erik Brakkee
27  */
28 public class InMemoryUserSet extends AbstractUserSet {
29     /**
30      * Users. All users in this set have their password validator and encoder
31      * set.
32      */
33     private Set<User> users;
34
35     /**
36      * Constructs an empty user set.
37      */
38     public InMemoryUserSet(NameValidator aPasswordValidator,
39         MessageDigester aPasswordEncoder) {
40         super(aPasswordValidator, aPasswordEncoder);
41         users = new TreeSet<User>();
42     }
43
44     @Override
45     public void userModified(User aUser) {
46         users.remove(aUser);
47         setPasswordInfo(aUser);
48         users.add(aUser);
49     }
50
51     @Override
52     public User find(String aName) {
53         for (User user : users) {
54             if (user.getName().equals(aName)) {
55                 return user;
56             }
57         }
58
59         return null;
60     }
61
62     @Override
63     public boolean add(User aUser) {
64         setPasswordInfo(aUser);
65
66         return users.add(aUser);
67     }
68
69     @Override
70     public boolean contains(User aUser) {
71         return users.contains(aUser);
72     }
73
74     @Override
75     public boolean remove(User aUser) {
76         return users.remove(aUser);
77     }
78
79     @Override
80     public Set<User> list() {
81         Set<User> list = new TreeSet<User>();
82         list.addAll(users);
83         return list;
84     }
85
86     @Override
87     public Set<User> list(Group aGroup) {
88         Set<User> result = new TreeSet<User>();
89
90         for (User user : users) {
91             if (user.getGroups().contains(aGroup)) {
92                 result.add(user);
93             }
94         }
95
96         return result;
97     }
98
99     @Override
100     public int size() {
101         return users.size();
102     }
103     
104     @Override
105     public void clearCache() {
106         // Empty   
107     }
108 }