void add(MatchedHandler h) { MatchedHandler[] handlers = subscriptions.get(h.pattern()); if (handlers == null) { handlers = new MatchedHandler[]{h}; subscriptions.put(h.pattern(), handlers); } else { MatchedHandler[] newHandlers = new MatchedHandler[handlers.length + 1]; System.arraycopy(handlers, 0, newHandlers, 0, handlers.length); newHandlers[handlers.length] = h; subscriptions.put(h.pattern(), newHandlers); } }
boolean remove(MatchedHandler h) { MatchedHandler[] handlers = subscriptions.get(h.pattern()); if (handlers == null) { return false; } else { boolean found = false; if (handlers.length == 1) { for (int i = 0; i < handlers.length; i++) { if (handlers[i] == h) { subscriptions.remove(h.pattern()); return true; } } } else { MatchedHandler[] newHandlers = new MatchedHandler[handlers.length - 1]; int i = 0, j = 0; while (i < handlers.length) { if (handlers[i] == h) { found = true; } else { newHandlers[j] = handlers[i]; j++; } i++; } } return found; } }
@Override public void doSubscribe(MatchedHandler handler) { if (handler instanceof ClassMatchedHandler) { Object matchType = handler.pattern(); if (matchType == null) { ClassMatchedHandler cmh = (ClassMatchedHandler) handler; reflectCMHType(cmh); } } Class cxtType = handler.getCxtType(); if (cxtType == null) { cxtType = reflectHandlerCxtType(handler); handler.setCxtType(cxtType); } // check that the port type carries the event type in this direction if (!portType.hasEvent(isPositive, cxtType)) { throw new RuntimeException("Cannot subscribe handler " + handler + " to " + (isPositive ? "positive " : "negative ") + portType.getClass().getCanonicalName() + " for " + cxtType.getCanonicalName() + " events."); } rwLock.writeLock().lock(); try { handlers.subscribe(handler); } finally { rwLock.writeLock().unlock(); } }