KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > UserAccountList


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * User Account List Class
23  */

24 public class UserAccountList
25 {
26
27     // User account list
28

29     private Vector JavaDoc<UserAccount> m_users;
30
31     /**
32      * Create a user account list.
33      */

34     public UserAccountList()
35     {
36         m_users = new Vector JavaDoc<UserAccount>();
37     }
38
39     /**
40      * Add a user to the list of accounts.
41      *
42      * @param user UserAccount
43      */

44     public final void addUser(UserAccount user)
45     {
46
47         // Check if the user exists on the list
48

49         removeUser(user);
50         m_users.add(user);
51     }
52
53     /**
54      * Find the required user account details.
55      *
56      * @param user java.lang.String
57      * @return UserAccount
58      */

59     public final UserAccount findUser(String JavaDoc user)
60     {
61
62         // Search for the specified user account
63

64         for (int i = 0; i < m_users.size(); i++)
65         {
66             UserAccount acc = m_users.get(i);
67             if (acc.getUserName().equalsIgnoreCase(user))
68                 return acc;
69         }
70
71         // User not found
72

73         return null;
74     }
75
76     /**
77      * Determine if the specified user account exists in the list.
78      *
79      * @return boolean
80      * @param user java.lang.String
81      */

82     public final boolean hasUser(String JavaDoc user)
83     {
84
85         // Search for the specified user account
86

87         for (int i = 0; i < m_users.size(); i++)
88         {
89             UserAccount acc = m_users.get(i);
90             if (acc.getUserName().compareTo(user) == 0)
91                 return true;
92         }
93
94         // User not found
95

96         return false;
97     }
98
99     /**
100      * Return the specified user account details
101      *
102      * @param idx int
103      * @return UserAccount
104      */

105     public final UserAccount getUserAt(int idx)
106     {
107         if (idx >= m_users.size())
108             return null;
109         return m_users.get(idx);
110     }
111
112     /**
113      * Return the number of defined user accounts.
114      *
115      * @return int
116      */

117     public final int numberOfUsers()
118     {
119         return m_users.size();
120     }
121
122     /**
123      * Remove all user accounts from the list.
124      */

125     public final void removeAllUsers()
126     {
127         m_users.removeAllElements();
128     }
129
130     /**
131      * Remvoe the specified user account from the list.
132      *
133      * @param userAcc UserAccount
134      */

135     public final void removeUser(UserAccount userAcc)
136     {
137
138         // Search for the specified user account
139

140         for (int i = 0; i < m_users.size(); i++)
141         {
142             UserAccount acc = m_users.get(i);
143             if (acc.getUserName().compareTo(userAcc.getUserName()) == 0)
144             {
145                 m_users.remove(i);
146                 return;
147             }
148         }
149     }
150
151     /**
152      * Remvoe the specified user account from the list.
153      *
154      * @param user java.lang.String
155      */

156     public final void removeUser(String JavaDoc user)
157     {
158
159         // Search for the specified user account
160

161         for (int i = 0; i < m_users.size(); i++)
162         {
163             UserAccount acc = m_users.get(i);
164             if (acc.getUserName().compareTo(user) == 0)
165             {
166                 m_users.remove(i);
167                 return;
168             }
169         }
170     }
171
172     /**
173      * Return the user account list as a string.
174      *
175      * @return java.lang.String
176      */

177     public String JavaDoc toString()
178     {
179         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
180
181         str.append("[");
182         str.append(m_users.size());
183         str.append(":");
184
185         for (int i = 0; i < m_users.size(); i++)
186         {
187             UserAccount acc = m_users.get(i);
188             str.append(acc.getUserName());
189             str.append(",");
190         }
191         str.append("]");
192
193         return str.toString();
194     }
195 }
Popular Tags