@Test public void emptyResultsAreCachedWhenThereAreNoRegistrations() { this.cachingRegistry.select("key"); this.cachingRegistry.select("key"); assertEquals(1, this.cacheMisses.get()); }
@Override public List<Registration<K, ? extends V>> select(K key) { UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } cacheMiss(key); selectedRegs = FastList.newList(); for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } return selectedRegs; }
CachingRegistry implements Registry<K, V> { @Override public List<Registration<K, ? extends V>> select(K key) { UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } cacheMiss(key); selectedRegs = FastList.newList(); for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } return selectedRegs; } }
CachingRegistry implements Registry<K, V> { @Override public List<Registration<K, ? extends V>> select(K key) { UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } cacheMiss(key); selectedRegs = FastList.newList(); for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } return selectedRegs; } CachingRegistry(boolean useCache, boolean cacheNotFound, Consumer<K> onNotFound); }
CachingRegistry implements Registry<K, V> { @Override public List<Registration<K, ? extends V>> select(K key) { UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } cacheMiss(key); selectedRegs = FastList.newList(); for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } return selectedRegs; } CachingRegistry(boolean useCache, boolean cacheNotFound, Consumer<K> onNotFound); @Override Registration<K, V> register(Selector<K> sel, V obj); @Override @SuppressWarnings("unchecked") boolean unregister(final K key); @Override List<Registration<K, ? extends V>> select(K key); @Override void clear(); @Override Iterator<Registration<K, ? extends V>> iterator(); }
CachingRegistry implements Registry<K, V> { @Override public List<Registration<K, ? extends V>> select(K key) { UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } cacheMiss(key); selectedRegs = FastList.newList(); for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } return selectedRegs; } CachingRegistry(boolean useCache, boolean cacheNotFound, Consumer<K> onNotFound); @Override Registration<K, V> register(Selector<K> sel, V obj); @Override @SuppressWarnings("unchecked") boolean unregister(final K key); @Override List<Registration<K, ? extends V>> select(K key); @Override void clear(); @Override Iterator<Registration<K, ? extends V>> iterator();  }