2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.usermgt;
18 import java.util.ArrayList;
19 import java.util.List;
21 import java.util.TreeSet;
22 import java.util.concurrent.atomic.AtomicLong;
25 * In-memory group set implementation.
27 * @author Erik Brakkee
29 public class InMemoryGroupSet implements GroupSet {
31 private AtomicLong pk = new AtomicLong(1l);
36 private List<Group> groups;
39 * Constructs an empty group set.
41 public InMemoryGroupSet() {
42 groups = new ArrayList<Group>();
49 * org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
51 public void groupModified(Group aGroup) {
52 for (int i = 0; i < groups.size(); i++) {
53 if (groups.get(i).getPrimaryKey().equals(aGroup.getPrimaryKey())) {
64 * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
66 public Group find(String aName) {
67 for (Group group : groups) {
68 if (group.getName().equals(aName)) {
79 * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
81 public boolean contains(Group aGroup) {
82 return groups.contains(aGroup);
88 * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
90 public boolean add(Group aGroup) {
91 aGroup.setPrimaryKey(pk.getAndIncrement());
92 if ( find(aGroup.getName()) != null ) {
95 return groups.add(aGroup);
101 * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
103 public boolean remove(Group aGroup) {
104 return groups.remove(aGroup);
110 * @see org.wamblee.usermgt.GroupSet#list()
112 public Set<Group> list() {
113 Set<Group> list = new TreeSet<Group>();
115 for (Group group : groups) {
116 list.add(new Group(group));
125 * @see org.wamblee.usermgt.GroupSet#size()
128 return groups.size();