void doUnsubscribe(MatchedHandler handler) { Class cxtType = handler.getCxtType(); if (cxtType == null) { cxtType = reflectHandlerCxtType(handler); handler.setCxtType(cxtType); } rwLock.writeLock().lock(); try { if (!handlers.unsubscribe(handler)) { throw new RuntimeException("Handler " + handler + " is not subscribed to " + (isPositive ? "positive " : "negative ") + portType.getClass().getCanonicalName() + " for " + handler.getCxtType().getCanonicalName() + " events."); } } finally { rwLock.writeLock().unlock(); } }
void subscribe(MatchedHandler h) { totalMatchers++; Class<? extends PatternExtractor> eventType = h.getCxtType(); for (int i = 0; i < matchers.length; i++) { if (matchers[i].eventType.equals(eventType)) { matchers[i].add(h); return; } } MatchedHandlerEntry mhe = new MatchedHandlerEntry(eventType); mhe.add(h); MatchedHandlerEntry[] newHandlers = new MatchedHandlerEntry[matchers.length + 1]; System.arraycopy(matchers, 0, newHandlers, 0, matchers.length); newHandlers[matchers.length] = mhe; matchers = newHandlers; }
@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(); } }
boolean unsubscribe(MatchedHandler h) { Class<? extends PatternExtractor> eventType = h.getCxtType(); boolean found = false; int empties = 0; for (int i = 0; i < matchers.length; i++) { if (matchers[i].eventType.equals(eventType)) { if (matchers[i].remove(h)) { found = true; totalMatchers--; if (matchers[i].isEmpty()) { empties++; matchers[i] = null; } } } } if (empties > 0) { removeMatchers(empties); } return found; }