001package org.apache.fulcrum.security.model.basic.entity.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Set;
023
024import org.apache.fulcrum.security.entity.User;
025import org.apache.fulcrum.security.entity.impl.SecurityEntityImpl;
026import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
027import org.apache.fulcrum.security.util.UserSet;
028
029/**
030 * Represents the "basic" model where users are part of groups, but nothing
031 * else.
032 * 
033 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
034 * @version $Id: BasicGroup.java 223057 2004-07-05 19:28:23Z epugh $
035 */
036public class BasicGroupImpl extends SecurityEntityImpl implements BasicGroup
037{
038    /**
039         * Serial version
040         */
041        private static final long serialVersionUID = 8754093174457116551L;
042        
043        /** User set **/
044        private Set<? extends User> userSet = new UserSet<>();
045
046    /**
047     * Get the users that are part of this group
048     * 
049     * @return a set of users
050     */
051    public UserSet<?> getUsers()
052    {
053        if (userSet instanceof UserSet)
054        {
055            return (UserSet<?>) userSet;
056        }
057        else
058        {
059            userSet = new UserSet<>(userSet);
060            return (UserSet<?>) userSet;
061        }
062    }
063
064    /**
065     * Set the users that are part of this group
066     * 
067     * @param userSet
068     *            a set of users
069     */
070    public void setUsers(UserSet userSet)
071    {
072        if (userSet != null)
073        {
074            this.userSet = userSet;
075        }
076        else
077        {
078            this.userSet = new UserSet<>();
079        }
080    }
081
082    /**
083     * Get the users that are part of this group as a Set
084     * 
085     * @return a set of users
086     */
087    @SuppressWarnings("unchecked")
088    public <T extends User> Set<T> getUsersAsSet()
089    {
090        return (Set<T>) userSet;
091    }
092
093    /**
094     * Set the users that are part of this group as a Set
095     * 
096     * @param users
097     *            a set of users
098     */
099    public <T extends User> void setUsersAsSet(Set<T> users)
100    {
101        this.userSet = users;
102    }
103
104    /**
105     * Add a user to this group
106     * 
107     * @param user
108     *            the user to add
109     */
110    public void addUser(User user)
111    {
112        getUsers().add(user);
113    }
114
115    /**
116     * Remove a user from this group
117     * 
118     * @param user
119     *            the user to remove
120     */
121    public void removeUser(User user)
122    {
123        getUsers().remove(user);
124    }
125}