KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > UserManager


1 /**
2  * com.mckoi.database.UserManager 11 Nov 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * A class that manages the list of users connected to the engine.
31  * <p>
32  * This class is thread safe, however it is recommended that the callee should
33  * synchronize over this object when inspecting a subset of the user list.
34  * The reason being that a user can connect or disconnect at any time.
35  *
36  * @author Tobias Downer
37  */

38
39 public final class UserManager {
40
41   /**
42    * The list of User objects that are currently connected to the database
43    * engine.
44    */

45   private ArrayList JavaDoc user_list;
46
47   /**
48    * Constructs the UserManager.
49    */

50   UserManager() {
51     user_list = new ArrayList JavaDoc();
52   }
53
54   /**
55    * Called when a new user connects to the engine.
56    */

57   synchronized void userLoggedIn(User user) {
58     if (!user_list.contains(user)) {
59       user_list.add(user);
60     }
61     else {
62       throw new Error JavaDoc("UserManager already has this User instance logged in.");
63     }
64   }
65
66   /**
67    * Called when the user logs out of the engine.
68    */

69   synchronized void userLoggedOut(User user) {
70     user_list.remove(user);
71   }
72
73   /**
74    * Returns the number of users that are logged in.
75    */

76   public synchronized int userCount() {
77     return user_list.size();
78   }
79
80   /**
81    * Returns the User object at index 'n' in the manager where 0 is the first
82    * user.
83    */

84   public synchronized User userAt(int n) {
85     return (User) user_list.get(n);
86   }
87
88 }
89
Popular Tags