public void register(MsgHandler handler) { if (handler == null) { throw new IllegalArgumentException("register handler cannot be null"); } int[] interests = handler.getInterests(); if (interests != null) { for (Integer interest : interests) { Set<MsgHandler> msgHandlers = handlers.get(interest); if (msgHandlers == null) { CopyOnWriteArraySet<MsgHandler> created = new CopyOnWriteArraySet<>(); msgHandlers = handlers.putIfAbsent(interest, msgHandlers); if (msgHandlers == null) { msgHandlers = created; } } msgHandlers.add(handler); } for (Integer interest : interests) { Msg msg = stickyMsg.get(interest); if (msg != null) { handler.onMsg(msg); } } } else { throw new IllegalArgumentException("register handler which has no interest"); } }
private void post(Msg msg, boolean stick) { Integer what = msg.what(); Set<MsgHandler> msgHandlers = handlers.get(what); if (msgHandlers != null && !msgHandlers.isEmpty()) { for (MsgHandler h : msgHandlers) { h.onMsg(msg); } } else { if (!stick) { logger.warning("post: dead event: " + msg); } } if (stick) { stickyMsg.put(what, msg); } }
public void unregister(MsgHandler handler) { if (handler == null) { throw new IllegalArgumentException("unregister handler cannot be null"); } int[] interests = handler.getInterests(); if (interests == null) { for (Integer interest : interests) { Set<MsgHandler> msgHandlers = handlers.get(interest); if (msgHandlers != null) { msgHandlers.remove(handler); } } } else { throw new IllegalArgumentException("unregister handler which has no interest"); } }
// Stores current term to database public void storeTerm() { try { Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); String insertTerm = "INSERT INTO term (tID,lname,fname,loc,ext,mgr,ACT," + "termD,serial,model,note) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(insertTerm); ps.setInt(1, gettID()); ps.setString(2, getLname()); ps.setString(3, getFname()); ps.setString(4, getLoc()); ps.setString(5, getExt()); ps.setString(6, getMgr()); ps.setString(7, getACT()); ps.setString(8, getTermD()); ps.setString(9, getSerial()); ps.setString(10, getModel()); ps.setString(11, getNote()); ps.executeUpdate(); } catch (SQLException ex) { MsgHandler.setErrMsg("Error writing to db: ", ex); } }