b0fc90baecb1a3cb0c907443b2afa914b8179006
[utils] / security / impl / src / main / java / org / wamblee / security / authentication / InMemoryGroupSet.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 java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.TreeSet;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 /**
25  * In-memory group set implementation.
26  * 
27  * @author Erik Brakkee
28  */
29 public class InMemoryGroupSet implements GroupSet {
30
31     private AtomicLong pk = new AtomicLong(1l);
32
33     /**
34      * Groups.
35      */
36     private List<Group> groups;
37
38     /**
39      * Constructs an empty group set.
40      */
41     public InMemoryGroupSet() {
42         groups = new ArrayList<Group>();
43     }
44
45     /*
46      * (non-Javadoc)
47      * 
48      * @see
49      * org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
50      */
51     public void groupModified(Group aGroup) {
52         for (int i = 0; i < groups.size(); i++) {
53             if (groups.get(i).getPrimaryKey().equals(aGroup.getPrimaryKey())) {
54                 groups.remove(i);
55                 groups.add(aGroup);
56                 return;
57             }
58         }
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
65      */
66     public Group find(String aName) {
67         for (Group group : groups) {
68             if (group.getName().equals(aName)) {
69                 return group;
70             }
71         }
72
73         return null;
74     }
75
76     /*
77      * (non-Javadoc)
78      * 
79      * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
80      */
81     public boolean contains(Group aGroup) {
82         return groups.contains(aGroup);
83     }
84
85     /*
86      * (non-Javadoc)
87      * 
88      * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
89      */
90     public boolean add(Group aGroup) {
91         aGroup.setPrimaryKey(pk.getAndIncrement());
92         if (find(aGroup.getName()) != null) {
93             return false;
94         }
95         return groups.add(aGroup);
96     }
97
98     /*
99      * (non-Javadoc)
100      * 
101      * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
102      */
103     public boolean remove(Group aGroup) {
104         return groups.remove(aGroup);
105     }
106
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.wamblee.usermgt.GroupSet#list()
111      */
112     public Set<Group> list() {
113         Set<Group> list = new TreeSet<Group>();
114
115         for (Group group : groups) {
116             list.add(new Group(group));
117         }
118
119         return list;
120     }
121
122     /*
123      * (non-Javadoc)
124      * 
125      * @see org.wamblee.usermgt.GroupSet#size()
126      */
127     public int size() {
128         return groups.size();
129     }
130 }