/** * 某个用户是否过期,(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; }
/** * 根据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; }
/** * 获取当前账号的登陆集中的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; }
/** * 根据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); }
/** * 给当前账号的登陆集中的 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; }
/** * 某个用户是否过期 * * @param user sessionUser * @return 过期返回true,否则返回false */ public boolean isUserExpired(SessionUser user) { return (user.getExpire() * 1000L + user.getCreateTime().getTime()) < (new Date().getTime()); }