/** * 根据id删除 当前账号的登陆集中的SessionUser * * @param id SessionUser id */ public void deleteUser(int id) { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); Iterator<SessionUser> iterator = users.iterator(); SessionUser sessionUser; while (iterator.hasNext()) { sessionUser = iterator.next(); if (id == sessionUser.getId()) { iterator.remove(); sessionUserCollection.decrementTotal(); } } }
/** 向当前账号的登陆集添加一个SessionUser */ public void addUser() { SessionUser user = new SessionUser(); user.setExpire(expire); LinkedList<SessionUser> users = sessionUserCollection.getUsers(); sessionUserCollection.incrementTotal(); if (users.size() == 0) { user.setId(sessionUserCollection.getTotal()); } else { user.setId(users.getLast().getId() + 1); } users.add(user); }
/** * 获取当前未过期的登陆数,同时删除已过期的登陆 * * @return 当前未过期的登录数 */ public int getAliveSessions() { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); if (users.size() == 0) { return 0; } Iterator<SessionUser> iterator = users.iterator(); SessionUser user; while (iterator.hasNext()) { user = iterator.next(); if (isUserExpired(user)) { iterator.remove(); sessionUserCollection.decrementTotal(); } } return sessionUserCollection.getTotal(); }
/** * 根据id获取当前账号登陆集的 登陆session * * @param id 某个SessionUser的id * @return SessionUser */ public SessionUser getUser(int id) { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); for (SessionUser user : users) { if (id == user.getId()) { return user; } } return null; }
/** * 某个用户是否过期,(id,createTime) 才能唯一标示一个用户 * * @param id sessionUser id * @param createTime SessionUser 创建时间 * @return 过期返回true,未过期返回false */ public boolean isUserExpired(int id, Date createTime) { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); for (SessionUser user : users) { if (user.getId() == id && user.getCreateTime().getTime() == createTime.getTime()) { return isUserExpired(user); } } return true; }
/** * 获取当前账号的登陆集中的SessionUser 的属性 * * @param id SessionUser id * @param key 属性 key * @return 属性 value */ public Object getProperty(int id, String key) { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); for (SessionUser user : users) { if (id == user.getId()) { return user.getProperties().get(key); } } return null; }
/** * 给当前账号的登陆集中的 SessionUser 添加属性 * * @param id SessionUser id * @param key 属性 key * @param object 属性 value * @return 是否添加成功,无此id返回false */ public boolean addProperty(int id, String key, Object object) { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); Iterator<SessionUser> iterator = users.iterator(); SessionUser sessionUser; while (iterator.hasNext()) { sessionUser = iterator.next(); if (id == sessionUser.getId()) { sessionUser.getProperties().put(key, object); return true; } } return false; }
/** * 只传入expire,则新建一个SessionCollection和第一个SessionUser * * @param expire 过期时间 */ public SessionUserCollectionHelper(long expire) { this.sessionUserCollection = new SessionUserCollection(); sessionUserCollection.setTotal(0); this.expire = expire; addUser(); }
/** 删除当前账号的登陆集的最后一个SessionUser */ public void deleteLastUser() { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); users.removeLast(); sessionUserCollection.decrementTotal(); }
/** * 返回前账号登陆集中最后一个登陆的sessionUser * * @return SessionUser */ public SessionUser getLastUser() { LinkedList<SessionUser> users = sessionUserCollection.getUsers(); return users.getLast(); }