@Override public boolean matches(Object key) { for (Selector s: selectors) { if (!s.matches(key)) { return false; } } return true; }
@Override public void value(final MutableList<Registration<K, ? extends V>> regs) { Iterator<Registration<K, ? extends V>> registrationIterator = regs.iterator(); Registration<K, ? extends V> reg; while (registrationIterator.hasNext()) { reg = registrationIterator.next(); if (reg.getSelector().matches(key)) { registrationIterator.remove(); modified.compareAndSet(false, true); } } if (useCache && modified.get()) { threadLocalCache.clear(); } } });
@Override @SuppressWarnings("unchecked") public synchronized boolean unregister(Object key) { boolean found = false; for (Selector sel : registrations.keySet()) { if (!sel.matches(key)) { continue; } if (null != registrations.remove(sel) && !found) { found = true; } } if (useCache) cache.remove(key); return found; }
@Override public List<Registration<K, ? extends V>> select(K key) { // use a thread-local cache UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); // maybe pull Registrations from cache for this key List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } // cache not used or cache miss cacheMiss(key); selectedRegs = FastList.newList(); // find Registrations based on Selector for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } // nothing found, maybe invoke handler if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } // return return selectedRegs; }